IN: Match from a List
The IN keyword checks if a value is in a list of values.
It is a short and clear way to match many values, instead of writing many OR conditions.
Syntax
SELECT column1, column2, ... FROM table_name
WHERE column IN (value1, value2, ...);
Example 1: Employees in HR or Marketing
SELECT name, department FROM employees
WHERE department IN ('HR', 'Marketing');
Output:
name | department
-------+------------
Alice | HR
David | Marketing
Ryan | Marketing
Anna | HR
(4 rows)
This returns all employees who are in either HR or Marketing. It is the same as department = 'HR' OR department = 'Marketing'.
Example 2: Employees aged 24, 30 or 33
SELECT name, age FROM employees
WHERE age IN (24, 30, 33);
Output:
name | age
--------+-----
David | 30
Mike | 24
Jack | 33
Lucy | 30
Anna | 24
Thomas | 33
(6 rows)
This returns employees whose age is 24, 30 or 33.
Tip
Combine IN with NOT to exclude values:
SELECT name, department FROM employees
WHERE department NOT IN ('IT', 'Sales', 'Finance');
Output:
name | department
-------+------------
Alice | HR
David | Marketing
Ryan | Marketing
Anna | HR
(4 rows)
The IN keyword keeps your filters short and easy to read when you match many values.
In the exercise, use IN to find employees by their names.