| SELECT |
| CASE |
| WHEN salary < 3000 THEN 'Low' |
| WHEN salary BETWEEN 3000 AND 7000 THEN 'Medium' |
| WHEN salary > 7000 THEN 'High' |
| ELSE 'Unknown' |
| END AS salary_level, |
| COUNT(*) AS num_employees |
| FROM |
| employees |
| GROUP BY |
| CASE |
| WHEN salary < 3000 THEN 'Low' |
| WHEN salary BETWEEN 3000 AND 7000 THEN 'Medium' |
| WHEN salary > 7000 THEN 'High' |
| ELSE 'Unknown' |
| END; |
这个查询会返回每个薪水级别的员工数量。注意,在SELECT和GROUP BY子句中都须要重复CASE WHEN语句,以确保精确的分组和结果。