Count Bank Accounts in Each Income Category, Including Empty Categories

Quick Overview

A SQL question that asks you to count bank accounts in three monthly income bands and return every band, including bands with no accounts. It tests CASE bucketing with inclusive and strict boundaries, and keeping empty groups in a grouped result.

Count Bank Accounts in Each Income Category, Including Empty Categories

Company: McKinsey

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Count the bank accounts in each income category, using these definitions: - `low_income`: monthly income strictly less than 25000 - `medium_income`: monthly income in the inclusive range [25000, 60000] - `high_income`: monthly income strictly greater than 60000 All three categories must appear in the result, with a count of 0 when a category has no accounts. ```hint Empty categories A plain `GROUP BY` over the accounts only produces groups that contain at least one row. ``` ### Input Table(s) `bank_accounts` | Column | Type | Meaning | |---|---|---| | account_id | int | Bank account identifier | | monthly_income | int | Monthly income for the account | Primary key: `account_id`. Each row holds the monthly income for one bank account. ### Output Contract - Columns, in this order: `income_category` (text, one of `low_income`, `medium_income`, `high_income`) and `accounts_count` (integer). - Exactly three rows, one per category, including categories with a count of 0. - Order the rows `low_income`, `medium_income`, `high_income`. `income_category` is unique, so this order is fully determined. - No rounding. ### Example `bank_accounts` | account_id | monthly_income | |---|---| | 1 | 15320 | | 2 | 98210 | | 3 | 45100 | | 4 | 67250 | | 5 | 32000 | Expected result: | income_category | accounts_count | |---|---| | low_income | 1 | | medium_income | 2 | | high_income | 2 | Account 1 (15320) is the only low-income account. Accounts 3 (45100) and 5 (32000) fall in the medium range, and accounts 2 (98210) and 4 (67250) are above 60000. A second input that tests the boundaries and an empty category: | account_id | monthly_income | |---|---| | 10 | 25000 | | 11 | 60000 | | 12 | 60001 | Expected result: | income_category | accounts_count | |---|---| | low_income | 0 | | medium_income | 2 | | high_income | 1 | 25000 and 60000 are both inside the inclusive medium range, 60001 is high, and no account is below 25000, so `low_income` still appears with 0. ### Constraints and Clarifications - `account_id` is unique, so each account is counted once. - The boundaries 25000 and 60000 belong to `medium_income`. - An account whose `monthly_income` is NULL fits none of the three definitions and is not counted in any category. - If the table is empty, return the three categories, each with 0.

Overview: A SQL question that asks you to count bank accounts in three monthly income bands and return every band, including bands with no accounts. It tests CASE bucketing with inclusive and strict boundaries, and keeping empty groups in a grouped result.

McKinsey logo
McKinsey
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Count the bank accounts in each income category, using these definitions:

  • low_income : monthly income strictly less than 25000
  • medium_income : monthly income in the inclusive range [25000, 60000]
  • high_income : monthly income strictly greater than 60000

All three categories must appear in the result, with a count of 0 when a category has no accounts.

Input Table(s)

bank_accounts

ColumnTypeMeaning
account_idintBank account identifier
monthly_incomeintMonthly income for the account

Primary key: account_id. Each row holds the monthly income for one bank account.

Output Contract

  • Columns, in this order: income_category (text, one of low_income , medium_income , high_income ) and accounts_count (integer).
  • Exactly three rows, one per category, including categories with a count of 0.
  • Order the rows low_income , medium_income , high_income . income_category is unique, so this order is fully determined.
  • No rounding.

Example

bank_accounts

account_idmonthly_income
115320
298210
345100
467250
532000

Expected result:

income_categoryaccounts_count
low_income1
medium_income2
high_income2

Account 1 (15320) is the only low-income account. Accounts 3 (45100) and 5 (32000) fall in the medium range, and accounts 2 (98210) and 4 (67250) are above 60000.

A second input that tests the boundaries and an empty category:

account_idmonthly_income
1025000
1160000
1260001

Expected result:

income_categoryaccounts_count
low_income0
medium_income2
high_income1

25000 and 60000 are both inside the inclusive medium range, 60001 is high, and no account is below 25000, so low_income still appears with 0.

Constraints and Clarifications

  • account_id is unique, so each account is counted once.
  • The boundaries 25000 and 60000 belong to medium_income .
  • An account whose monthly_income is NULL fits none of the three definitions and is not counted in any category.
  • If the table is empty, return the three categories, each with 0.