SQL Select Unique - sql - sql tutorial - learn sql
- SELECT UNIQUE is an old syntax which was used in oracle description but later ANSI standard defines DISTINCT as the official keyword.
- After that oracle also added DISTINCT but did not withdraw the service of UNIQUE keyword for the sake of backward compatibility.
- In simple words, we can say that SELECT UNIQUE statement is used to retrieve a unique or distinct element from the table.
- The SELECT UNIQUE construct is an Oracle-only SQL statement. It is equivalent to SELECT DISTINCT.
- The SELECT DISTINCT statement is used to return only distinct (different) values.
- Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
- The SELECT DISTINCT statement is used to return only distinct (different) values.
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorialSyntax
Syntax
- The syntax for SELECT UNIQUE is as follows:
SELECT UNIQUE "column_name"
FROM "table_name";
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorialExample
Example
- We use the following table for our example.
Table product _Information
Store_Name | Sales | Txn_Date |
---|---|---|
Los Angeles | 1500 | Jan-05-1999 |
San Diego | 250 | Jan-07-1999 |
Los Angeles | 300 | Jan-08-1999 |
Boston | 700 | Jan-08-1999 |
- To select all distinct stores in Table product_Information in Oracle, we key in,
SELECT UNIQUE Sales FROM product_Information;
Result:
Sales |
---|
1500 |
250 |
300 |
700 |
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorialExercises
Exercises
- For these exercises, assume we have a table called province _Sales with the following data:
Table province _Sales
province | Year | Orders | Total_Sales |
---|---|---|---|
West | 2013 | 1560 | 325000 |
West | 2014 | 1820 | 380000 |
North | 2013 | 790 | 148000 |
North | 2014 | 995 | 185000 |
East | 2013 | 1760 | 375000 |
East | 2014 | 2220 | 450000 |
South | 2013 | 1790 | 388000 |
South | 2014 | 1695 | 360000 |
- (True or False) The following two statements are equivalent in MySQL:
SELECT UNIQUE province FROM province _Sales;
SELECT DISTINCT province FROM province _Sales;
- Answer: False.
- Which of the following statements are valid in Oracle?
SELECT DISTINCT Year FROM Region_Sales;
SELECT UNIQUE * FROM Region_Sales;
SELECT * FROM Region_Sales;
SELECT UNIQUE Year FROM Region_Sales;
- Answers : a), c), d).
- What is the output for the following statement?
SELECT UNIQUE Region FROM Region_Sales WHERE Orders < 1000;
- Answers : The result is,
REGION |
---|
North |