Write a Subquery in a WHERE Clause
In this step, you will learn how to use a subquery within the WHERE
clause of a SQL query. A subquery, also known as an inner query or nested query, is a query embedded inside another query. Subqueries are used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
Understanding Subqueries in the WHERE Clause
A subquery in the WHERE
clause is typically used to compare a column's value against the result of the subquery. The subquery is executed first, and its result is then used by the outer query.
Basic Syntax:
SELECT column1, column2
FROM table_name
WHERE column_name IN (SELECT column_name FROM another_table WHERE condition);
Here, the subquery (SELECT column_name FROM another_table WHERE condition)
returns a set of values. The outer query then selects rows from table_name
where column_name
is in that set.
Scenario:
Let's assume you have two tables: employees
and departments
. The employees
table contains information about employees, including their employee_id
, employee_name
, and department_id
. The departments
table contains information about departments, including their department_id
and department_name
.
We want to find all employees who work in the 'Sales' department.
Step 1: Create the Tables and Insert Data
First, connect to the PostgreSQL database using the postgres
user:
sudo -u postgres psql
Next, create the departments
table:
CREATE TABLE departments (
department_id SERIAL PRIMARY KEY,
department_name VARCHAR(50)
);
Insert some sample data into the departments
table:
INSERT INTO departments (department_name) VALUES
('Sales'),
('Marketing'),
('Engineering');
Now, create the employees
table:
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
employee_name VARCHAR(50),
department_id INTEGER REFERENCES departments(department_id)
);
Insert some sample data into the employees
table:
INSERT INTO employees (employee_name, department_id) VALUES
('Alice', 1),
('Bob', 2),
('Charlie', 1),
('David', 3),
('Eve', 2);
Step 2: Write the Subquery
Now, let's write the query to find all employees who work in the 'Sales' department using a subquery in the WHERE
clause.
SELECT employee_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'Sales');
Explanation:
- The subquery
(SELECT department_id FROM departments WHERE department_name = 'Sales')
selects the department_id
from the departments
table where the department_name
is 'Sales'. In this case, it will return 1
.
- The outer query then selects the
employee_name
from the employees
table where the department_id
is in the set returned by the subquery (which is just 1
).
Step 3: Execute the Query and View the Results
Execute the query in your psql
terminal. You should see the following output:
employee_name
---------------
Alice
Charlie
(2 rows)
This shows that Alice and Charlie are the employees who work in the 'Sales' department.
Step 4: Using EXISTS
with Subqueries
Another way to use subqueries in the WHERE
clause is with the EXISTS
operator. The EXISTS
operator tests for the existence of rows in a subquery. It returns true if the subquery returns any rows, and false otherwise.
Here's an example of using EXISTS
to achieve the same result:
SELECT employee_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM departments d
WHERE d.department_id = e.department_id
AND d.department_name = 'Sales'
);
This query achieves the same result as the previous one, but uses the EXISTS
operator instead of IN
.
Explanation:
- The subquery
SELECT 1 FROM departments d WHERE d.department_id = e.department_id AND d.department_name = 'Sales'
checks if there is a department with the name 'Sales' that has the same department_id
as the current employee.
- If the subquery returns any rows (meaning there is a 'Sales' department with the same
department_id
), the EXISTS
operator returns true, and the employee's name is selected.
Execute the query in your psql
terminal. You should see the same output as before:
employee_name
---------------
Alice
Charlie
(2 rows)
You have now successfully used a subquery in the WHERE
clause to filter data based on a condition in another table. You've also learned how to use the EXISTS
operator with a subquery.