Sub topic 4
Removing Duplicate Rows with `DISTINCT`
Assume you know the basics of relational tables, columns, and SELECT ... FROM ..., and that you can run SQL in PostgreSQL, MySQL, SQLite, or another SQL client.
A query can return repeated values even when the table contains many different rows. DISTINCT removes duplicate result rows. It compares the values in every selected expression, then keeps one row for each unique combination.
Selecting Unique Values
Suppose employees contains many employees in the same department:
SELECT DISTINCT department
FROM employees;This returns each department once. If 20 employees work in Sales, Sales appears only one time.
DISTINCT applies to the complete select list. If you select two columns, the pair must be unique:
SELECT DISTINCT department, job_title
FROM employees;This keeps one row for each different department and job-title combination. The query may still return several rows for Sales because Sales can have several job titles.
| Query | What counts as a duplicate? |
|---|---|
| `SELECT DISTINCT department` | The department value |
| `SELECT DISTINCT department, job_title` | The department and job-title pair |
| `SELECT DISTINCT *` | Every column in the selected row |
DISTINCT does not choose a particular employee when several rows have the same selected values. It only removes duplicate rows from the result. If you need one specific row, such as the newest employee in each department, you need a different technique involving ordering and often window functions.
`DISTINCT` with Expressions
The database compares the values after it evaluates expressions and aliases do not change the comparison:
SELECT DISTINCT
department,
salary * 12 AS annual_salary
FROM employees;Two employees with the same department and the same annual salary produce one result row. Employees with different salaries produce different rows, even if their departments match.
You can also use DISTINCT with filtering:
SELECT DISTINCT department
FROM employees
WHERE salary >= 50000;The database first considers employees whose salary is at least 50,000, then removes repeated department values from that filtered result.
Common Errors and Trade-offs
DISTINCT can hide a problem in a join or filter. If a query unexpectedly repeats rows, first ask why the source produces multiple matches. Adding DISTINCT may make the output look correct while concealing a missing join condition or an incorrect relationship.
It can also require extra work. The database may sort or hash the selected values to find duplicates. For a small result this is usually unimportant, but for a large result, selecting only the columns you need can reduce the work.
NULL values are treated as equal for duplicate elimination. Therefore, if several result rows contain NULL in the same selected position, DISTINCT keeps one representative of those rows.
> callout note > DISTINCT removes duplicate result rows. It does not remove duplicate records from the table, and it does not identify which underlying record should remain.
Exercise
What rows can this query return if the table has Sales employees with job titles Manager and Associate, including several employees in each role?
Show answer
It returns at most two rows: (Sales, Manager) and (Sales, Associate). Repeated employees with the same department and job title collapse into one result row.