I have two tables:
Companies: (id, name)
Workers: (id, name, country)
I would like to get all companies and sort them by numbers of employees from a given country.
For a query looking for companies that have more workers from the USA, the result should give:
#workers from USA | company id | company name
----------------------------------------------
90 6 foo corp
45 9 bar corp
0 3 foobar corp
I tried:
select
count(w.country='USA') as mycount,
w.company_id,
c.company_name
from
companies c
left join workers w on
c.id=w.company_id
group by
w.company_id,
c.company_name
order by mycount desc;
But that's counting all workers regardless of their countries.
Any ideas?