What does DISTINCT do?
DISTINCT in SQL is used to select only unique values from a dataset, removing repeated rows.
A simple usage example:
sql
SELECT DISTINCT DepartmentID
FROM Employees;- Pulls every unique value of
DepartmentID. - If a department shows up for several employees, it appears only once.
An example with several columns:
sql
SELECT DISTINCT Name, DepartmentID
FROM Employees;- Uniqueness is based on the combination of the
NameandDepartmentIDcolumns. - Repeated pairs get excluded from the result.
Notes:
- It applies only to the selected columns, not the whole table.
- Useful for removing duplicates from query results.
Put simply,
DISTINCTensures that every value or combination of values appears only once in the result.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.