0 0 votes Write SQL command to find DepartmentID, EmployeeName from Employee table whose average salary is above 20000. Databases sql query databases + – rayhanrjt 1.8k views answer comment Share Follow Print See 1 comment 1 1 comment reply Vishal_kumar98 commented Jan 6, 2023 reply Follow flag SELECT DepartmentID, EmployeeName FROM Employee WHERE (SELECT AVG(Salary) FROM Employee) > 20000; 0 0 replyShare Please log in or register to add a comment.
1 1 vote This query will select the DepartmentID and EmployeeName columns from the Employee table and return only the rows where the average salary of all employees in the table is above $20000$ :- //SQL CODE:- SELECT DepartmentID, EmployeeName FROM Employee WHERE (SELECT AVG(Salary) FROM Employee) > 20000; This query will select the DepartmentID and EmployeeName columns from the Employee table, and return all rows where the average salary for the department is above 20000. The subquery in the WHERE clause calculates the average salary for each department by selecting the Salary column from the Employee table, grouped by DepartmentID. The outer query then filters the results to include only rows where the average salary is above $20000$ :- //SQL CODE:- SELECT DepartmentID, EmployeeName FROM Employee AS e WHERE (SELECT AVG(Salary) FROM Employee WHERE DepartmentID = e.DepartmentID) > 20000 Abhrajyoti00 answered Jan 6, 2023 • edited Jan 7, 2023 by Abhrajyoti00 Abhrajyoti00 comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments Abhrajyoti00 commented Jan 7, 2023 reply Follow flag @gatecse Sir, yes. That was wrongly written. 0 0 replyShare gatecse commented Jan 9, 2023 reply Follow flag SELECT DepartmentID FROM Employee group by DepartmentID having AVG(Salary) > 20000; Select EmployeeName, DepartmentID from Employee where DepartmentID in ( SELECT DepartmentID FROM Employee group by DepartmentID having AVG(Salary) > 20000; ) The first query will return the needed departmentIDs and the second one will return the employeenames in them. 2 2 replyShare Abhrajyoti00 commented Jan 9, 2023 reply Follow flag @gatecse Sir, oh yes group by clause is most appropriate for this query. Thanks for pointing out the mistake. 1 1 replyShare Please log in or register to add a comment.