SQL Exercise: In ASC order, list managers and count their employees

SQL employee Database: Exercise-96 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

96. From the following table, write a SQL query to create a list of the managers and the number of employees they supervise. Sort the result set in ascending order on manager. Return manager ID and number of employees under them.

Sample table: employees

Sample Solution:

SELECT w.manager_id,
FROM employees w,
     employees m
WHERE w.manager_id = m.emp_id
GROUP BY w.manager_id
ORDER BY w.manager_id ASC;

Sample Output:

  manager_id | count
      65646 |     2
      66928 |     5
      67832 |     1
      67858 |     1
      68319 |     3
      69062 |     1
(6 rows)


The said query in SQL that retrieves the manager ID along with the number of employees who report to each manager from the 'employees' table.

This creates a self-join, where each row in the 'employees' table is compared with every other row in the same table to identify which employees report to each manager.

The WHERE clause filters the rows where the manager ID of the 'employees' table aliased as "w" matches the employee ID of the 'employees' table aliased as "m", indicating that the employee in the "w" row reports to the manager in the "m" row.

The GROUP BY clause groups the results by the manager ID, so that the count of employees can be calculated for each manager and the ORDER BY clause sorts the results in ascending order by the manager ID.

Relational Algebra Expression:

Relational Algebra Expression: List the manager no and the number of employees working for those managers in ascending order on manager id.

Relational Algebra Tree:

Relational Algebra Tree: List the manager no and the number of employees working for those managers in ascending order on manager id.

Practice Online

Sample Database: employee

employee database structure

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: List employees salary within min_salary and max_salary.
Next SQL Exercise: Number of employee for each job in each department.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Grouped LIMIT in PostgreSQL: Show the first N rows for each group?

db=# SELECT * FROM xxx;
 id | section_id | name
  1 |          1 | A
  2 |          1 | B
  3 |          1 | C
  4 |          1 | D
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
  8 |          2 | H
(8 rows)

I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:

id | section_id | name
  1 |          1 | A
  2 |          1 | B
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
(5 rows)

PostgreSQL v9.3 you can do a lateral join

select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer
join lateral (
    select * from t t_inner
    where t_inner.section_id = t_outer.section_id
    order by t_inner.name
    limit 2
) t_top on true
order by t_outer.section_id;

Database: PostgreSQL

Ref: https://bit.ly/3AfYwZI


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook