DISTINCT
Removes duplicate rows from query results.
SELECT DISTINCT column1, column2 FROM table_name;This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Removes duplicate rows from query results.
The DISTINCT keyword is a fundamental SQL operator used to eliminate duplicate rows from a result set. It operates by evaluating the combined values of all columns specified in the SELECT statement; if two rows share identical values across every selected column, they are treated as a single record in the output. Technically, database engines implement this by performing a sort operation or building a hash table of the results, which implies a performance overhead compared to a standard SELECT. This overhead scales with the volume of data and the complexity of the data types involved. A critical edge case involves NULL values: in SQL standards, NULL is treated as a unique value for the purpose of the DISTINCT operation, meaning multiple NULLs will be collapsed into a single NULL entry in the result. When used with aggregate functions like COUNT(DISTINCT column), it ignores NULLs while counting only unique non-null occurrences.
Quick reference
Syntax
SELECT DISTINCT column1, column2 FROM table_name;
Inputs
Parameters
See it in practice
Examples
Filtering Unique Column Values
SELECT DISTINCT category FROM inventory_items;Electronics Furniture Apparel
Retrieves the unique labels from the category column, collapsing all duplicate entries into single rows.
Uniqueness Across Multiple Columns
SELECT DISTINCT city, state FROM branch_offices ORDER BY state;Austin, TX Dallas, TX Miami, FL
Returns unique combinations of city and state. If two branches exist in Austin, TX, only one row is returned.
Aggregating Distinct Values
SELECT COUNT(DISTINCT visitor_id) FROM page_views;4502
Calculates the total number of unique non-null visitor IDs in the table, ignoring repetitive visits from the same user.
Debug faster
Common Errors
LogicError
Cause: Attempting to apply DISTINCT to only one column in a multi-column SELECT by placing it incorrectly.
Fix: DISTINCT always applies to the entire row returned. To isolate uniqueness to one column while selecting others, use GROUP BY or a Window Function.
SELECT id, DISTINCT name FROM users;PerformanceError
Cause: Using DISTINCT on columns that are already guaranteed to be unique, such as Primary Keys or columns with UNIQUE constraints.
Fix: Remove the DISTINCT keyword to prevent the database engine from performing an unnecessary sort or hash pass on the data.
SELECT DISTINCT user_id FROM users WHERE user_id = 101;Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Removes duplicate rows from query results.
columns: Column(s) used to determine uniqueness.
LogicError: DISTINCT always applies to the entire row returned. To isolate uniqueness to one column while selecting others, use GROUP BY or a Window Function. PerformanceError: Remove the DISTINCT keyword to prevent the database engine from performing an unnecessary sort or hash pass on the data.