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, ...)
Code language: SQL (Structured Query Language) (sql)

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)
Code language: SQL (Structured Query Language) (sql)

The IN operator is equivalent to multiple OR operators:

expression = v1 OR 
expression = v2 OR 
expression = v3 OR
...
Code language: SQL (Structured Query Language) (sql)

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, ...)
Code language: SQL (Structured Query Language) (sql)

It is equivalent to the following syntax:

expression <> v1 AND
expression <> v2 AND 
expression <> v3 AND
...
Code language: SQL (Structured Query Language) (sql)

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;
Code language: SQL (Structured Query Language) (sql)

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;
Code language: SQL (Structured Query Language) (sql)

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%';
Code language: SQL (Structured Query Language) (sql)

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%'
);
Code language: SQL (Structured Query Language) (sql)

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;
Code language: SQL (Structured Query Language) (sql)

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;
Code language: SQL (Structured Query Language) (sql)

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 ?