SQL IN Tutorial
Example Table
We will use the following table named employees
for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, employee_name, department, salary) VALUES
(1, 'John Doe', 'HR', 50000.00),
(2, 'Jane Smith', 'Finance', 60000.00),
(3, 'Emily Davis', 'IT', 70000.00),
(4, 'Michael Brown', 'Finance', 80000.00),
(5, 'Jessica Wilson', 'IT', 90000.00);
Employees Table
employee_id | employee_name | department | salary |
---|---|---|---|
1 | John Doe | HR | 50000.00 |
2 | Jane Smith | Finance | 60000.00 |
3 | Emily Davis | IT | 70000.00 |
4 | Michael Brown | Finance | 80000.00 |
5 | Jessica Wilson | IT | 90000.00 |
Using SQL IN
The SQL IN
operator allows you to specify multiple values in a WHERE
clause. It is used to filter the result set based on a list of specified values.
Example 1: Select Employees from Specific Departments
To select employees who work in the 'Finance' or 'IT' departments, use the following query:
SELECT employee_name, department
FROM employees
WHERE department IN ('Finance', 'IT');
Result:
employee_name | Jane Smith |
---|---|
department | Finance |
Emily Davis | |
IT | |
Michael Brown | |
Finance | |
Jessica Wilson | |
IT |
Example 2: Select Employees with Specific Salaries
To select employees who have salaries of 60000.00, 70000.00, or 90000.00, use the following query:
SELECT employee_name, salary
FROM employees
WHERE salary IN (60000.00, 70000.00, 90000.00);
Result:
employee_name | Jane Smith |
---|---|
salary | 60000.00 |
Emily Davis | |
70000.00 | |
Jessica Wilson | |
90000.00 |