The five clauses that make up almost every query
Every SQL query you'll write in a coding interview is some combination of these five clauses, always in the same logical order:
SELECT column1, column2 -- which columns to return
FROM table_name -- which table to read
WHERE condition -- which rows to keep
ORDER BY column1 [ASC | DESC] -- how to sort the result
LIMIT n; -- how many rows to returnWHERE is evaluated before SELECT conceptually — the engine decides which rows survive first, then decides which columns of those rows to show you. This matters the moment you try to filter on a column you didn't select: it's completely legal, because WHERE never cared what you put in SELECT in the first place.
DISTINCT sits right after SELECT and removes duplicate rows from the result — not duplicate values in one column while keeping others, a common misconception. SELECT DISTINCT city, country FROM addresses removes rows that are duplicates across both columns together, not city duplicates alone.
Combining conditions: AND, OR, NOT, IN, BETWEEN
AND/ORcombine boolean conditions, withANDbinding tighter thanOR—WHERE a AND b OR cmeans(a AND b) OR c, nota AND (b OR c). Parenthesize when it matters; don't rely on remembering precedence rules under interview pressure.IN (v1, v2, v3)is shorthand forcol = v1 OR col = v2 OR col = v3— cleaner to read and, in practice, often better-optimized by the query planner than a long OR chain.BETWEEN a AND bis inclusive on both ends (col >= a AND col <= b) — a very common assumption bug is treating it as a half-open range.
The single most important gotcha on this page: NULL is not a value
NULL means "unknown" or "absent," not "empty string" or "zero." That single fact has a consequence that surprises almost everyone the first time they hit it:
SELECT * FROM users WHERE middle_name = NULL; -- returns ZERO rows, always
SELECT * FROM users WHERE middle_name IS NULL; -- this is what you meant= NULL doesn't error — it silently evaluates to UNKNOWN (SQL's three-valued logic: TRUE, FALSE, UNKNOWN), and a WHERE clause only keeps rows where the condition is TRUE. UNKNOWN rows are filtered out exactly like FALSE ones, with no warning. The only correct way to test for NULL is IS NULL / IS NOT NULL. This exact trap reappears in a much more dangerous form with NOT IN against a subquery — that's covered in depth in the Set Operations & Anti-Joins subtopic, but the root cause is this same three-valued logic, so internalizing it here pays off twice.
A related, smaller gotcha: NULL values sort as if they were the smallest (or, on some engines, largest) possible value — ORDER BY puts them first or last depending on the engine's default, and most engines let you override this explicitly with ORDER BY col NULLS LAST / NULLS FIRST (PostgreSQL, Oracle) or an equivalent CASE-based trick (MySQL, which lacks the syntax).
Why interviewers care
This subtopic almost never is the interview question — it's the baseline fluency the interviewer assumes so they can spend the interview watching you reason about joins, aggregation, or a tricky edge case instead of watching you recall clause order. Where it does become the question: an interviewer deliberately seeds a nullable column into the schema and watches whether you reach for = NULL or IS NULL without being told which columns are nullable. Getting this wrong on a warmup question is a worse signal than getting a hard question partially wrong, precisely because it's supposed to be automatic.
Pitfalls and interview gotchas
= NULLinstead ofIS NULL. Covered above — the single most common silent bug in this entire subtopic.- Assuming
SELECT DISTINCT col1, col2dedupes oncol1alone. It dedupes on the whole row of selected columns. - Forgetting
ORDER BYhas no guaranteed default. Without an explicitORDER BY, SQL makes no promise whatsoever about row order — a query that "happens" to return rows in insertion order today can return them in a different order tomorrow after an index is added or a query plan changes. Never rely on implicit ordering, in an interview or in production. LIMITwithoutORDER BY. "Give me the top 5" is meaningless without a defined sort — always pairLIMITwithORDER BYunless you genuinely don't care which rows you get.
Where this goes next
String & Date Functions (next) builds directly on WHERE/CASE to reshape values once simple comparison isn't enough — pattern matching text, extracting parts of a date, or bucketing a continuous value into a handful of labels. After that, Joins & Multi-Table Queries is where this single-table fluency gets combined across tables, which is where the majority of realistic interview questions actually live.
Further Resources (Optional)
- Select Star SQL — a free, interactive book that teaches SQL from first principles (Ch. 1-4 cover this subtopic directly)Reference40m
- Mode SQL Tutorial — Introduction to SQL (SELECT, FROM, WHERE, ORDER BY, LIMIT)Reference20m
- PostgreSQL Docs — Comparison Functions and Operators (read the note on NULL and IS DISTINCT FROM carefully)Reference15m
- freeCodeCamp — SQL Tutorial for Beginners (watch the first ~30 minutes for SELECT/WHERE/ORDER BY if you're new to SQL entirely; skip if not)Video30m
Practice Problems
Interview relevancy:!!!Critical — must-have!!Important — highly recommended!Good to have~Niche — rarely asked
- Recyclable and Low Fat ProductsEasy!!1/510m
- Find Customer RefereeEasy!!1/510m
- Big CountriesEasy!!1/510m
- Not Boring MoviesEasy!!1/512m
Optional Practice (Extra Reps)
For once you've cleared the main set above and want more reps on this pattern. These don't count toward the roadmap's progress stats — solve them purely for your own benefit.
- Invalid TweetsEasy!1/510m
- Article Views IEasy!1/510m