Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Subqueries in FROM Clauses | Subqueries in Human Resources
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

Натисніть або перетягніть елементи та заповніть пропуски

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Натисніть або перетягніть елементи та заповніть пропуски

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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