S
S
Stazhor2018-04-09 13:43:49
MySQL
Stazhor, 2018-04-09 13:43:49

Compose correct SQL query?

5aa05f2de9c25210833360.png
There are two such tables. It is necessary to display the names of departments in which the number of employees does not exceed 3 people.
select department_id
from employee
group by department_id
having count(*) <= 3 // Display the IDs of these departments, but how do we display the department names?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-04-09
@Stazhor

select id, name
from department
where id in (
  select department_id
  from employee
  group by department_id
  having count(*) <= 3
)

It is better, of course, to break into two requests.

D
d-stream, 2018-04-09
@d-stream

use join

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question