The DISTINCT
keyword in SQL is used to eliminate duplicate rows from the result set of a query. It returns unique values for specified columns or expressions.
Syntax
The DISTINCT
keyword is typically used in conjunction with the SELECT
statement.
SELECT DISTINCT column1, column2, ...
FROM table_name;
column1
, column2
, ...: The columns for which you want to retrieve unique values.
Example
Consider a table products
with a column category
containing the categories of various products.
CREATE TABLE products (
product_id INT,
product_name VARCHAR(100),
category VARCHAR(50)
);
INSERT INTO products (product_id, product_name, category)
VALUES
(1, 'T-shirt', 'Apparel'),
(2, 'Jeans', 'Apparel'),
(3, 'Sneakers', 'Footwear'),
(4, 'Sunglasses', 'Accessories'),
(5, 'T-shirt', 'Apparel'); -- Duplicate entry
To retrieve the distinct categories of products:
SELECT DISTINCT category
FROM products;
Output
| category |
|-------------|
| Apparel |
| Footwear |
| Accessories |
Usage
The DISTINCT
keyword is commonly used for various purposes, including:
- Removing duplicate rows from query results to focus on unique values.
- Generating unique lists of values for reports or user interfaces.
- Ensuring data integrity by identifying duplicate entries in tables.
Handling Variations in Syntax
The DISTINCT
keyword is supported across all major SQL database systems and is generally used in the same way. However, some systems may have minor variations in syntax or behavior.
The DISTINCT
keyword in SQL is a valuable tool for identifying unique values within a dataset and eliminating duplicate rows from query results. Whether you're generating reports, analyzing data, or ensuring data integrity, understanding how to use DISTINCT
effectively is essential for SQL programming and data manipulation tasks.