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.
A train of length 260 m crosses another train of length 120 m running is opposite direction in 5 sec. If speed of longer train is 90 km/h, then in what ...
In which of the following group, Header and Footer can be found on the insert button while using PowerPoint?
Two places X and Y are 450 km apart. A truck leaves place X from place Y and at the same time another truck leaves place Y to X. Both the men meet 9 hou...
A and B are the roots of equation x2 - 13x + k = 0. If A - B = 5, what is the value of k?
The ratio between the speed of a bus and train is 15:27 respectively. Also, a car covered a distance of 720 km in 9 hours. The speed of the bus is three...
Manoj alone can complete 35% of a work alone in 7 days. Vipin is 20% less efficient than Manoj. Manoj and Vipin together started the work and left after...
Jaya goes to station at a speed of 6 km / hr and returns back at a speed of 9 km / hr. If she takes 5 hrs. in all. Find the distance between her home an...
A set (Set ‘P’) of 4 pipes can fill ______% of a tank in 5 minutes while another of set (Set ‘Q’) of 5 pipes can empty the 60% of completely fil...
Which of the following is the justification that aligns text on both margins of a document in Word?