📅  最后修改于: 2023-12-03 14:47:35.368000             🧑  作者: Mango
In SQL, the SELECT UNIQUE
statement is used to retrieve distinct or unique values from a table. It is commonly used to eliminate duplicate records and fetch only unique values. This query returns a result set consisting of one occurrence of each unique value in the specified column(s).
The syntax for using SELECT UNIQUE
is as follows:
SELECT UNIQUE column_name(s)
FROM table_name;
Consider a table named employees
with the following data:
| emp_id | emp_name | department | |--------|-----------|------------| | 1 | John Doe | HR | | 2 | Jane Smith| IT | | 3 | John Doe | Sales | | 4 | Adam Hill | IT | | 5 | Jane Smith| HR |
If we want to fetch only the unique employee names from the table, we can use the SELECT UNIQUE
statement:
SELECT UNIQUE emp_name
FROM employees;
The result set will contain only the distinct employee names:
| emp_name | |-----------| | John Doe | | Jane Smith| | Adam Hill |
SELECT UNIQUE
statement is equivalent to the SELECT DISTINCT
statement. Both can be used interchangeably to retrieve unique values.SELECT UNIQUE
statement to fetch unique combinations of those columns' values.Now you can effectively utilize the SELECT UNIQUE
statement to retrieve distinct values from your SQL tables.