In SQL, The DISTINCT keyword is used to eliminate duplicate records from the result set of a SELECT query.
It ensures that unique values are retrieved for a specified column or combination of columns.
SELECT DISTINCT column1, column2, FROM tableName;
column1, column2: The columns you want to retrieve from the table.
tableName: The name of the table from which you want to retrieve data.
Eliminating Duplicate Records: DISTINCT keyword ensures there are no duplicate records or column values retrieved from the table.
Improving Data Quality: It eliminates duplicate records and Improves quality by finding and analyzing unique values from the table.
Consider a scenario where you want to retrieve a list of unique `category` names from the `products` table:
SELECT DISTINCT category FROM products;
The above query will return only the unique `category` values from the products table, eliminating all the duplicate entries from the table.
We can use the DISTINCT keyword with multiple columns to retrieve unique combinations of values.
SELECT DISTINCT column1, column2 FROM tableName;
For example, to retrieve unique combinations of category and product_price from the products table.
SELECT DISTINCT category, product_price FROM products;
The above query will return only the unique combinations of category and product_price from the `products` table.
A DISTINCT keyword is often used with aggregate functions to get unique values along with some aggregate calculation.
SELECT department, COUNT(DISTINCT employee_id) AS num_employees FROM employees GROUP BY department;
In the below example, It will filter out the unique values of product_name and it will sort the product_name in ASCENDING order.
SELECT DISTINCT product_name FROM products ORDER BY product_name;
In the below example, It will filter out all unique values from the product_launched_date column from the products table.
SELECT DISTINCT product_launched_date FROM products;