Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Using Inner Subqueries in the FROM Section | Nested Subqueries
course content

Зміст курсу

Intermediate SQL

Using Inner Subqueries in the FROM SectionUsing Inner Subqueries in the FROM Section

As you may have understood from previous chapters, an inner subquery creates a table with which we then perform operations in the outer query.

From this, we can conclude that we can also use an inner query within the FROM section to select something from this table.

Let's consider an example where we need to work with employees whose salary is above a certain value. But instead of a WHERE clause, we will use an inner query:

We've created a new table using an inner subquery with which we can work and gather information.

Note

Notice that for such a table, it's essential to provide an alias.

When might this be useful?

Such a query is highly readable; sometimes, it can replace a WHERE clause.

Additionally, if you need to work not with the entire table but only with a filtered part, an inner subquery within the FROM section can be extremely handy.

Now, let's move on to a small task!

In this task, we'll be working with the department table.

Let me remind you what it looks like:

id name type budget
1 Engineering tech 1000000.00
2 Marketing non-tech 500000.00
3 Sales manager 750000.00
4 Human Resources non-tech 300000.00
... ... ... ...
10 Legal non-tech 450000.00

Завдання

Your task is to retrieve the average budget (budget column) for all managerial departments from the department table. Use an inner subquery in the FROM section to filter only the managerial type of department. Also, use the alias manager_departments for this table so the task is checked correctly.

Note:

The response should have only one column, average_budget. This is an alias for the aggregate function AVG().

Note

This task could also be done using a WHERE clause, but we use a nested query here to practice this syntax, which will be useful for solving more complex problems in the future.

Все було зрозуміло?

Секція 2. Розділ 3
toggle bottom row
course content

Зміст курсу

Intermediate SQL

Using Inner Subqueries in the FROM SectionUsing Inner Subqueries in the FROM Section

As you may have understood from previous chapters, an inner subquery creates a table with which we then perform operations in the outer query.

From this, we can conclude that we can also use an inner query within the FROM section to select something from this table.

Let's consider an example where we need to work with employees whose salary is above a certain value. But instead of a WHERE clause, we will use an inner query:

We've created a new table using an inner subquery with which we can work and gather information.

Note

Notice that for such a table, it's essential to provide an alias.

When might this be useful?

Such a query is highly readable; sometimes, it can replace a WHERE clause.

Additionally, if you need to work not with the entire table but only with a filtered part, an inner subquery within the FROM section can be extremely handy.

Now, let's move on to a small task!

In this task, we'll be working with the department table.

Let me remind you what it looks like:

id name type budget
1 Engineering tech 1000000.00
2 Marketing non-tech 500000.00
3 Sales manager 750000.00
4 Human Resources non-tech 300000.00
... ... ... ...
10 Legal non-tech 450000.00

Завдання

Your task is to retrieve the average budget (budget column) for all managerial departments from the department table. Use an inner subquery in the FROM section to filter only the managerial type of department. Also, use the alias manager_departments for this table so the task is checked correctly.

Note:

The response should have only one column, average_budget. This is an alias for the aggregate function AVG().

Note

This task could also be done using a WHERE clause, but we use a nested query here to practice this syntax, which will be useful for solving more complex problems in the future.

Все було зрозуміло?

Секція 2. Розділ 3
toggle bottom row
some-alt