SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement in SQL is used to return unique values from a specified column or combination of columns in a table. It eliminates duplicate records and displays only unique values.
Here is the basic syntax for the SELECT DISTINCT statement:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example Table
Let's assume we have the following table named Employees:
| ID | Name | Department |
|---|---|---|
| 1 | John | Sales |
| 2 | Jane | Marketing |
| 3 | John | Sales |
| 4 | Alice | HR |
| 5 | Bob | Sales |
| 6 | Jane | Marketing |
Query to Get Unique Departments
To get a list of unique departments from the Employees table, you can use the following query:
SELECT DISTINCT Department
FROM Employees;
Output:
| Department |
|---|
| Sales |
| Marketing |
| HR |
Query to Get Unique Names
To get a list of unique names from the Employees table, you can use the following query:
SELECT DISTINCT Name
FROM Employees;
Output:
| Name |
|---|
| John |
| Jane |
| Alice |
| Bob |
Query to Get Unique Combinations of Name and Department
To get a list of unique combinations of names and departments, you can use the following query:
SELECT DISTINCT Name, Department
FROM Employees;
Output:
| Name | Department |
|---|---|
| John | Sales |
| Jane | Marketing |
| Alice | HR |
| Bob | Sales |
In this example, the SELECT DISTINCT statement ensures that only unique combinations of Name and Department are returned.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
