totn MariaDB

MariaDB: IN Condition

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

Description

The MariaDB IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the IN condition in MariaDB is:

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

OR

expression IN (subquery);

Parameters or Arguments

expression
A value to test.
value1, value2 ..., or value_n
The values to test against expression.
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.

Note

  • The MariaDB IN condition will return the records where expression is value1, value2..., or value_n.
  • The MariaDB IN condition is also called the MariaDB IN operator.

Example - With Character

Let's look at an example of how to use the IN condition with character values in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_name IN ('TechOnTheNet.com', 'CheckYourMath.com', 'BigActivities.com');

This MariaDB IN condition example would return all rows from the sites table where the site_name is either 'TechOnTheNet.com' or 'CheckYourMath.com. Because the * is used in the SELECT, all fields from the sites table would appear in the result set.

The above IN example is equivalent to the following SELECT statement:

SELECT *
FROM sites
WHERE site_name = 'TechOnTheNet.com'
OR site_name = 'CheckYourMath.com'
OR site_name = 'BigActivities.com';

As you can see, using the MariaDB IN condition makes the statement easier to read and more efficient.

Example - With Numeric

Next, let's look at an example of how to use the IN condition with numeric values in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_id IN (1, 2, 3, 99);

This MariaDB IN condition example would return all sites where the site_id is either 1, 2, 3, or 99.

The above IN example is equivalent to the following SELECT statement:

SELECT *
FROM sites
WHERE site_id = 1
OR site_id = 2
OR site_id = 3
OR site_id = 99;

Example - Using NOT operator

Finally, let's look at an example of how to use the IN condition with the NOT operator in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_name NOT IN ('TechOnTheNet.com', 'CheckYourMath.com');

This MariaDB IN condition example would return all rows from the sites table where the site_name is not 'TechOnTheNet.com' or 'CheckYourMath.com'. Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.