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.
If a sum of money is to be divided among A, B, C such that A’s share is equal to thrice B’s share and B’s share is 8 times C’s share then their ...
Amit and Bhanu put Rs. 14,400 and Rs. 19,200, respectively, into a startup. After 4 months, Chinu joined them with an investment of Rs. 10,800. If by th...
If a sum of money is to be divided among A, B, C such that A’s share is equal to twice B’s share and B’s share is 8 times C’s share then their s...
Paras and Punit started a business by investing Rs. 18,000 and Rs. 24,000 respectively. Paras also worked as the active manager and for that he is enti...
‘A’ started a business by investing Rs. 2000. Three months later, ‘B’ joined by investing Rs. ‘x’. If at the end of the year ‘B’ receive...
- X and Y opened a joint venture by investing Rs. 1200 and Rs. ‘k’ respectively. Y left the business after 6 months. If X got Rs. 5400 from the total pro...
'Pawan' and 'Qureshi' initiated a business venture with investments in the ratio of 2:3, respectively. After 8 months, 'Rita' became a partner in the bu...
The contributions made by A and B are in the ratio of 6:5. If 12% of total profit is donated and A gets 1320 as his share of profit, what is the total p...
P and Q invested in a partnership with Rs.400 and Rs.700 respectively. After 4 months Q added Rs. 'x' more. If at the end of the year profit share of P ...
Makhan Lal started a business with the capital investing Rs 18,400. After 6 months Arvind Lal invested Rs 16200. At the end of one year they made a prof...