SQL DISTINCT || DB2 for i SQL

DISTINCT clause in DB2 for i SQL
DISTINCT clause in DB2 for i SQL, DISTINCT, SELECT DISTINCT, SQL Tutorial, SQL, DML, DB2 for i SQL, IBMi DB2
DISTINCT clause in DB2 for i SQL

The SELECT DISTINCT statement is used to return only distinct rows i.e. different rows. It will avoid the duplicate rows to return in any specific column/table. It fetches the unique values.

Syntax of DISTINCT clause

SELECT DISTINCT column1, column2, ...
FROM table_name

Create table Customer

Use existing table Customer to understand the functioning of this clause.

Example 1: Select all the different City from Customer Table

SELECT DISTINCT CUSTCITY FROM customer

Output:

CUSTCITY 
AGRA     
PATNA    
HISAR    
IDUKKI   
KOLKATA  
CHENNAI  
KOCHI    

Example 2: Select all the different rows from Customer Table

SELECT DISTINCT * FROM customer

Output:

CUSTID   CUSTNAME              CUSTCITY              CUSTSTATE             CUSTCOUNTRY
     1   Amit                  PATNA                 BIHAR                 INDIA      
     2   Anil                  PATNA                 BIHAR                 INDIA      
     3   Dhanraj               HISAR                 HARYANA               INDIA      
     4   Udeep                 KOCHI                 KERELA                INDIA      
     5   Yatin                 IDUKKI                KERALA                INDIA      
     6   Tinku                 AGRA                  UTTAR PRADESH         INDIA      
     7   Mounish               KOLKATA               WEST BENGAL           INDIA      
     8   Annamalai             CHENNAI               TAMIL NADU            INDIA      
     9   Kannan                CHENNAI               TAMIL NADU            INDIA      

Example 3: Select all the different rows from Customer Table based on list of expressions

SELECT DISTINCT CUSTCITY, CUSTSTATE, CUSTCOUNTRY FROM customer

Output:

CUSTCITY              CUSTSTATE             CUSTCOUNTRY
AGRA                  UTTAR PRADESH         INDIA      
HISAR                 HARYANA               INDIA      
KOLKATA               WEST BENGAL           INDIA      
KOCHI                 KERELA                INDIA      
IDUKKI                KERALA                INDIA      
PATNA                 BIHAR                 INDIA      
CHENNAI               TAMIL NADU            INDIA      

You may like these posts

  • Creating Indexes in DB2 for i SQL Indexes can be created using CREATE INDEX statement and can be used to sort and select data. Indexes also helps the system to retrieve the data …
  • How to retrieve catalog information about a table There are several tables and views exists in QSYS2 scheme that fulfil several purposes of the end user. The SYSTABLES view is one…
  • Basic Select statement in DB2 for i SQL We can use SELECT statement to retrieve data from the table/tables or view/views. We can use SELECT to retrieve data in any way. IF SQL SE…
  • LIKE predicate in Search condition in DB2 for i SQL It is used to search for a specified pattern in a column and is used in WHERE clause. There are two wildcards used with LIKE…
  • How to create and use View in DB2 for i SQL A view is similar to Non-keyed LF and is used to access data in one or more table or views. We can create view by using the SELECT stat…
  • How to retrieve catalog information about a column QSYS2 schema contains a view called SYSCOLUMNS that contains a row for each column of a table and view in the schema. Display…

Post a Comment