r/learnSQL • u/thequerylab • Sep 17 '26
If you have an SQL interview soon, don’t ignore these small things!!!! (Part 10)
I’ve noticed something about SQL interviews.
Most people don’t fail because they don’t know SQL.
They fail because they forget tiny things while typing under pressure. It's pressure!!!
Few examples I’ve seen in real interviews:
1. NOT IN vs NOT EXISTS
If the list contains NULL values:
NOT IN→ can return no rowsNOT EXISTS→ checks if a matching row exists
Example:
SELECT 30
WHERE 30 NOT IN (10, 20, NULL);internally SQL does this:
30 <> 10 → TRUE
30 <> 20 → TRUE
30 <> NULL → UNKNOWN
TRUE AND TRUE AND UNKNOWN = UNKNOWNSo if someone asks "find users who don’t have any subscription", NOT IN can give the wrong result when NULL is involved!
2. ROWS vs RANGE
Example:
SELECT
sub_date,
amount,
SUM(amount) OVER (
ORDER BY sub_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM subscription;ROWS works with individual rows, while RANGE works with groups of rows having the same ORDER BY value.
So if multiple orders have the same sub_date, ROWS and RANGE can give different running totals.
3. LEFT JOIN + WHERE can change your result
Example:
SELECT *
FROM users u
LEFT JOIN subscriptions s
ON u.user_id = s.user_id
WHERE s.plan = 'Pro';The WHERE condition removes users who don't have a matching subscription.
So even though you wrote LEFT JOIN, you end up keeping only users who have a matching Pro subscription.
If you want to keep all users, put the condition in the ON clause instead:
LEFT JOIN subscriptions s
ON u.user_id = s.user_id
AND s.plan = 'Pro'4. GROUP BY 1,2
This looks perfectly fine:
SELECT department, job_title, COUNT(*)
FROM employees
GROUP BY 1, 2;But during an interview, especially when you're already under pressure, it can create unnecessary confusion if new column gets added to SELECT
Writing:
GROUP BY department, job_titletakes a little more typing but it's much easier to read, explain and modify later.
These are all very small things and basics, but they come up surprisingly often in interviews.
Curious what others have seen.
What’s a small SQL thing people still mess up in interviews even though they know it?
Always interesting to hear these and your interview experiences.
4
u/dizzysoup Sep 18 '26
I always write out the full group by args, ends up being more work if you need to edit something later on
4
u/Alternative_Cake4074 Sep 17 '26
When connecting table A and table B, there can be null values involved, and some many forget to handle them.
For example:
SELECT e.name, e.dept, s.amount
FROM employees e
LEFT JOIN sales s ON e.empid = s.empid
vs
SELECT e.name, e.dept, IFNULL (s.amount, 0) AS amount
FROM employees e
LEFT JOIN sales s ON e.empid = s.empid
1
u/G_Thorne Sep 17 '26
Interesting, I had no idea you could GROUP BY column index. Is it in the standard or RDBMS specific?
As you say, it does feel like a recipie for disaster and best avoided.
3
u/Bright_Situation1844 Sep 17 '26
Sql interviews are not open book?
2
u/Final_Profile8639 Sep 30 '26
Difference between types of JOINS. Where to put Where and where to put Group by having.
1
u/onthepik Sep 18 '26 edited Sep 18 '26
It is helpful but i dont undestand why not simlify the query. Make it clear and straight rather than short but have to remember tricks.
Just require a solid set operator knowledge (which is not much).
Edit: Dont think anyone could mess with order index cause it is listed in select, except SELECT alias.*
1
u/dragmenta Sep 21 '26
Para que tanto quilombo... Select y from y listo...
1
u/sqlsip Oct 14 '26
Great tips! In the past I’ve gotten tripped up just explaining basic joins 🤦♀️ sometimes I feel like I understand something well day-to-day, but to explain it in words is often difficult 😂