Example
Employees table
| ID | MANAGER_ID | SAL | DEPT_ID |
|---|---|---|---|
| 1 | 8 | 3000 | 20 |
| 2 | 8 | 3000 | 20 |
| 3 | 1 | 800 | 20 |
| 4 | 7 | 950 | 30 |
| 5 | 2 | 1100 | 20 |
| 6 | 9 | 1300 | 10 |
| 7 | 10 | 2850 | 30 |
| 8 | 10 | 2975 | 20 |
| 9 | 10 | 2450 | 10 |
| 10 | NULL | 5000 | 10 |
Count the number of employees greater than two and group by department
SELECT COUNT(ID) EMP_NO, DEPT_ID
FROM EMPLOYEES
WHERE MANAGER_ID IS NOT NULL
GROUP BY DEPT_ID
HAVING COUNT(ID) > 2;
Results
| EMP_NO | DEPT_ID |
|---|---|
| 5 | 20 |
Select the salary with having
SELECT ID, SAL
FROM EMPLOYEES
GROUP BY ID, SAL
HAVING SUM(SAL) >= 3000;
Results
| ID | SAL |
|---|---|
| 1 | 3000.00 |
| 2 | 3000.00 |
| 10 | 5000.00 |