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.
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; |
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductDesc | Category | SRP | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | PEN | 7.45 | 10 | 74.50 |
SELECT ProductDesc FROM Product; |
This SELECT statement will retrieve a single column called ProductDesc from the table Product.
ProductDesc |
---|
Mouse |
Harddrive |
Keyboard |
RAM |
Honda |
PEN |
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.
ProductID | ProductDesc | SRP |
---|---|---|
7001 | Mouse | 75.00 |
7002 | Harddrive | 65.00 |
7003 | Keyboard | 36.00 |
7004 | RAM | 23.50 |
7005 | Honda | 1,200 |
7006 | PEN | 7.45 |
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; |
ProductID | ProductDesc | Category | SRP | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | PEN | 7.45 | 10 | 74.50 |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!