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.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.