хотелось бы посмотреть на версию mysql 5.7
Ну например так:
SELECT department.name as department_name,
employee.name
FROM employee
JOIN department ON employee.department_id = department.id
WHERE 2 > ( SELECT COUNT(*)
FROM employee emp
WHERE emp.department_id = employee.department_id
AND emp.id < employee.id )
Ну или так:
SELECT department.name as department_name,
subquery.name
FROM department
JOIN ( SELECT employee.*,
@row_number := CASE WHEN @department = department_id
THEN @row_number + 1
ELSE 1
END rownumber,
@department := department_id
FROM employee
CROSS JOIN ( SELECT @department:=0, @row_number:=0 ) variables
ORDER BY department_id, id ) subquery ON subquery.department_id = department.id
WHERE subquery.rownumber <= 2
fiddle