A query's rows come back in no promised order: usually the order they were added to the table, but SQL doesn't guarantee it. To choose the order, end the query with ORDER BY and a column. Rows are sorted smallest first (A to Z for text); add DESC after the column to sort biggest first.
You can sort by more than one column, with a comma between them. The second column only decides between rows that tie on the first, and DESC turns around only the column it follows. A NULL sorts before any value.
query = (
"SELECT name, category "
"FROM products "
"ORDER BY category DESC, name "
)
This lists the office products first, by name A to Z, then kitchen and home, and the gift card, whose category is NULL, last. In this lesson you write two queries, and the check prints both, with show(db, query) and show(db, query2). Your code must print exactly this:
name | price Gift card | 50 Lamp | 29 Teapot | 22 Plant | 15 Mug | 12 Stapler | 9 Notebook | 7 Pen | 2 (8 rows) name | category | price Gift card | NULL | 50 Lamp | home | 29 Plant | home | 15 Teapot | kitchen | 22 Mug | kitchen | 12 Stapler | office | 9 Notebook | office | 7 Pen | office | 2 (8 rows)
Your task: the connection db to shop.db is already there. In query, list each product's name and price, most expensive first. In query2, list name, category and price like a catalog: by category A to Z, and inside each category from the most expensive.
Assignment
Follow the Coding Tutorial to write both queries, one clause per line: SELECT, FROM, then ORDER BY.
HintORDER BY is the last line. Write DESC right after the column it turns around: in query2 that's price, not category. End each string with a space so the clauses don't run together.
Free plan: 30 tutor messages. PRO is unlimited.
