DB2 Syntax Fundamentals
WHERE clause
In simple queries, the WHERE clause is used to filter data.
1. syntax: SELECT * FROM table_name WHERE condition
2. Example:
SELECT * FROM users WHERE age > 18;
This SQL statement returns all people from the users table whose age is greater than 18.
IN clause
The IN clause is used to specify one or more values.
1. syntax: SELECT * FROM table_name WHERE field IN (column_name)
2. Example:
SELECT * FROM users WHERE country IN ('USA', 'Canada');
This SQL statement returns all people from the users table whose country is either 'USA' or 'Canada'.
LIKE operator
The LIKE operator is used to match a specific pattern within strings.
1. syntax: SELECT * FROM table_name WHERE field LIKE '%pattern%'
2. Example:
SELECT * FROM users WHERE username LIKE 'j%';
This SQL statement returns all people from the users table whose usernames start with 'j'.
OR operator
The OR operator is used to connect two conditions in a WHERE clause.
1. syntax: SELECT * FROM table_name WHERE condition1 OR condition2
2. Example:
SELECT * FROM users WHERE age > 18 OR country='USA';
This SQL statement returns all people from the users table whose age is greater than 18 or whose country is 'USA'.
AND operator
The AND operator is used to connect two conditions in a WHERE clause.
1. syntax: SELECT * FROM table_name WHERE condition1 AND condition2
2. Example:
SELECT * FROM users WHERE age > 18 AND country='USA';
This SQL statement returns all people from the users table whose age is greater than 18 and whose country is 'USA'.