SQL Server Interview Question #52

What is DISTINCT?

SELECT, Filtering, Sorting & Built-in Functions Mid-Level Intermediate

Detailed Explanation

DISTINCT removes duplicate rows from the selected result based on the complete set of expressions in the SELECT list.

It is useful when uniqueness is genuinely required, but it should not be added merely to hide duplicate rows caused by an incorrect join. DISTINCT can require additional sorting or hashing work, so the underlying query logic should first be correct.

Code Example

SELECT DISTINCT CategoryId
FROM dbo.Products
WHERE IsActive = 1;