SQL IN operator

The SQL IN operator allows you to specify multiple values in a WHERE clause. The syntax is:

SELECT * FROM table_name WHERE column_name1 IN (‘some_value1’, ‘some_value2’);

We will use the Employes table to demonstrate how to fetch specific data from database table with the SQL IN operator:

+-----------+-----------+-----------+
| lastName  | firstName | extension |
+-----------+-----------+-----------+
| Murphy    | Diane     | x5800     |
| Patterson | Mary      | x4611     |
| Firrelli  | Jeff      | x9273     |
| Patterson | William   | x4871     |
| Bondur    | Gerard    | x5408     |
| Bow       | Anthony   | x5428     |
| Jennings  | Leslie    | x3291     |
| Thompson  | Leslie    | x4065     |
| Firrelli  | Julie     | x2173     |
| Patterson | Steve     | x4334     |
| Tseng     | Foon Yue  | x2248     |
| Vanauf    | George    | x4102     |
| Bondur    | Loui      | x6493     |
| Hernandez | Gerard    | x2028     |
| Castillo  | Pamela    | x2759     |
| Bott      | Larry     | x2311     |
| Jones     | Barry     | x102      |
| Fixter    | Andy      | x101      |
| Marsh     | Peter     | x102      |
| King      | Tom       | x103      |
| Nishi     | Mami      | x101      |
| Kato      | Yoshimi   | x102      |
| Gerard    | Martin    | x2312     |
+-----------+-----------+-----------+

If we would like to fetch data from the Employees table with all employees whose last name is “Patterson” or “Bow”, we would use the following command:

SELECT * FROM employees WHERE lastName IN (‘Patterson’, ‘Bow’);

This would produce the following result:

+-----------+-----------+-----------+
| lastName  | firstName | extension |
+-----------+-----------+-----------+
| Patterson | Mary      | x4611     |
| Patterson | William   | x4871     |
| Bow       | Anthony   | x5428     |
| Patterson | Steve     | x4334     |
+-----------+-----------+-----------+
Geek University 2022