Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select dept names who have more than 2 employees whose salary is greater than 1000

Tags:

sql

How would do the following in SQL

"select dept names who have more than 2 employees whose salary is greater than 1000" ?

DeptId DeptName
------ --------
1          one
2          two
3        three

EmpId DeptId Salary
----- ------ ------
121      1    2000
122      1    2000
123      1    5000
124      1    4000
131      2    2000
132      2    6000
133      2    1000
134      2    1000
125      3    1000
126      3   20000


RESULT: one
like image 235
Anshul Avatar asked Nov 27 '22 18:11

Anshul


1 Answers

How about something like this?

SELECT D.DeptName FROM
Department D WHERE (SELECT COUNT(*) 
                    FROM Employee E 
                    WHERE E.DeptID = D.DeptID AND
                            E.Salary > 1000) > 2
like image 109
John Petrak Avatar answered Dec 10 '22 02:12

John Petrak