Summarizing Data · COUNT(*) · COUNT(col) · COUNT(DISTINCT col)
3. Counting managers
Concept: COUNT(*) · COUNT(col) · COUNT(DISTINCT col)show
COUNT comes in three flavours. COUNT(*) counts rows. COUNT(col) counts only the rows where col has a value (it skips NULLs). COUNT(DISTINCT col) counts how many different values col has, so repeats count once.
Example (different data):
SELECT COUNT(*) AS loans, COUNT(returned_on) AS returned, COUNT(DISTINCT member_id) AS members FROM library_loans;
Your task
HR is planning a management training and needs three numbers: the headcount, how many people report to someone, and how many different people actually manage others.
Return one row with headcount (number of employees), with_manager (employees whose manager_id is filled in) and managers (number of different people who appear as someone's manager_id).
Solution unlocks after 3 more tries
Starting Postgres in your browser…
query.sql
Output
Write a query and press Run Query (Ctrl/⌘ + Enter).