SQL > SQL Commands > Select Unique

The SELECT UNIQUE construct is an Oracle-only SQL statement. It is equivalent to SELECT DISTINCT.

Syntax

The syntax for SELECT UNIQUE is as follows:

SELECT UNIQUE "column_name"
FROM "table_name";

Example

We use the following table for our example.

Table Store_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 Store_Information in Oracle, we key in,

SELECT UNIQUE Sales FROM Store_Information;

Result:

Sales
1500
250
300
700

Exercises

For these exercises, assume we have a table called Region_Sales with the following data:

Table Region_Sales

 Region  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 

1. (True or False) The following two statements are equivalent in MySQL:
SELECT UNIQUE Region FROM Region_Sales;
SELECT DISTINCT Region FROM Region_Sales;

2. Which of the following statements are valid in Oracle?
a) SELECT DISTINCT Year FROM Region_Sales;
b) SELECT UNIQUE * FROM Region_Sales;
c) SELECT * FROM Region_Sales;
d) SELECT UNIQUE Year FROM Region_Sales;

3. What is the output for the following statement?
SELECT UNIQUE Region FROM Region_Sales WHERE Orders < 1000;

Next: SQL JOIN

This page was last updated on June 19, 2023.




Copyright © 2024   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact