Question
Which SQL query will correctly calculate the average
salary of employees in each department and group them by department in a table named 'employees'?Solution
To calculate the average salary of employees in each department, we need to use the AVG() aggregate function in SQL. The GROUP BY clause is essential here, as it groups the results by department, ensuring the average is calculated for each individual department, not for the entire table. The correct SQL query is: SELECT department, AVG(salary) FROM employees GROUP BY department This query returns the department and the average salary of employees in that department. Why other options are wrong: b) SELECT department, SUM(salary) FROM employees GROUP BY department : This calculates the sum of the salary, not the average. ItтАЩs useful when you want the total salary for each department. c) SELECT department, AVG(salary) FROM employees : This query is incomplete because it lacks the GROUP BY clause. Without it, it would give the average salary for the entire table, not by department. d) SELECT AVG(salary) FROM employees GROUP BY department : This query incorrectly places the aggregate function without selecting the department column, which will result in an error or incorrect output. e) SELECT department, COUNT(salary) FROM employees GROUP BY department : This counts the number of salaries in each department, not the average salary.
(1) рдореЛрд╣рдирджрд╛рд╕ рдХрд░рдордЪрдВрдж рдЧрд╛рдВрдзреА рдФрд░ рдорд╛рдУ
( рдп) рдХреЗ рдЕрдирдиреНрддрд░ рдореЗрдВ
...рдЕрддреНрдпрдзрд┐рдХ рдмрдврд╝рд╛тАУрдЪрдврд╝рд╛ рдХрд░ рдХрд╣реА рдЧрдИ рдмрд╛рдд рдХреЛ ┬а_____ рдХрд╣рддреЗ рд╣реИрдВред┬а
1 рдЕрд░реНрдЬреБрди рдЕрд╢реНрд╡рддреНрдерд╛рдорд╛ рдХреЛ рдкрдХрдбрд╝рдиреЗ рдХреЗ рд▓рд┐рдП рдирд┐рдХрд▓ рдкрдбрд╝реЗред
(...
рджреЗрд╢ рдореЗрдВ рдЖрдЬ рднреА рдХреМрди-рд╕реА рд╕рдорд╕реНрдпрд╛ рд╣реИ?
рдХреЛрдИ рд░рд╛рдЬреНрдп рдЕрдкрдиреЗ рдирд╛рдЧрд░рд┐рдХреЛрдВ рд╕реЗ рдХрд┐рди рдХрд░реНрддрд╡реНрдпреЛрдВ рдХреА рдЕрдкреЗрдХреНрд╖рд╛ рдХрд░рддрд╛ я┐╜...
рд╕рдХреНрд░рд┐рдп рдирд╛рдЧрд░рд┐рдХрддрд╛ рдХрд╛ рд╕рдЯрд┐рдХ рдЙрджрд╛рд╣рд░рдг рджрд┐рдП рдЧрдП рд╡рд┐рдХрд▓реНрдкреЛрдВ рдореЗрдВ рдХреМрди рджрд░я┐╜...
рджрд┐рдП рдЧрдП рдЕрдиреБрдЪреНрдЫреЗрдж рдХреЗ рдЕрдиреБрд╕рд╛рд░ тАЬ The moveтАЭ рдХрд╛ рд╣рд┐рдиреНрджреА рдЕрдиреБрд╡рд╛рдж рдХреНрдпрд╛ рд╣реЛрдЧя┐╜...
рдЧрджреНрдпрд╛рдВрд╢ рдореЗрдВ рдкреНрд░рдпреБрдХреНрдд рд╡рд╛рдХреНрдпрд╛рдВрд╢ “рд╡рд╣ рдШрдЯрд┐рдпрд╛ рдирд┐рдпреЛрдЬрдХрд╝ рд╣реИ рдЬреЛ рдЕя┐╜...
┬а'рдЕрдкрдирд╛рдирд╛' рдХреНрд░рд┐рдпрд╛ рдХрд┐рд╕ рд╕рд░реНрд╡рдирд╛рдо рд╕реЗ рдмрдиреА рд╣реИ ?
(1) ┬а рдореЛрд╣рдирджрд╛рд╕ рдХрд░рдордЪрдВрдж рдЧрд╛рдВрдзреА рдФрд░ рдорд╛рдУ
( рдп ) рдХреЗ рдЕрдирдиреНрддрд░ рдореЗрдВ
( рд░ ) рдия┐╜...