Since you're getting two values from the same table, and one is the key (or more importantly, unique), you can just apply an aggregate function to the other one:
select d.department_id,
min(d.department_name) as department_name,
count(e.employee_id)
from departments d
left join employees e on e.department_id = d.department_id
group by d.department_id;
SQL Fiddle. Either min
or max
of the name would work, and you only need to group by the ID. You could do it the other way around, but name is less likely to be unique.
Not sure if it would count, but as an exercise in futility, you could also just concatenate the values:
select d.department_id ||' '||d.department_name,
count(e.employee_id)
from departments d
left join employees e on e.department_id = d.department_id
group by d.department_id ||' '|| d.department_name;
You can lpad
the department_id
to maintain the illusion that they are separate columns, sort of. (It doesn't show up well as a Fiddle though).
Pointless hack, but then it's an interview question so maybe that's appropriate, and seems to just about fulfil the wording you used. Depending on how 'attribute' is defined - I'm taking it to mean a single expression here, which may be a stretch...