简体   繁体   中英

MySQL LEFT JOIN with GROUP BY and WHERE clause query

Tables that look like bellow

SQL 表

How to write SQL Query to read all department names with an employee count salary > 1000, if a department not found in employee we required to show as zero counts in output.

You can use left join and aggregation:

select d.name, count(e.id)
from departments d left join
     employees e
     on e.department_id = d.id and e.salary >= 10000
group by d.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM