10 SQL Queries That Run but Give the Wrong Answer
A free PDF for anyone preparing for SQL interview questions. No error message will warn you about these. The query runs and the answer is wrong.
Tested on MySQL 8.4 (default settings), with the real output of every wrong and fixed query
All ten traps use one small database, with a setup script so you can create it and run every query yourself.
Free. No payment details needed.
Interview Pages Editorial
Written by a computer-science educator with a PhD and industry software experience
Get the free PDF by email
Sign-up opens soon. This form isn't active yet, so nothing you type here is sent anywhere.
When it opens, we'll use your email only to send the PDF and occasional Interview Pages updates. Unsubscribe any time. See our privacy policy.
Each trap covers
- The query, and why it looks right
- What it actually returns, on a small table you can check by hand
- The fix, and how to spot the pattern next time
- A one-line "say it aloud" explanation
- A PostgreSQL 16 note where the behaviour differs
What's inside
- NOT IN with a NULL in the subquery
- = NULL instead of IS NULL
- LEFT JOIN, then filtering the right table in WHERE
- COUNT(*) after a LEFT JOIN
- Second-highest salary with LIMIT/OFFSET and a tie
- Filtering rows (WHERE) when you meant to filter groups (HAVING)
- A join that repeats rows and inflates SUM
- BETWEEN on a DATETIME column
- AVG silently skips NULLs
- Case- and accent-insensitive comparison under the default collation