SQL Select Distinct
The `SELECT DISTINCT` statement in SQL is used to return only distinct (different) values from a column. It eliminates duplicate values from the result set.
Syntax
The syntax for using `SELECT DISTINCT` is as follows:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example
Suppose you have a table named Employees
with a column named Department
. To get a list of distinct departments from this table, you would use the following query:
SELECT DISTINCT Department
FROM Employees;
This query retrieves unique department names from the Employees
table, filtering out any duplicate entries.
Notes
- Multiple Columns: You can use `DISTINCT` with multiple columns. In this case, it returns unique combinations of values across the specified columns.
- Performance: Using `DISTINCT` may affect performance, especially with large datasets. Ensure you only use it when necessary.
Understanding how to use `SELECT DISTINCT` can help you manage and analyze data more effectively by removing duplicate entries from your query results.