5 SQL Interview Questions That Trip Up Beginners
If you're prepping for a tech interview, SQL usually feels "easy" until you're actually in the room. Here are 5 questions that look simple on paper but catch people off guard — plus quick explanations so you don't get caught out.
1. What's the difference between WHERE and HAVING?
Most people say "WHERE filters rows, HAVING filters groups" and stop there — which is correct, but interviewers often follow up with: why can't you use an aggregate function like COUNT() inside WHERE?
The answer: WHERE is applied before GROUP BY, while the aggregate values don't exist yet at that stage. HAVING runs after grouping, once the aggregates are calculated. That's why:
SELECT Country, COUNT(CustomerID)
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
works, but putting COUNT(CustomerID) > 5 in a WHERE clause would throw an error.
2. Can a table have more than one primary key?
Trick question. A table can have only one primary key — but that primary key can be made up of multiple columns. This is called a composite key. So the answer is "one primary key, which may be composite," not "yes."
3. What happens if you JOIN two tables with no matching rows?
With an INNER JOIN, you get zero rows back — nothing at all, even if both tables have data. Beginners often expect some output. If the interviewer wants unmatched rows preserved, that's when you'd reach for a LEFT, RIGHT, or FULL OUTER JOIN instead.
4. Why can't you compare a column to NULL using =?
WHERE Address = NULL will never return any rows — not because there's no NULL data, but because NULL isn't a value you can compare with = or <>. SQL treats NULL as "unknown," and unknown compared to anything is also unknown (never true). You have to use IS NULL or IS NOT NULL instead.
5. What's the difference between DELETE and TRUNCATE?
Both remove data, but:
- DELETE is a DML command, can use a WHERE clause to remove specific rows, and can be rolled back.
- TRUNCATE is a DDL command, removes all rows at once, resets identity/auto-increment counters, and in most databases cannot be rolled back once committed.
If an interviewer asks "which is faster," the answer is TRUNCATE — since it doesn't log individual row deletions the way DELETE does.
These are the kind of gotchas that separate "I know SQL" from "I can survive a live interview." If you want the full breakdown — ER diagrams, normalization, transactions, ACID, joins, and the complete SQL syntax reference in one place — I put together a DBMS & SQL interview prep PDF with all of it.
What's a SQL question that caught you off guard in an interview? Drop it in the comments — always curious what people are actually being asked out there.
Top comments (0)