Select Unique in SQL
SELECT UNIQUE in SQL
The UNIQUE keyword in a SELECT statement is used to return distinct rows — that is, it eliminates duplicate rows from the result set.
Overview
SELECT UNIQUEis functionally equivalent toSELECT DISTINCT.It is mainly supported in Oracle and some other databases.
It ensures that the query returns only unique rows.
Syntax:
SELECT UNIQUE column1, column2, ...FROM table_nameWHERE condition;Example:
Suppose you have a students table with multiple rows having the same city values.
SELECT UNIQUE cityFROM students;This query will return each city only once, no duplicates.
Equivalent using DISTINCT (more common):
SELECT DISTINCT cityFROM students;Notes:
Both
UNIQUEandDISTINCTremove duplicate rows.DISTINCTis supported by nearly all SQL databases;UNIQUEis less common.Use either to get unique values in a column or combination of columns.
If you want examples using multiple columns or complex queries with UNIQUE or DISTINCT, just ask!