Careeroza
IN
The SQL IN Operator
IN lets you specify several values in a WHERE clause. It is shorthand for multiple OR conditions.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);Example
Customers in India, UK or USA:
SELECT * FROM Customers
WHERE Country IN ('India', 'UK', 'USA');Result: 5 rows
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 1 | Aarav Traders | Aarav Shah | Mumbai | India |
| 2 | Blue Harbor Foods | Emma Clark | London | UK |
| 4 | Delta Mart | Priya Nair | Bengaluru | India |
| 5 | Evergreen Grocers | Noah Wilson | New York | USA |
| 8 | Harbor Lights | Olivia Martin | London | UK |
NOT IN
Example
Customers not in those countries:
SELECT * FROM Customers
WHERE Country NOT IN ('India', 'UK', 'USA');Result: 3 rows
| CustomerID | CustomerName | ContactName | City | Country |
|---|---|---|---|---|
| 3 | Cedar & Co | Liam Brown | Toronto | Canada |
| 6 | Fjord Supplies | Ingrid Olsen | Oslo | Norway |
| 7 | Golden Spoon | Mei Chen | NULL | Singapore |
IN (SELECT)
The list can come from a subquery.
Example
Customers who have placed at least one order:
SELECT CustomerName FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders);Result: 6 rows
| CustomerName |
|---|
| Aarav Traders |
| Blue Harbor Foods |
| Cedar & Co |
| Delta Mart |
| Evergreen Grocers |
| Golden Spoon |
Note: Be careful with NOT IN and a subquery that can return NULL: if any value in the list is NULL, NOT IN returns no rows. NOT EXISTS avoids this (see the Subqueries module).