Interview Question Bank · CASE buckets · GROUP BY
1. Grade distribution by score band
EasyEducationQuestion #217
Concept: CASE buckets · GROUP BYshow
CASE turns a number into a label: Postgres checks the WHEN branches top to bottom and uses the first one that's true, so listing thresholds from highest to lowest (>= 90, then >= 80, ...) needs only one bound per branch. Ranges like BETWEEN 80 AND 89 leave gaps that decimal values fall through. A NULL fails every WHEN and lands in ELSE, so filter NULLs out first if they don't belong.
Example (different data):
SELECT CASE WHEN minutes >= 60 THEN 'long'
WHEN minutes >= 20 THEN 'medium'
ELSE 'short' END AS length_band,
COUNT(*) AS calls
FROM support_calls
WHERE minutes IS NOT NULL
GROUP BY 1
ORDER BY 1;Your task
Brightpath Academy, an online school, is reviewing grading across all its courses and wants to see how posted final scores spread across the usual bands.
Using only enrollments with a final_score, return grade_band ('A' for 90 and above, 'B' for 80 up to but not including 90, 'C' for 70 to under 80, 'D' for 60 to under 70, 'F' below 60) and enrollments (how many scores fall in that band). Sort by grade_band.
Starting Postgres in your browser…
Checking your access…