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.
Pipes P and Q can fill a tank in 12 hours and 15 hours respectively, while a third pipe C can empty the tank in 24 hours. If all three pipes are opened ...
Pipe βAβ and pipe βBβ can fill a cistern in 20 minutes and 18 minutes respectively. Pipe βCβ alone can empty the cistern in 12 minutes. If a...
An inlet pipe can fill an empty tank in 24 minutes whereas an outlet pipe can empty the fully filled tank in 36 minutes. Find the time taken by both the...
Pipe βAβ can fill a tank in 60 hours whereas leak βAβ can empty it in 70 hours. If they both operate along with pipe βBβ, then the given tan...
Pipe βAβ can fill a tank in 20 hours while pipe βBβ takes 32 hours to empty it. Pipe βAβ is opened alone when the tank was empty and after 1...
The efficiencies of two inlet pipes, 'P' and 'Q', are in the ratio of 3:5, respectively. When both pipes are open together, the tank is filled in 16 hou...
Pipe ‘A’ and pipe ‘B’, together can fill 20% of a tank in 7 hours while pipe ‘C’ takes 40 hours to empty it. ...
The time taken by pipe B3 alone to fill an empty tank completely is half of the time taken by pipe B1 alone to fill the same tank completely. Pipe B2 al...
A tank is 3/4 full of water. After removing 30 litres of water, it becomes 3/8 full. Find the full capacity of the tank.
A tank is filled with three pipes A, B, and C. Pipe A takes 18 hr. to fill 60% of the tank and Pipe B takes 8 hr. to fill 40% of the tank. Pipe A and Pi...