Count exam scores in fixed 20-point buckets, including empty buckets

Quick Overview

Write a SQL query that counts exam scores in five fixed 20-point ranges and returns every range, even the empty ones. It tests histogram bucketing, boundary handling for half-open and inclusive ranges, excluding NULL scores, and counting correctly after an outer join.

Count exam scores in fixed 20-point buckets, including empty buckets

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Bucket `score` into five 20-point ranges and return `bucket_id`, `bucket_label` and `student_count` (the number of students in that bucket). The buckets, as `bucket_id`: label, are 1 `[0,20)`, 2 `[20,40)`, 3 `[40,60)`, 4 `[60,80)` and 5 `[80,100]`. The last bucket has an inclusive upper bound, so `100` counts. ```hint Empty buckets Grouping the `scores` rows by themselves can only produce buckets that contain at least one score, but all five buckets must appear. ``` ### Input Table(s) `scores` | Column | Type | Meaning | |---|---|---| | score_id | INT | Exam result identifier | | student_name | VARCHAR(50) | Student who took the exam | | score | INT | Exam score from 0 to 100, may be NULL | Primary key: `score_id`. ### Output Contract - Columns, in this order: `bucket_id`, `bucket_label`, `student_count`. - Exactly 5 rows, one per bucket, even when a bucket is empty (its `student_count` is `0`). - `bucket_label` is the exact string shown above, for example `[40,60)`. - `student_count` is an integer: the number of `scores` rows whose non-NULL `score` falls in the bucket. - Order by `bucket_id` ascending. ### Example Input `scores`: | score_id | student_name | score | |---|---|---| | 1 | Ana | 15 | | 2 | Ben | 45 | | 3 | Cara | 55 | | 4 | Dan | 88 | | 5 | Eve | 100 | | 6 | Finn | 40 | Expected output: | bucket_id | bucket_label | student_count | |---|---|---| | 1 | [0,20) | 1 | | 2 | [20,40) | 0 | | 3 | [40,60) | 3 | | 4 | [60,80) | 0 | | 5 | [80,100] | 2 | Ana (15) is in bucket 1. Ben (45), Cara (55) and Finn (40, on the lower boundary of `[40,60)`) are in bucket 3. Dan (88) and Eve (100) are in bucket 5. Buckets 2 and 4 have no students, so they show `0`. ### Constraints and Clarifications - `score` is an integer from 0 to 100, or `NULL`. - Rows with a `NULL` score are excluded from every count. - A boundary score belongs to the bucket whose lower bound it matches, so `40` is in `[40,60)` and `80` is in `[80,100]`. - Each row is one exam result. Count rows; two rows with the same `student_name` both count. - `score_id` is unique.

Overview: Write a SQL query that counts exam scores in five fixed 20-point ranges and returns every range, even the empty ones. It tests histogram bucketing, boundary handling for half-open and inclusive ranges, excluding NULL scores, and counting correctly after an outer join.

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

Bucket score into five 20-point ranges and return bucket_id, bucket_label and student_count (the number of students in that bucket). The buckets, as bucket_id: label, are 1 [0,20), 2 [20,40), 3 [40,60), 4 [60,80) and 5 [80,100]. The last bucket has an inclusive upper bound, so 100 counts.

Input Table(s)

scores

ColumnTypeMeaning
score_idINTExam result identifier
student_nameVARCHAR(50)Student who took the exam
scoreINTExam score from 0 to 100, may be NULL

Primary key: score_id.

Output Contract

  • Columns, in this order: bucket_id , bucket_label , student_count .
  • Exactly 5 rows, one per bucket, even when a bucket is empty (its student_count is 0 ).
  • bucket_label is the exact string shown above, for example [40,60) .
  • student_count is an integer: the number of scores rows whose non-NULL score falls in the bucket.
  • Order by bucket_id ascending.

Example

Input scores:

score_idstudent_namescore
1Ana15
2Ben45
3Cara55
4Dan88
5Eve100
6Finn40

Expected output:

bucket_idbucket_labelstudent_count
1[0,20)1
2[20,40)0
3[40,60)3
4[60,80)0
5[80,100]2

Ana (15) is in bucket 1. Ben (45), Cara (55) and Finn (40, on the lower boundary of [40,60)) are in bucket 3. Dan (88) and Eve (100) are in bucket 5. Buckets 2 and 4 have no students, so they show 0.

Constraints and Clarifications

  • score is an integer from 0 to 100, or NULL .
  • Rows with a NULL score are excluded from every count.
  • A boundary score belongs to the bucket whose lower bound it matches, so 40 is in [40,60) and 80 is in [80,100] .
  • Each row is one exam result. Count rows; two rows with the same student_name both count.
  • score_id is unique.