Why is my SQL query not returning the result I expected?
One of the most confusing situations in SQL is when a query is technically correct but returns more rows than expected. This is especially common after joining one or more tables with JOIN.
In most cases, SQL Server is not doing anything wrong. The result is produced by the relationship between the tables and by the conditions used in the JOIN.
Why do rows multiply after a JOIN?
A JOIN produces a row for every matching combination between the participating tables. If one row in the first table matches several rows in the second table, the first row will appear several times in the result set.
A simple example
Assume we have the following tables:
customers ---------------- id | name 1 | John 2 | Michael orders ------------------------ id | customer_id | total 1 | 1 | 500 2 | 1 | 750 3 | 2 | 300
Now run this query:
SELECT c.id, c.name, o.total FROM customers c INNER JOIN orders o ON o.customer_id = c.id;
The result is:
id | name | total ---------------------- 1 | John | 500 1 | John | 750 2 | Michael | 300
John is not a duplicate database record. He appears twice because he has two matching orders.
Important: Repeated parent rows caused by a JOIN are not necessarily the same as true duplicate records stored in the database.
1. Ignoring a one-to-many relationship
The most common reason is a one-to-many relationship. For example:
- One customer can have many orders.
- One invoice can have many invoice lines.
- One employee can have many leave records.
- One product can have many price history records.
You may expect one row from the parent table, but if three rows in the child table match that parent, SQL will naturally return three rows.
2. An incomplete JOIN condition
Another frequent cause is joining tables with an incomplete condition.
For example, if two tables are related by both person_id and year, joining only by person_id can create incorrect matches.
-- Incomplete JOIN SELECT * FROM Salary s INNER JOIN Deduction d ON d.person_id = s.person_id;
If the relationship also depends on the year, use:
SELECT * FROM Salary s INNER JOIN Deduction d ON d.person_id = s.person_id AND d.year = s.year;
Missing part of a JOIN condition may create many unexpected matches and dramatically increase the number of rows.
3. Joining multiple one-to-many tables
The problem becomes even more noticeable when several one-to-many tables are joined in the same query.
Suppose a customer has:
- 3 orders,
- 2 addresses.
If both tables are joined directly to the customer table, the result can contain 3 × 2 = 6 rows.
This behavior is often called JOIN multiplication.
4. Is DISTINCT the solution?
A common first attempt is to use DISTINCT:
SELECT DISTINCT c.id, c.name FROM customers c INNER JOIN orders o ON o.customer_id = c.id;
This can return each customer once. However, DISTINCT is not always the real solution. It may only hide the visible symptom of an incorrect JOIN.
Warning: If the JOIN logic is incorrect, adding DISTINCT may hide a data relationship problem instead of solving it.
5. Use EXISTS when you only need to know whether a related row exists
If you do not need columns from the related table and only want to check whether a related record exists, EXISTS is often a better choice.
SELECT c.id, c.name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.id );
This returns customers that have at least one order without producing one customer row for every matching order.
6. Aggregate the detail table before joining
If you need totals, counts, or other summary values from the child table, aggregate it before performing the JOIN.
SELECT c.id, c.name, o.order_count, o.total_amount FROM customers c LEFT JOIN ( SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS total_amount FROM orders GROUP BY customer_id ) o ON o.customer_id = c.id;
Because the subquery returns only one row per customer, the parent rows no longer multiply.
7. Use ROW_NUMBER when you need only one related record
Sometimes you do not want all related rows. For example, you may want only the latest order for each customer.
In that case, ROW_NUMBER() is a very useful approach:
WITH LatestOrder AS ( SELECT id, customer_id, total, order_date, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY order_date DESC, id DESC ) AS rn FROM orders ) SELECT c.id, c.name, o.total, o.order_date FROM customers c LEFT JOIN LatestOrder o ON o.customer_id = c.id AND o.rn = 1;
This ensures that only the latest matching order for each customer participates in the final JOIN.
How can you find where the problem starts?
When a large query returns too many rows, avoid changing the entire query at once. Instead, add JOINs one by one and watch how the row count changes.
- Run the main table by itself.
- Add the first JOIN and check the row count.
- Add the next JOIN.
- Identify the JOIN where the row count increases unexpectedly.
- Check how many rows exist for each JOIN key in the related table.
For example:
SELECT customer_id, COUNT(*) AS row_count FROM orders GROUP BY customer_id HAVING COUNT(*) > 1;
This query shows customers with multiple orders and helps explain why a JOIN produces multiple rows.
Which technique should you use?
- JOIN: When you actually need columns from the related table.
- EXISTS: When you only need to test whether a related row exists.
- GROUP BY: When you need totals, counts, or other aggregated values.
- ROW_NUMBER: When you need one specific row per group, such as the latest record.
- DISTINCT: When you truly need to remove identical result rows.
Conclusion
Rows multiplying after a JOIN is often not an SQL error. It is the natural result of the relationship between the tables.
The key question is what level of data you expect from the query. If you need one row per customer, directly joining a child table containing several records per customer may not be the correct approach.
Instead of immediately adding DISTINCT, ask yourself:
“How many rows in the joined table match this record?”
In many SQL queries, the answer to that question reveals the real reason behind unexpected duplicate-looking rows.
0 Comments