SQL Tutorial
The DISTINCT
keyword in SQL is used to return unique values in the output. It eliminates all the duplicate records from the result set.
The basic syntax of DISTINCT
is:
SELECT DISTINCT column_name FROM table_name;
Example:
Suppose we have a table Employees
with the following data:
ID | Name | Department |
---|---|---|
1 | John Doe | Sales |
2 | Jane Doe | Marketing |
3 | Mike Smith | Sales |
4 | Alice Johnson | Sales |
5 | Bob Johnson | Marketing |
If we want to find out all the unique departments in the table, we can use the DISTINCT
keyword:
SELECT DISTINCT Department FROM Employees;
This query will return:
Sales Marketing
You can also use the DISTINCT
keyword with multiple columns. The query will then return unique combinations of those columns.
For example:
SELECT DISTINCT Name, Department FROM Employees;
This query will return all the unique Name
and Department
combinations from the Employees
table.
It's important to note that DISTINCT
applies to the combination of all columns listed after the DISTINCT
keyword, not each column individually.
Finally, remember that the DISTINCT
keyword can impact performance, as the database needs to sort and compare the result set to remove duplicates. Use it wisely.
Using DISTINCT to Identify Duplicate Records:
SELECT DISTINCT ColumnName FROM TableName;
SQL DELETE Duplicate Records with DISTINCT:
DELETE FROM TableName WHERE ColumnName IN ( SELECT DISTINCT ColumnName FROM TableName HAVING COUNT(ColumnName) > 1 );
Removing Duplicate Rows with SQL DISTINCT:
SELECT DISTINCT * FROM TableName;
Finding and Deleting Duplicates in SQL:
-- Identify duplicates SELECT ColumnName, COUNT(*) FROM TableName GROUP BY ColumnName HAVING COUNT(*) > 1; -- Delete duplicates DELETE FROM TableName WHERE ColumnName IN ( SELECT ColumnName FROM TableName GROUP BY ColumnName HAVING COUNT(*) > 1 );
DISTINCT vs GROUP BY for Identifying Duplicates:
SELECT ColumnName, COUNT(*) FROM TableName GROUP BY ColumnName HAVING COUNT(*) > 1;
SQL DISTINCT with Multiple Columns:
SELECT DISTINCT Column1, Column2 FROM TableName;
Avoiding Pitfalls When Using DISTINCT:
-- Be cautious with DISTINCT SELECT DISTINCT Column1, Column2, Column3 FROM TableName;
Identifying and Deleting Duplicate Data in SQL:
-- Identify duplicates SELECT Column1, Column2, COUNT(*) FROM TableName GROUP BY Column1, Column2 HAVING COUNT(*) > 1; -- Delete duplicates DELETE FROM TableName WHERE (Column1, Column2) IN ( SELECT Column1, Column2 FROM TableName GROUP BY Column1, Column2 HAVING COUNT(*) > 1 );