Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested Queries | Nested Subqueries
Intermediate SQL
course content

Course Content

Intermediate SQL

Intermediate SQL

1. Grouping
2. Nested Subqueries
3. Joining Tables
4. DDL and DML in SQL

book
Nested Queries

The school administration is pleased with our recent achievements and is eager to move forward.

As they begin their summer break, we have the chance to explore new methods and improve our SQL skills.

Our focus will be on nested queries, a key concept in SQL.

Let's look at an example from our company database. As the company grows, we've added a new department table. This changes how our database works.

Let's check out the new table's structure to see its role in our database:

The new table contains departments, their types, and budgets. And in the employee table, as you may recall, there is information about the department each employee belongs to.

Let's consider an example where we use nested queries to retrieve those employees who work in departments with budgets of $500,000 or less:

1234567
SELECT * FROM employees WHERE department IN ( SELECT name FROM department WHERE budget <= 500000 )
copy

We use a WHERE clause to specify that the department name should be present in the table we retrieve with the inner query.

Let's break down this query into two separate ones to see how it works. First, let's retrieve the names of departments with budgets of $500,000 or less (our inner query):

123
SELECT name FROM department WHERE budget <= 500000
copy

Now, considering the obtained result, let's write the second query to retrieve information about the employees from these departments:

12345
SELECT * FROM employees WHERE department IN ( 'Marketing', 'Human Resources', 'Finance', 'Customer Service', 'Legal' )
copy

The result of this query will be the same as the first query.

Here's a step-by-step breakdown:

  1. The inner query gets the names of the departments we need;

  2. The WHERE clause checks if these departments are in the table from the inner query;

  3. We get the result we want.

Here's the basic syntax for an inner query:

When using nested queries, keep these important points in mind:

  1. Single Result in Subquery: ensure the nested query returns only one column. If used in a comparison, it should ideally return a single value. This is crucial for operators like =, >, <, >=, <=, <>;

  2. Data Types: the data type of the returned column should match the column you're comparing it to, to avoid errors;

  3. Optimization: nested queries can be slow, especially if run for each row in the main query;

  4. Using the IN Operator: nested queries often use the IN operator to check for values in a subset. You can also use comparison operators for more complex conditions.

1. What is a nested query in SQL?

2. Which SQL operator is commonly used with nested queries to check for values in a subset?

3. Why is it important for the data type of the column returned by a nested query to match the column it is compared to?

What is a nested query in SQL?

What is a nested query in SQL?

Select the correct answer

Which SQL operator is commonly used with nested queries to check for values in a subset?

Which SQL operator is commonly used with nested queries to check for values in a subset?

Select the correct answer

Why is it important for the data type of the column returned by a nested query to match the column it is compared to?

Why is it important for the data type of the column returned by a nested query to match the column it is compared to?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt