Query normalised data¶
Query all books sorted by
language_id
andtitle
:7def select_all_records_ordered_by_language_number(cursor): 8 print("All books ordered by language id and title:") 9 for row in cursor.execute( 10 """SELECT language_code, author, title FROM books 11 ORDER BY language_code,title""" 12 ): 13 print(row)
All books ordered by language id and title: (1, 'Veit Schiele', 'Jupyter Tutorial') (2, 'Veit Schiele', 'Jupyter Tutorial') (2, 'Veit Schiele', 'PyViz Tutorial') (2, 'Veit Schiele', 'Python basics')
In order to receive not only the ID of the languages but also the corresponding language codes, a connection to the language codes stored there is established with
JOIN
via theid
column in thelanguages
table:16def select_all_records_ordered_by_language_code(cursor): 17 print("All books ordered by language code and title:") 18 for row in cursor.execute( 19 """SELECT languages.language_code, books.author, books.title 20 FROM books 21 JOIN languages ON (books.language_code = languages.id) 22 ORDER BY languages.language_code,title""" 23 ): 24 print(row)
All books ordered by language code and title: ('de', 'Veit Schiele', 'Jupyter Tutorial') ('en', 'Veit Schiele', 'Jupyter Tutorial') ('en', 'Veit Schiele', 'PyViz Tutorial') ('en', 'Veit Schiele', 'Python basics')