Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Subqueries in FROM Clauses | Subqueries in Human Resources
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Subqueries in SQL

bookSubqueries in FROM Clauses

1234567891011121314151617
SELECT d.name AS department_name, avg_salaries.avg_salary FROM departments d JOIN ( SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ) AS avg_salaries ON d.department_id = avg_salaries.department_id ORDER BY avg_salaries.avg_salary DESC;
copy

When you use a subquery in the FROM clause, you are creating a derived table that exists only for the duration of the query. In the example above, the subquery calculates the average salary for each department from the employees table. This result set is given an alias, avg_salaries, and is treated just like a regular table in the main query. The main query then joins this derived table with the departments table, allowing you to display each department's name alongside its average salary. This approach is especially useful in HR analytics, where you often need to combine aggregated results with other tables to produce meaningful reports. Using subqueries in the FROM clause helps you break down complex problems into manageable steps, making your SQL more readable and modular.

1. Which of the following is a key benefit of using a subquery in the FROM clause?

2. Fill in the blanks to complete the query that finds the average salary per department and joins it with the departments table:

question mark

Which of the following is a key benefit of using a subquery in the FROM clause?

Select the correct answer

question-icon

Fill in the blanks to complete the query that finds the average salary per department and joins it with the departments table:

(salary) AS avg_salaryFROM employees GROUP BY
name | avg_salary
----------------|-----------
Engineering | 87666.67
Finance | 81000.00
Marketing | 73333.33
Human Resources | 70500.00
Sales | 66000.00
IT | 78500.00
Customer Support| 55500.00
Logistics | 60500.00

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain what a derived table is in more detail?

How does the subquery improve the readability of the SQL?

Can you give another example of using a subquery in the FROM clause?

bookSubqueries in FROM Clauses

Scorri per mostrare il menu

1234567891011121314151617
SELECT d.name AS department_name, avg_salaries.avg_salary FROM departments d JOIN ( SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ) AS avg_salaries ON d.department_id = avg_salaries.department_id ORDER BY avg_salaries.avg_salary DESC;
copy

When you use a subquery in the FROM clause, you are creating a derived table that exists only for the duration of the query. In the example above, the subquery calculates the average salary for each department from the employees table. This result set is given an alias, avg_salaries, and is treated just like a regular table in the main query. The main query then joins this derived table with the departments table, allowing you to display each department's name alongside its average salary. This approach is especially useful in HR analytics, where you often need to combine aggregated results with other tables to produce meaningful reports. Using subqueries in the FROM clause helps you break down complex problems into manageable steps, making your SQL more readable and modular.

1. Which of the following is a key benefit of using a subquery in the FROM clause?

2. Fill in the blanks to complete the query that finds the average salary per department and joins it with the departments table:

question mark

Which of the following is a key benefit of using a subquery in the FROM clause?

Select the correct answer

question-icon

Fill in the blanks to complete the query that finds the average salary per department and joins it with the departments table:

(salary) AS avg_salaryFROM employees GROUP BY
name | avg_salary
----------------|-----------
Engineering | 87666.67
Finance | 81000.00
Marketing | 73333.33
Human Resources | 70500.00
Sales | 66000.00
IT | 78500.00
Customer Support| 55500.00
Logistics | 60500.00

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt