Quick answer: The most common SQL mistakes are comparing with = NULL instead of IS NULL, filtering aggregates in WHERE instead of HAVING, selecting columns that are not in GROUP BY, turning a LEFT JOIN into an inner join with a WHERE filter, using NOT IN with NULLs, missing the last day with BETWEEN on dates, and double-counting after joins. Most give wrong answers silently, so test queries on small data you can check by hand.
The worst SQL mistakes are not the ones that throw errors. They are the ones that run, return a neat table and are quietly wrong. In an analyst job, that means a wrong number in a report someone acts on.
Below are ten SQL mistakes we see beginners make again and again. Each has the wrong query, why it is wrong and a fixed query. The examples use a simple schema: customers, orders (order_id, customer_id, order_date, amount, status) and order_items. Where MySQL and PostgreSQL behave differently, it is noted.
1. Comparing with = NULL
-- Wrong: returns no rows, even if some phones are missing
SELECT customer_id, name FROM customers WHERE phone = NULL;
Why it is wrong: NULL means "unknown", so phone = NULL is also unknown, never true. The WHERE clause only keeps rows where the condition is true.
-- Fixed
SELECT customer_id, name FROM customers WHERE phone IS NULL;
Use IS NULL and IS NOT NULL. The same rule applies to <> NULL, which also matches nothing.
2. Filtering an aggregate in WHERE
-- Wrong: error, aggregates are not allowed in WHERE
SELECT customer_id, SUM(amount) AS total
FROM orders
WHERE SUM(amount) > 10000
GROUP BY customer_id;
Why it is wrong: WHERE filters individual rows before grouping, so totals do not exist yet. Filters on aggregated values belong in HAVING, which runs after GROUP BY.
-- Fixed
SELECT customer_id, SUM(amount) AS total
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 10000;
You can still use WHERE for row-level filters, such as WHERE status = 'DELIVERED', in the same query. Our GROUP BY guide explains the order in detail.
3. Selecting columns that are not in GROUP BY
-- Wrong: order_date is neither grouped nor aggregated
SELECT customer_id, order_date, SUM(amount) AS total
FROM orders
GROUP BY customer_id;
Why it is wrong: Each customer has many order dates, so the database cannot know which one to show. PostgreSQL rejects this query. MySQL 8 also rejects it by default (the ONLY_FULL_GROUP_BY mode), but if that mode is turned off, it silently returns an arbitrary date.
-- Fixed: decide which date you actually want
SELECT customer_id, MAX(order_date) AS last_order_date, SUM(amount) AS total
FROM orders
GROUP BY customer_id;
Every selected column must either be in GROUP BY or wrapped in an aggregate.
Interview note: "What is the difference between WHERE and HAVING?" and "Why does this GROUP BY query fail?" are two of the most common fresher SQL questions. Explain the order in which clauses run, not just the rule.
4. Turning a LEFT JOIN into an INNER JOIN
-- Wrong: customers with no 2026 orders disappear
SELECT c.customer_id, c.name, COUNT(o.order_id) AS orders_2026
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-01-01'
GROUP BY c.customer_id, c.name;
Why it is wrong: For customers with no matching orders, the LEFT JOIN fills o.order_date with NULL. The WHERE condition is not true for NULL, so those customers are removed, which is exactly what an inner join does.
-- Fixed: move the filter on the right-hand table into ON
SELECT c.customer_id, c.name, COUNT(o.order_id) AS orders_2026
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.order_date >= '2026-01-01'
GROUP BY c.customer_id, c.name;
Now every customer appears, with 0 for those who did not order. Notice COUNT(o.order_id) rather than COUNT(*): COUNT(*) would count the empty joined row as 1. Our SQL joins explainer covers this with diagrams.
5. Using NOT IN with a subquery that can return NULL
-- Wrong: returns no rows if any order has a NULL customer_id
SELECT customer_id, name
FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);
Why it is wrong: NOT IN checks that the value differs from every item in the list. If the list contains NULL, the comparison with it is unknown, so no row ever passes. This is standard SQL behaviour in every major database.
-- Fixed
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
);
NOT EXISTS handles NULL correctly. A LEFT JOIN ... WHERE o.order_id IS NULL also works.
6. Missing the last day with BETWEEN
-- Wrong if order_date stores a time: misses most of 31 January
SELECT COUNT(*) FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-31';
Why it is wrong: If order_date is a DATETIME or TIMESTAMP, '2026-01-31' means midnight at the start of 31 January. An order at 2:30 pm that day is excluded.
-- Fixed: half-open range
SELECT COUNT(*) FROM orders
WHERE order_date >= '2026-01-01'
AND order_date < '2026-02-01';
This works whether the column holds a date or a date and time, so make it your default.
7. Wrapping an indexed column in a function
-- Slow on large tables: the index on order_date cannot be used
SELECT order_id, amount FROM orders
WHERE YEAR(order_date) = 2026;
Why it is wrong: The result is correct, but the database has to calculate YEAR() for every row, so a normal index on order_date is not used. On millions of rows, that turns a fast lookup into a full table scan.
-- Fixed: compare the raw column with a range
SELECT order_id, amount FROM orders
WHERE order_date >= '2026-01-01'
AND order_date < '2027-01-01';
YEAR() is MySQL; PostgreSQL uses EXTRACT(YEAR FROM order_date), with the same problem and the same fix. See SQL indexes explained for why.
8. Integer division
-- Wrong in PostgreSQL and SQL Server: returns 0
SELECT SUM(CASE WHEN status = 'CANCELLED' THEN 1 ELSE 0 END) / COUNT(*)
AS cancel_rate
FROM orders;
Why it is wrong: In PostgreSQL and SQL Server, dividing an integer by an integer gives an integer, so 0.08 becomes 0. MySQL returns a decimal here, which is why this bug often appears only when a query moves between databases.
-- Fixed: make one side a decimal
SELECT ROUND(100.0 * SUM(CASE WHEN status = 'CANCELLED' THEN 1 ELSE 0 END)
/ COUNT(*), 2) AS cancel_rate_pct
FROM orders;
Multiplying by 100.0 forces decimal arithmetic in every database.
9. Double-counting after a one-to-many join
-- Wrong: each order's amount is added once per item
SELECT o.customer_id, SUM(o.amount) AS total_spent
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
GROUP BY o.customer_id;
Why it is wrong: An order with three items appears in three joined rows, so its amount is summed three times. This is called fan-out, and it inflates totals without any error.
-- Fixed: aggregate each table at its own level first
WITH item_counts AS (
SELECT order_id, COUNT(*) AS items
FROM order_items
GROUP BY order_id
)
SELECT o.customer_id,
SUM(o.amount) AS total_spent,
SUM(ic.items) AS total_items
FROM orders o
JOIN item_counts ic ON ic.order_id = o.order_id
GROUP BY o.customer_id;
Now each order is one row before the final sum. Check row counts before and after every join; a sudden jump is the warning sign.
10. UPDATE or DELETE without a WHERE clause
-- Wrong: cancels every order in the table
UPDATE orders SET status = 'CANCELLED';
Why it is wrong: Without WHERE, the statement applies to every row. There is no undo once it is committed.
-- Fixed: check first, then change inside a transaction
SELECT order_id, status FROM orders WHERE order_id = 1042;
START TRANSACTION; -- BEGIN; in PostgreSQL
UPDATE orders SET status = 'CANCELLED' WHERE order_id = 1042;
-- check the affected row count, then:
COMMIT; -- or ROLLBACK; if it is wrong
Run the SELECT with the same WHERE first to see exactly what will change. MySQL Workbench has a safe-updates setting that blocks updates without a key in WHERE. Read SQL transactions explained for more.
Pro tip: A quick SQL debugging habit: before trusting any result, run the query on a tiny table of five or six rows where you can work out the answer by hand. If the numbers match, scale up.
How to avoid these SQL mistakes
- Test on small data where you know the answer.
- Check row counts after each join and filter.
- Read queries in execution order:
FROM,JOIN,WHERE,GROUP BY,HAVING,SELECT,ORDER BY. - Know your database: date functions, division and
GROUP BYrules differ. - Practise on real projects. Our SQL project ideas give you schemas where these bugs show up naturally.
These mistakes also appear in interviews as "spot the bug" questions, so practise with our SQL interview questions for freshers. SQL is a full phase of our Data Analytics with AI course, where trainers review your queries for exactly these problems.
Frequently asked questions
Why does my SQL query run but return the wrong answer?
The usual causes are NULL handling, a LEFT JOIN filtered in the WHERE clause, rows multiplied by a one-to-many join, or a date range that misses part of a day. Run the query on a few rows where you know the correct answer and compare.
How do I debug a long SQL query?
Break it into parts. Run each CTE or subquery on its own, check the row count after every join, and add columns back one at a time. Most bugs show up as an unexpected jump or drop in the row count.
Is SQL the same in MySQL and PostgreSQL?
The core is the same, but details differ: date functions, integer division, string concatenation and how strictly GROUP BY is enforced. Always check which database a query is written for before copying it. Our MySQL vs PostgreSQL comparison lists the main differences.
Are these SQL mistakes asked in interviews?
Yes. Interviewers often show a query with one of these bugs and ask what is wrong, especially NULL handling, WHERE versus HAVING and LEFT JOIN filtering. Knowing why each one fails is more useful than memorising the fix.
Want a trainer to review your SQL? Book a free demo class and see how CodeBegun teaches SQL with real datasets and live query reviews.
