SELECT name, city FROM customers WHERE city = 'London' ORDER BY name;
name
city
Aisha Khan
London
Chen Li
London
SELECT lists the columns you want. SELECT * means all columns: fine for exploring, but in application code name the columns (less data, and code does not break when columns are added).
FROM names the table.
WHERE keeps only rows where the condition is true.
ORDER BY sorts. Without ORDER BY, the order of rows is not guaranteed, even if it looks stable.
WHERE operators
Operator
Example
=, <> (or !=), <, >, <=, >=
price_pence >= 1000
AND, OR, NOT
category = 'Stationery' OR price_pence > 3000
IN (...)
status IN ('paid', 'pending')
BETWEEN a AND b
Inclusive at both ends
LIKE / ILIKE
Pattern match: % any characters, _ one character. ILIKE ignores case (PostgreSQL)
SELECT name, price_pence FROM productsWHERE category = 'Stationery' OR price_pence > 3000ORDER BY price_pence DESC;
name
price_pence
Kettle
3499
Pen Set
1250
Notebook
450
SQL
SELECT name, price_pence FROM products WHERE price_pence BETWEEN 800 AND 1500 ORDER BY price_pence;
name
price_pence
Mug
899
Pen Set
1250
SQL
SELECT name FROM customers WHERE name LIKE 'A%' OR name ILIKE '%li';
name
Aisha Khan
Chen Li
Use brackets when mixing AND and OR: AND binds tighter, so a OR b AND c means a OR (b AND c).
A NULL trap already
SQL
SELECT name FROM customers WHERE city <> 'London';
name
Ben Smith
Eve Jones
Dara Patel is missing, although her city is certainly not London. Her city is NULL (unknown), and NULL <> 'London' is not true, it is unknown, so WHERE drops the row. Lesson 219 explains this fully. To include her: WHERE city <> 'London' OR city IS NULL, or WHERE city IS DISTINCT FROM 'London'.
ORDER BY and LIMIT
SQL
SELECT name, price_pence FROM products ORDER BY price_pence DESC LIMIT 2;
name
price_pence
Kettle
3499
Desk Lamp
2999
ASC (default) or DESC; sort by several columns: ORDER BY category, price_pence DESC.
LIMIT n returns at most n rows; OFFSET m skips m rows first (and has problems for paging, Lesson 222). The SQL standard form is FETCH FIRST 2 ROWS ONLY; SQL Server uses TOP 2.
NULLs sort last in ascending order in PostgreSQL (NULLS FIRST / NULLS LAST changes it).
DISTINCT and aliases
SQL
SELECT DISTINCT category FROM products ORDER BY category;
category
Home
Kitchen
Stationery
An alias renames a column or table in the result: SELECT price_pence / 100.0 AS price_pounds FROM products p. Table aliases (p) make joins shorter (Lesson 215).
The logical order of a query
You write clauses in one order, but the database processes them in another. This explains many errors:
Written order
Logical processing order
1. SELECT
1. FROM (and JOINs)
2. FROM
2. WHERE
3. WHERE
3. GROUP BY
4. GROUP BY
4. HAVING
5. HAVING
5. SELECT (and window functions)
6. ORDER BY
6. DISTINCT
7. LIMIT
7. ORDER BY
8. LIMIT / OFFSET
So you cannot use a SELECT alias in WHERE (WHERE runs before SELECT), but you can use it in ORDER BY. And WHERE cannot filter on aggregates like COUNT(*); that is what HAVING is for (Lesson 216). A classic interview question.
How to say it in an interview
"SELECT chooses columns, FROM the table, WHERE filters rows, ORDER BY sorts and LIMIT restricts the count; without ORDER BY the order is not guaranteed. WHERE supports comparisons, AND and OR with brackets, IN, BETWEEN, LIKE and IS NULL. Logically the database runs FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT, which is why a SELECT alias works in ORDER BY but not in WHERE. I also remember that comparisons with NULL are unknown, so not-equal filters silently drop NULL rows."