Db2 AND

Summary: in this tutorial, you will learn how to use the Db2 AND operator to combine multiple Boolean expressions.

Introduction Db2 AND operator

The AND operator is a logical operator that combines two Boolean expressions or predicates. You use the AND operator to specify that a search must satisfy both conditions.

The following illustrates the AND operator syntax:

boolean_expression1 AND boolean_expression2
Code language: SQL (Structured Query Language) (sql)

In this syntax, the boolean_expression1 and boolean_expression2 evaluate to true, false, and unknown.

This table displays the result when combining true, false, and unknown values using the AND operator:

TRUEFALSEUNKNOWN
TRUETRUEFALSEUNKNOWN
FALSEFALSEFALSEFALSE
UNKNOWNUNKNOWNFALSEUNKNOWN

If you use both AND and OR operators in an expression, Db2 evaluates the AND operator first. However, you can change the order of evaluation using parentheses.

To negate the AND operator, you use the NOT operator as follows:

NOT (boolean_expression1 AND boolean_expression2)
Code language: SQL (Structured Query Language) (sql)

The AND operator is often used in the WHERE clause to form the search condition for the SELECT, UPDATE, and DELETE statement.

Db2 AND operator examples

Let’s use the books table from the sample database for the demo.

1) Db2 AND operator example

This example uses the AND operator to find the books whose ratings are between 4 and 5:

SELECT    
    title, 
    rating, 
    total_pages
FROM    
    books
WHERE rating >= 4
        AND rating <= 5
ORDER BY 
    title;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

db2 AND operator example

2) Using multiple Db2 AND operators example

This example uses two AND operators to find books whose ratings are between 4 and 5 and the number of pages is greater than 1300:

SELECT    
    title, 
    rating, 
    total_pages
FROM    
    books
WHERE rating >= 4
        AND rating <= 5 AND total_pages > 1300
ORDER BY 
    title;
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

db2 AND operator multiple AND examples

3) Using Db2 AND operator with parentheses

If you use the AND and OR operators with more than two conditions, you can use parentheses to explicitly specify the order in which you want Db2 to evaluate the conditions.

This example uses both AND and OR operators to find books that satisfy both the following conditions:

  • The number of pages is greater than 1,000 and less than 1,200
  • Rating is greater than 4.7

Query:

SELECT   
    title, 
    total_pages, 
    rating
FROM   
    books
WHERE(total_pages < 1200
        AND total_pages > 1000)
    OR rating > 4.7
ORDER BY 
    rating DESC;
Code language: SQL (Structured Query Language) (sql)

If you move the parentheses, the meaning of the WHERE clause can change significantly:

SELECT   
    title, 
    total_pages, 
    rating
FROM   
    books
WHERE total_pages < 1200
        AND (total_pages > 1000
    OR rating > 4.7)
ORDER BY 
    rating DESC;
Code language: SQL (Structured Query Language) (sql)
db2 AND with OR operator example

This query returns books that satisfy both the following conditions:

  • The number of pages is less than 1200
  • The number of pages is greater than 1000 or rating is greater than 4.7

4) Using Db2 NOT AND operator example

This example uses the NOT AND operator to find books that have the number of pages less than or equal 500 or rating greater than 4.7:

SELECT    
    title, 
    total_pages, 
    rating
FROM    
    books
WHERE NOT(total_pages > 500
            AND rating < 4.7)
ORDER BY 
    total_pages DESC, 
    rating DESC;
Code language: SQL (Structured Query Language) (sql)

Here is the result set:

db2 NOT AND example

Notice that based on De Morgan’s laws, NOT (A AND B) is equivalent to (NOT A OR NOT B)

In this tutorial, you have learned how to use the Db2 AND operator to form a search condition that combines two Boolean expressions.

Was this tutorial helpful ?