SELECT, WHERE and ORDER BY
Reading a table: choosing columns, filtering rows and sorting, with the mistakes that return wrong rows silently.
Every SQL screening starts with reading one table. The syntax is small; the mistakes are in the details of WHERE.
The shape of a query
SELECT name, wpm
FROM results
WHERE skill = 'typing' AND wpm >= 35
ORDER BY wpm DESC
LIMIT 10;
Clauses go in this order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. Putting LIMIT before ORDER BY is a syntax error in most databases.
Filtering
- Text matches are exact unless you use LIKE:
name LIKE 'A%'matches names starting with A;'%son'matches endings. - NULL is not a value.
WHERE city = NULLreturns nothing; useIS NULL. IN ('paid', 'shipped')is clearer than a chain of ORs.BETWEEN 10 AND 20includes both ends.- Dates: compare against a date literal in the database's format, usually
'2026-10-24'.
Sorting and limiting
ORDER BY defaults to ascending. Add DESC for highest first. LIMIT (or TOP in SQL Server) cuts the result after sorting, which is how "the five most recent" works.
A habit that saves tables
Before any UPDATE or DELETE, run a SELECT with the same WHERE clause and check the row count. UPDATE users SET active = 0 without a WHERE deactivates everyone, and the database will not ask whether you meant it.
The SQL test covers LIKE, NULL, ORDER BY with LIMIT, and the missing-WHERE trap. Each answer explains the rule.
More in Accounts and data