Sub topic 10
Removing Duplicate Rows with DISTINCT
Assume you know how tables contain rows and columns, and you can run SQL in an Oracle session. This chapter uses the HR sample schema, especially the employees table.
What DISTINCT does
A query can return repeated values when several rows share the same value. The DISTINCT keyword removes duplicate result rows.
For example, many employees can work in the same department:
SELECT department_id
FROM employees
ORDER BY department_id;This returns one row for every employee, so department numbers may appear repeatedly. Add DISTINCT to return each department number once:
SELECT DISTINCT department_id
FROM employees
ORDER BY department_id;DISTINCT applies to the complete set of expressions in the SELECT list. Oracle compares the selected values together, not each column independently.
SELECT DISTINCT department_id, job_id
FROM employees
ORDER BY department_id, job_id;This returns each unique department and job combination. The same department can still appear several times if its employees have different jobs.
| Query | What counts as a duplicate? |
|---|---|
| `SELECT DISTINCT department_id` | The same department number |
| `SELECT DISTINCT department_id, job_id` | The same department and job combination |
| `SELECT DISTINCT last_name, salary` | The same last name and salary |
How Oracle processes DISTINCT
Oracle logically evaluates a query in stages. The FROM clause supplies rows, and WHERE filters them. The SELECT list produces result values. DISTINCT then removes repeated result rows. ORDER BY sorts the remaining rows.
This order explains why you can sort a distinct result:
SELECT DISTINCT department_id
FROM employees
WHERE department_id IS NOT NULL
ORDER BY department_id;The ORDER BY expression normally must be available in the distinct result. For clarity, select the column you want to sort by. In more complex queries, an ORDER BY expression that is not part of the distinct select list can cause Oracle errors or produce a result that does not match your intent.
DISTINCT and NULL
Oracle treats all NULL values as one duplicate value for DISTINCT. Therefore, if several employees have no commission percentage, a distinct query returns one NULL row for that value.
SELECT DISTINCT commission_pct
FROM employees
ORDER BY commission_pct;You can replace the NULL value before duplicate removal with NVL:
SELECT DISTINCT NVL(commission_pct, 0) AS commission_pct
FROM employees
ORDER BY commission_pct;Here, missing commissions become 0, so they are grouped with any actual zero values. That may be useful for display, but it changes the meaning of the data. Use it only when treating missing and zero as equivalent is correct.
Common errors and trade-offs
Do not add DISTINCT merely to hide duplicates caused by an incorrect join or an unclear query. First determine why multiple rows exist. A join may correctly return several matching rows, and DISTINCT could hide that relationship.
DISTINCT can also require sorting or other work, especially when the result contains many rows or large expressions. Select only the columns you need, and use DISTINCT when uniqueness is part of the requested result.
DISTINCT is different from grouping. DISTINCT removes repeated result rows. GROUP BY creates groups that can be used with aggregate functions such as COUNT and AVG.
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
ORDER BY department_id;Use this form when you need a count, not merely a list of department numbers.
Exercise
What does SELECT DISTINCT department_id, job_id FROM employees return?
Show answer
It returns one row for each unique combination of departmentid and jobid. It does not return one row per department if a department has multiple job IDs.