Your question is SQL NULL Handling. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Xylem builds water-treatment equipment, and a field-engineer dashboard queries sensor readings from pump stations. The query below is supposed to flag stations with a missing or offline flow-rate reading, but the numbers don't match what field engineers see on site.
CREATE TABLE pump_stations (
station_id INT PRIMARY KEY,
pump_name VARCHAR(80),
location_note VARCHAR(120) -- nullable, filled in only for stations with special access instructions
);
CREATE TABLE sensor_readings (
reading_id INT PRIMARY KEY,
station_id INT,
flow_rate_gpm NUMERIC(8,2), -- nullable when a sensor fails to report
status VARCHAR(20), -- nullable when a station has never checked in
recorded_at TIMESTAMP
);
SELECT
ps.pump_name || ' - ' || ps.location_note AS display_label,
AVG(sr.flow_rate_gpm) AS avg_flow_rate,
COUNT(*) AS reading_count
FROM pump_stations ps
JOIN sensor_readings sr ON sr.station_id = ps.station_id
WHERE sr.flow_rate_gpm = NULL
OR sr.status <> 'Offline'
GROUP BY ps.pump_name, ps.location_note;
What is wrong with this query's handling of NULLs, and how would you fix it?