A query describes the result
In a library request slip you write what you need, for example all books about coffee with the newest first. You don't write which bookcase to go to, in what order to take books off the shelves or how to pick the right ones. How to find the books is the librarian's call: they know the stacks better than you.
SQL works the same way. A query describes the result you need: which rows, which columns, in what order. How to get that result is the DBMS's call. That is why SQL is called a declarative language: it states the goal instead of listing the steps.
For the description to be complete, the query from the previous lesson lacks two parts: which rows to keep and how to order them.
WHERE: which rows
WHERE goes after FROM, and a condition comes after the word WHERE. The DBMS keeps in the result only the rows for which the condition is true:
SELECT name, price FROM menu_items WHERE price < 160; name | price
-----------+--------
Espresso | 140.00
Green tea | 150.00
(2 rows)Of the 13 menu items, only two cost less than 160 rubles. The other 11 rows didn't make it into the result.
A condition can compare like this:
=: equal;<>: not equal;<and>: less and greater;<=and>=: less or equal, greater or equal.
A number is written as is, 160, and text in single quotes: category = 'tea'. Inside the quotes case matters: 'Food' and 'food' are different values. Other kinds of conditions are covered in the chapter on filtering.
ORDER BY: in what order
ORDER BY goes at the very end of the query, and a column name comes after these words. The DBMS sorts the rows by that column in ascending order. The word DESC after the column name reverses the order: the rows go in descending order. Chapter 2 covers sorting in more detail.
SELECT name, price
FROM menu_items
WHERE category = 'tea'
ORDER BY price DESC; name | price
-------------------+--------
Sea buckthorn tea | 230.00
Green tea | 150.00
(2 rows)This is a complete description of the result:
SELECT: names and prices;FROM: of menu items;WHERE: tea only;ORDER BY … DESC: from the most to the least expensive.
The parts of a query are always written in one order: SELECT, FROM, WHERE, ORDER BY. You may leave out WHERE and ORDER BY, but when they are there, they stand in their places.
What, not how
A query is written starting with SELECT. But its meaning builds up in a different order. First the table from FROM is taken, then WHERE drops the rows you don't need, then SELECT keeps the columns you need, and ORDER BY sorts what is left.
The widget holds seven of the thirteen menu items. Press "Next" and watch how many rows remain after each clause and which clause is highlighted in the query:
| name | category | price |
|---|---|---|
| Espresso | coffee | 140.00 |
| Green tea | tea | 150.00 |
| Croissant | food | 160.00 |
| Americano | coffee | 170.00 |
| Latte | coffee | 220.00 |
| Raf | coffee | 280.00 |
| Syrniki | food | 310.00 |
FROM: the database takes table menu_items — 7 rows. Press Next.
This order describes the meaning of the query, not the DBMS's actual work. The DBMS must return the same result as if it ran the clauses one after another. The path to it, it chooses on its own. For example, a column may have an index: a lookup the DBMS uses to find rows without scanning the whole table. Chapter 14 is about indexes.
Interviewers often ask in what order the clauses of a query run. The answer: FROM, WHERE, SELECT, ORDER BY, even though they are written differently.
Since the DBMS chooses the path to the result, without ORDER BY it chooses the order of rows too. That order may change from one run to the next. If the order matters, you have to ask for it.
Here the librarian analogy breaks again. A librarian may bring books in any order, depending on mood. The DBMS chooses its path by rules and always brings exactly what the query describes. An order without ORDER BY is not described, so it is not promised.
Common mistake
The parts of the query are not in their places: ORDER BY comes before WHERE.
SELECT name, price FROM menu_items ORDER BY price WHERE category = 'tea'; -- ✗ERROR: syntax error at or near "WHERE"This is a syntax error: the DBMS cannot parse the query and does not run it. The message shows the word where parsing stopped. By meaning the clauses run as FROM, WHERE, SELECT, ORDER BY, while the order in which they are written is fixed: WHERE always comes before ORDER BY.
Key points
- SQL is a declarative language: a query describes the result, and the DBMS chooses how to get it.
WHEREkeeps the rows for which the condition is true,ORDER BYsorts them,DESCin descending order.- You write
SELECT,FROM,WHERE,ORDER BY, while by meaning they run asFROM,WHERE,SELECT,ORDER BY. - Without
ORDER BYthe order of rows is not guaranteed.