TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

DB2 - SQL Select Distinct Statement


SQL uses DISTINCT to remove duplicate rows from the result set.

Syntax:

SELECT DISTINCT column1, column2, ...
  FROM table_name;

DB2 Database:

Below is a selection from the "Product" table in the DB2 database.

ProductIDProductDescCategorySRPQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7005HondaBikes1,200
7006PEN7.451074.50
7007KeyboardAccessories40.00331,118.00
7008RAMComponents23.5016376.00

Example:

For getting the unique ProductDesc, you need to use the following query.

SELECT DISTINCT ProductDesc FROM Product;

The above SQL statement lists the number of different (distinct) ProductDesc in the "Product" table.

Result:
ProductIDProductDescCategorySRPQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RAMComponents23.5016376.00
7005HondaBikes1,200
7006PEN7.451074.50

Note: If you have multiple column names listed after the DISTINCT keyword, The DISTINCT keyword is applied to all columns. It means that the query will use the combination of values in all columns to evaluate the distinction.



If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal