DB2 - SQL Where Clause
The WHERE clause is used to filter records.
Retrieving just the data you want, involves specifying search criteria, also known as a filter condition.
Within a SELECT statement, data is filtered by specifying search criteria in the WHERE clause.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition; |
WHERE clause is not only used in SELECT statement, it is also used in UPDATE, DELETE statement.
DB2 Database:
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 |
7007 | Cddrive | Accessories | 75.00 | | |
7008 | Speaker | Accessories | 75.00 | | |
Example:
The below SQL statement retrieves two columns from the "Product" table, but instead of returning all rows, only
rows with a SRP value of 75.00 are returned.
SELECT ProductID, ProductDesc
FROM Product
WHERE SRP = 75.00; |
Result:
ProductID | ProductDesc |
7001 | Mouse |
7007 | Cddrive |
7008 | Speaker |
Numeric Fields vs Text Fields
SQL requires single quotes around text values (most database systems will also allow double quotes).. However, numeric fields should not be enclosed in quotes.
SELECT ProductID, ProductDesc, SRP
FROM Product
WHERE Category = 'Accessories'; |
Result:
ProductID | ProductDesc | SRP |
7001 | Mouse | 75.00 |
7003 | Keyboard | 36.00 |
7007 | Cddrive | 75.00 |
7008 | Speaker | 75.00 |
WHERE Clause Operators:
SQL supports a whole range of conditional operators in the WHERE clause as listed.
Operator | Description |
= | Equality |
<> | Non-equality |
!= | Non-equality |
< | Less than |
<= | Less than or equal to |
!< | Not less than |
> | Greater than |
>= | Greater than or equal to |
!> | Not greater than |
BETWEEN | Between two specified values including the specified start and end value |
IS NULL | Is a NULL value |
Example 1:
To List all products that cost less than $10
SELECT ProductID, ProductDesc
FROM Product
WHERE SRP < 10; |
Result:
ProductID | ProductDesc |
7006 | PEN |
Example 2:
To List all Category products other than 'Accessories'
SELECT ProductID, ProductDesc, SRP
FROM Product
WHERE Category <> 'Accessories'; |
Result:
ProductID | ProductDesc | SRP |
7002 | Harddrive | 65.00 |
7004 | RAM | 23.50 |
7005 | Honda | 1,200 |
7006 | PEN | 7.45 |
Example 3:
To retrieve all products with a price between $5 and $10, including the specified start and end values.
SELECT ProductID, ProductDesc
FROM Product
WHERE SRP BETWEEN 5 AND 25; |
Result:
ProductID | ProductDesc |
7004 | RAM |
7006 | RAM |
Example 4:
To return a list of all products that have no Category(i.e. null value)
SELECT ProductID, ProductDesc, SRP
FROM Product
WHERE Category IS NULL; |
Result:
ProductID | ProductDesc | SRP |
7002 | Harddrive | 65.00 |
7006 | PEN | 7.45 |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!