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

DB2 - SQL Select Statement


SELECT statement is used to retrieve data from a database.

To use SELECT, at a minimum, specify two pieces of information, that what you want to select and from where you want to select it.

Syntax:

SELECT column1, column2, ...
  FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:

SELECT * 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

Example 1 : Simple Retrieval

SELECT ProductDesc FROM Product;

This SELECT statement will retrieve a single column called ProductDesc from the table Product.

Result:
ProductDesc
Mouse
Harddrive
Keyboard
RAM
Honda
PEN


Example 2 : Retrieving Multiple Columns

To retrieve multiple columns from a table, multiple column names must be specified after the SELECT keyword, and each column must be separated by a comma.

SELECT ProductID, ProductDesc, SRP FROM Product;

This SELECT statement will retrieve data from multiple columns of the Product table.

Result:
ProductIDProductDescSRP
7001Mouse75.00
7002Harddrive65.00
7003Keyboard36.00
7004RAM23.50
7005Honda1,200
7006PEN7.45


Example 3 : Retrieving All Columns

In addition to being able to specify desired columns (one or more, as seen earlier), SELECT statements can also request all columns without having to list them individually. This is done by using the asterisk (*) wildcard character in lieu of actual column names, as follows.

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



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