DB2 Tutorial

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

Db2 IN

Summary: in this tutorial, you will learn how to use the Db2 IN to compare a value with a set of values.

Introduction to Db2 IN operator

The Db2 IN operator is a logical operator that compares a value with a set of values:

expression IN (v1, v2, v3, ...)

The IN operator returns true if the value of the expression matches one of the value in the list v1, v2, v3… Otherwise, it returns false.

The value list can be literal values as shown in the syntax above or a result set of a query. In this case, we called the query that supplies value to IN operator is a subquery. The subquery must return a single column or you will get an error.

The following illustrates the syntax of the IN operator with a subquery.

expression IN (subquery)

The IN operator is equivalent to multiple OR operators:

expression = v1 OR expression = v2 OR expression = v3 OR ...

Using IN operator makes the code cleaner and easier to understand.

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

expression NOT IN (v1, v2, v3, ...)

It is equivalent to the following syntax:

expression <> v1 AND expression <> v2 AND expression <> v3 AND ...

You often use the IN operator in the WHERE clause of the SELECT, DELETE, and UPDATE statements.

Db2 IN operator examples

We will use the books table from the sample database to demonstrates the IN operator.

1) Using Db2 IN operator with a list of numeric values

This example uses the IN operator to find the books whose publisher id is in the list of 100, 103, and 105:

SELECT title, publisher_id FROM books WHERE publisher_id IN (100, 103, 105) ORDER BY title;

Here is the result set:

Db2 IN example

The query above is equivalent to the following query that uses multiple OR operators:

SELECT title, publisher_id FROM books WHERE publisher_id = 100 OR publisher_id = 103 OR publisher_id = 105 ORDER BY title;

2) Using Db2 IN with a subquery

The following query uses the LIKE operator to find publisher ids of the publisher whose name starts with 'Addison Wesley':

SELECT publisher_id FROM publishers WHERE name LIKE 'Addison Wesley%';

It returns three rows:

Db2 IN subquery

You can use this result set to supply to the IN operator to find books whose publisher name starts with 'Addison Wesley':

SELECT title, publisher_id FROM books WHERE publisher_id IN ( SELECT publisher_id FROM publishers WHERE name LIKE 'Addison Wesley%' );

Here is the partial output:

Db2 IN with subquery example

3) Using Db2 NOT IN operator example

The following example uses the NOT IN operator to find books whose publisher id is not in the list (100,103,105):

SELECT title, publisher_id FROM books WHERE publisher_id NOT IN (100, 103, 105) ORDER BY title;

The output is as follows:

Db2 NOT IN example

It is equivalent to the following query that uses multiple AND operators:

SELECT title, publisher_id FROM books WHERE publisher_id <> 100 AND publisher_id <> 103 AND publisher_id <> 105 ORDER BY title;

In this tutorial, you have learned how to use the Db2 IN operator to compare a value with a list of values.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 LIKE
Next Db2 LIMIT

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.