DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 AND

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

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)

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;

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;

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;

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;
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;

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 ?
  • YesNo
Previous Db2 SELECT DISTINCT
Next Db2 OR

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.