📜  SQL SELECT UNIQUE(1)

📅  最后修改于: 2023-12-03 14:47:35.368000             🧑  作者: Mango

SQL SELECT UNIQUE

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;
Example

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 |

Notes
  • The SELECT UNIQUE statement is equivalent to the SELECT DISTINCT statement. Both can be used interchangeably to retrieve unique values.
  • You can specify multiple columns in the SELECT UNIQUE statement to fetch unique combinations of those columns' values.
  • The order of the unique values in the result set may not necessarily match the order of their occurrence in the table.

Now you can effectively utilize the SELECT UNIQUE statement to retrieve distinct values from your SQL tables.