Pergunta

I am using HR schema,Employees and Departments table. I was asked during the interview following query. Write a query to print department_name,department_no, count of all employees in corresponding department,but the condition is I can use only one attribute with group by clause.

        SQL> desc employees;
     Name                                                  Null?    Type
     ----------------------------------------------------- -------- ------------------------------------
     EMPLOYEE_ID                                           NOT NULL NUMBER(6)
     FIRST_NAME                                                     VARCHAR2(20)
     LAST_NAME                                             NOT NULL VARCHAR2(25)
     EMAIL                                                 NOT NULL VARCHAR2(25)
     PHONE_NUMBER                                                   VARCHAR2(20)
     HIRE_DATE                                             NOT NULL DATE
     JOB_ID                                                NOT NULL VARCHAR2(10)
     SALARY                                                         NUMBER(8,2)
     COMMISSION_PCT                                                 NUMBER(2,2)
     MANAGER_ID                                                     NUMBER(6)
     DEPARTMENT_ID                                                  NUMBER(4)

SQL> desc departments
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- ------------------------------------
 DEPARTMENT_ID                                         NOT NULL NUMBER(4)
 DEPARTMENT_NAME                                       NOT NULL VARCHAR2(30)
 MANAGER_ID                                                     NUMBER(6)
 LOCATION_ID                                                    NUMBER(4)

I tried various options to frame query, I used having clause with count(employee_id) but to no effect.Any help appreciated

Foi útil?

Solução

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 maxof 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;

SQL Fiddle.

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...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top