Search This Blog

SQL: IN Condition

 

SQL: IN Condition

This SQL tutorial explains how to use the SQL IN condition with syntax and examples.

Description

The SQL IN condition (sometimes called the IN operator) allows you to easily test if an expression matches any value in a list of values. It is used to help reduce the need for multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the IN condition in SQL is:

expression IN (value1, value2, .... value_n);

OR

expression IN (subquery);

Parameters or Arguments

expression
This is a value to test.
value1, value2 ..., alue_n
These are the values to test against expression. If any of these values matches expression, then the IN condition will evaluate to true.
subquery
This is a SELECT statement whose result set will be tested against expression. If any of these values matches expression, then the IN condition will evaluate to true.

DDL/DML for Examples

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example - Using the IN Condition with Character Values

The IN condition can be used with any data type in SQL. Let's look at how to use the IN condition with character (string) values.

In this example, we have a table called suppliers with the following data:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Enter the following SQL statement:


SELECT *
FROM suppliers
WHERE supplier_name IN ('Microsoft', 'Oracle', 'Flowers Foods');

There will be 3 records selected. These are the results that you should see:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
300OracleRedwood CityCalifornia
800Flowers FoodsThomasvilleGeorgia

This example would return all rows from the suppliers table where the supplier_name is either Microsoft, Oracle or Flowers Foods. Because the * is used in the select, all fields from the suppliers table would appear in the result set.

It is equivalent to the following SQL statement:


SELECT *
FROM suppliers
WHERE supplier_name = 'Microsoft'
OR supplier_name = 'Oracle'
OR supplier_name = 'Flowers Foods';

As you can see, using the IN condition makes the statement easier to read and more efficient than using multiple OR conditions.

Example - Using the IN Condition with Numeric Values

Next, let's look at how to use the IN condition with numeric values.

In this example, we have a table called customers with the following data:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

Enter the following SQL statement:


SELECT *
FROM customers
WHERE customer_id IN (5000, 7000, 8000, 9000);

There will be 4 records selected. These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
5000SmithJanedigminecraft.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

This example would return all records from the customers table where the customer_id is either 5000, 7000, 8000 or 9000.

It is equivalent to the following SQL statement:


SELECT *
FROM customers
WHERE customer_id = 5000
OR customer_id = 7000
OR customer_id = 8000
OR customer_id = 9000;

Example - Using the IN Condition with the NOT Operator

Finally, let's look at how to use the IN condition with the NOT operator. The NOT operator is used to negate a condition. When we use the NOT operator with the IN condition, we create a NOT IN condition. This will test to see if an expression is not in a list.

In this example, we have a table called products with the following data:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following SQL statement:


SELECT *
FROM products
WHERE product_name NOT IN ('Pear', 'Banana', 'Bread');

There will be 4 records selected. These are the results that you should see:

product_idproduct_namecategory_id
3Orange50
4Apple50
6Sliced Ham25
7KleenexNULL

This example would return all rows from the products table where the product_name is not Pear, Banana or Bread. Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.

It is equivalent to the following SQL statement:


SELECT *
FROM products
WHERE product_name <> 'Pear'
AND product_name <> 'Banana'
AND product_name <> 'Bread';

As you can see, the equivalent statement is written using AND conditions instead of OR conditions because the IN condition is negated.

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...