Sub topic 1
Why Duplicate Rows Appear in Query Results
A SQL result can contain repeated-looking rows even when the database is working correctly. The repetition usually comes from the table data, a join that matches several rows, or selecting fewer columns than distinguish the underlying rows.
Prerequisites
You should know basic SELECT and FROM syntax, WHERE filters, ORDER BY, and the meaning of tables, rows, and columns.
Duplicate Data and Duplicate Results
A table may contain multiple rows with the same value in one column. For example, many customers can live in the same country:
SELECT customer_id, name, country
FROM customers
ORDER BY country, customer_id;The country value may appear many times, but the rows are not duplicates because their customer_id or name values differ.
A result can also look duplicated when the query selects only the repeated column:
SELECT country
FROM customers
ORDER BY country;This query returns one row for every customer. It does not automatically combine equal country values. SQL normally preserves the number of matching rows.
| customer_id | name | country |
|---|---|---|
| 101 | Ana | Canada |
| 102 | Ben | Canada |
| 103 | Chen | Japan |
The first query returns three distinct customer rows. The second query displays Canada twice because two source rows match the selected value.
Joins Can Multiply Rows
Joins commonly create repeated-looking results. Suppose one customer has several orders:
SELECT c.customer_id, c.name, o.order_id
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE c.customer_id = 101
ORDER BY o.order_id;If customer 101 has three orders, the customer columns appear three times. Each result row represents a different customer-order relationship. The database is showing all matching combinations, not repeating one row by mistake.
This is called a one-to-many relationship: one row in the first table matches many rows in the second table.
flowchart TD
A[One customer] --> B[Order 1]
A --> C[Order 2]
A --> D[Order 3]Projection Can Hide Differences
A query's selected columns are sometimes called its projection. Projection means choosing which columns appear in the result. If the chosen columns do not include the values that differ between source rows, several different rows can look identical.
For example:
SELECT c.name
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE c.customer_id = 101;This returns the customer's name once for each matching order. The result may show:
Ana
Ana
AnaThose rows still correspond to separate orders, even though order_id is not displayed.
Find the Source of Repetition
When a result looks duplicated, inspect the query in stages:
- Run the query against one table.
- Add each join one at a time.
- Select identifying columns such as primary keys.
- Check whether a one-to-many or many-to-many relationship is producing multiple matches.
- Decide whether you need every relationship or only unique displayed values.
A primary key is a column, or group of columns, that identifies one row. Including it often reveals why two rows that look alike are actually different.
The next step is learning how SELECT DISTINCT can collapse identical result values when that is the intended meaning.
Exercise
Why might this query return the same customer name several times: SELECT c.name FROM customers AS c JOIN orders AS o ON o.customerid = c.customerid;?
Show answer
One customer can match several order rows, so the join returns one result row for each matching order. The selected columns hide the order differences.