- This topic is empty.
-
AuthorPosts
-
March 7, 2026 at 1:05 pm #6179



While working through the DESE database exercises in
CS50βs Introduction to Databases with SQL, one task asks us to identify:The school district(s) with the least number of pupils, reporting only their names.
This corresponds to 9.sql in the assignment.
During the process, the following query was attempted:
SELECT districts.name FROM districts WHERE expenditures.name = (SELECT MIN(expenditures.pupils) FROM expenditures);At first glance, the idea behind the query is reasonable: use a subquery with
MIN()to find the smallest number of pupils. However, the query contains several issues.Letβs examine them and correct them step-by-step.
π― Understanding the Schema
From the DESE database structure:
districts table
column meaning id district identifier name district name expenditures table
column meaning district_id district reference pupils number of pupils in the district per_pupil_expenditure spending per student Relationship:
districts.id β expenditures.district_idThis tells us that the number of pupils belongs to the expenditures table, not the districts table.
π¨ Issue 1: Referencing a Table That Isnβt in the Query
The attempted query used:
WHERE expenditures.nameBut the
expenditurestable was never included in theFROMclause.SQL therefore has no idea where that table comes from.
Whenever you reference columns from another table, you must JOIN that table first.
π¨ Issue 2: Using a Column That Doesnβt Exist
The query referenced:
expenditures.nameBut the
expenditurestable has nonamecolumn.The district name is stored in:
districts.name
π¨ Issue 3: Comparing Incompatible Values
The attempted comparison was essentially:
district name = minimum pupilsThis compares:
text = numberwhich makes no logical sense.
Instead we must compare:
pupil count = minimum pupil count
π§ Correct Logical Approach
To answer the question properly, we need to:
- Join districts with expenditures
- Determine the smallest pupil count
- Return the district(s) whose pupil count matches that value
β Correct Query
SELECT districts.name FROM districts JOIN expenditures ON districts.id = expenditures.district_id WHERE expenditures.pupils = ( SELECT MIN(pupils) FROM expenditures );
π How the Query Works
Step 1: Find the Minimum
SELECT MIN(pupils) FROM expenditures;Example result:
230
Step 2: Match Districts with That Value
The outer query then finds all districts whose pupil count equals that minimum.
WHERE expenditures.pupils = 230This returns the district name(s).
π Logical Flow
Find smallest pupil count β Match districts with that value β Return district name(s)
π Key SQL Concept Learned
This exercise introduces a powerful SQL pattern:
WHERE column = (SELECT MIN(column))This pattern is commonly used to find:
- the cheapest product
- the smallest city by population
- the lowest-performing branch
- the minimum sales value
π Final Takeaway
When a problem asks for rows with the smallest or largest value, using a subquery with
MIN()orMAX()is often the safest and most accurate approach.It ensures that all matching rows are returned, even when multiple records share the same minimum value.
-
AuthorPosts
- You must be logged in to reply to this topic.
