SQL UNIQUE Keyword
The SQL UNIQUE keyword is a operator and it is used to check whether the sub-query has any duplicate values in the result. It returns true if the sub-query has no duplicate values, else returns false.
Syntax
The syntax for using UNIQUE operator is given below:
SELECT table1.column FROM table1 WHERE UNIQUE ( SELECT table2.column FROM table2 WHERE table1.column = table2.column);
Example:
Consider a database tables called Employee and Contact_Info with the following records:
Table 1: Employee table
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
Table 2: Contact_Info table
Phone_Number | EmpID | Address |
---|---|---|
+1-80XXXXX000 | 2 | XXX, Brooklyn, New York, USA |
+33-14XXXXX01 | 3 | XXX, Grenelle, Paris, France |
+31-20XXXXX19 | 4 | XXX, Geuzenveld, Amsterdam, Netherlands |
+86-10XXXXX458 | 6 | XXX, Yizhuangzhen, Beijing, China |
+65-67XXXXX4 | 7 | XXX, Yishun, Singapore |
+81-35XXXXX72 | 8 | XXX, Koto City, Tokyo, Japan |
To find all the employees whose contact information is updated in the Contact_Info table, the below mentioned SQL code can be used.
SELECT Employee.EmpID, Employee.Name FROM Employee WHERE UNIQUE ( SELECT Contact_Info.column FROM Contact_Info WHERE Employee.EmpID = Contact_Info.EmpID);
This will produce the following result:
EmpID | Name |
---|---|
2 | Marry |
3 | Jo |
4 | Kim |
6 | Huang |
Please note that, if the sub-query has duplicate values, the query will show an error.
❮ SQL Keywords