Using WHERE Clause in SELECT statement in DB2 for i SQL |
The WHERE clause can be used to specify the search condition that identifies the row or rows that we want to retrieve, update or delete.
A search condition consists of one or more predicates. A predicate specifies a test that we want SQL to apply to specific row/rows of the table.
Select * from PF1 where empid = 'E1'
Output
EMPID EMPNAME MANAGERID E1 EMPNAME1 M1
Expressions in the WHERE clause
It is that we want to compare to something else. We can specify following expressions.
Where empid = 'E1'
WHERE 'E1' = EMPID
WHERE empid = :empid
Where empName = User
Where empName is NULL
Note:
Comparison operators used with WHERE clause
Supported SQL comparison operators.
NOT keyword used with WHERE clause
We can precede a predicate with the NOT keyword to specify that we want the opposite of the predicate value i.e. TRUE if the predicate is FALSE. NOT only applies to the predicate it precede, not to all the predicates in the WHERE clause.
Therefore, if we want those employees whose employee id is not 'E1'
SELECT * FROM PF1 WHERE NOT EMPID = 'E1'
which is equivalent to
SELECT * FROM PF1 WHERE NOT EMPID <> 'E1'
Output
EMPID EMPNAME MANAGERID E2 EMPNAME2 M2