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.
The average price of two laptops, namely 'D' and 'E' is Rs. 65,500 whereas the average price of laptops 'E' and 'F' is Rs. 70,000. If the average price ...
Four numbers are arranged in ascending order. When the 1st two numbers are increased by the number equal to their position, and the last two numbers (i....
The average age of 32 children and their teacher's age are 28 years. If the teacher's age is excluded, the average reduces by 1. What is the teacher's age?
Average of four numbers is 20. Sum of smallest and largest number is 50. If difference between other two numbers is 2, then find the square of second sm...
Sachin Tendulkar's average runs over 40 innings was 52. During his subsequent four innings, he scored 55, 42, 0, and 60 runs. To increase his average to...
The average salary of 24 employees is recorded as ₹ 16,584. If the salary of an employee with value ₹14,526 was entered Wrongly as ₹ 41,238, then ...
There are three numbers 'a', 'b' and 'c' such that average of 'a' and 'b' is 14, average of 'b' and 'c' is 16 and average of 'a' and 'c' is 30. Find the...
The average mark obtained by 120 candidates in a certain examination is 50. If the average marks of passed candidates are 55 and that of the failed cand...
Average of 8 numbers is 50. If average of first four and last two numbers is 40 and 35, respectively then find the fifth number given that ratio of fift...
Fifteen numbers have an average of 70. The average of the first seven is 60, and the average of the last five is 63. The eighth number is 50% of the ni...