Skip to content
Astera Technology

Why does max(a,b) hide an available reading when the other is NULL?

Resolve optional inputs before taking the highest recorded value.

Download complete case notes and answer keys ↓

Prepare the report input

Original report asks for the highest recorded value among two optional inputs, not an asserted peak of unseen data. SQLite multi-argument max returns NULL when any argument is NULL. Handle the missing-input branches explicitly; both missing remains NULL.

Every record and report rule below is original and fictional. Reproduce the setup in a fresh SQLite test database; input previews include every table in this small fixture.

Synthetic input: readings
idab
A5NULL (unknown or absent)
B28
CNULL (unknown or absent)NULL (unknown or absent)
Download editable CSV ↓
Complete CREATE and INSERT fixture
CREATE TABLE readings(id TEXT PRIMARY KEY,a INTEGER,b INTEGER); INSERT INTO readings VALUES('A',5,NULL),('B',2,8),('C',NULL,NULL);

Compare misleading and corrected results

Misleading SELECT: reproduce the defect
SELECT id,max(a,b) AS known_max FROM readings ORDER BY id;
Literal misleading result
idknown_max
ANULL (unknown or absent)
B8
CNULL (unknown or absent)
Download editable CSV ↓
Corrected SELECT
SELECT id,CASE WHEN a IS NULL THEN b WHEN b IS NULL THEN a ELSE max(a,b) END AS known_max FROM readings ORDER BY id;
Literal corrected result
idknown_max
A5
B8
CNULL (unknown or absent)
Download editable CSV ↓

A has known five and no b, so should show five. B shows eight; C remains unknown. Do not replace missing inputs by zero because valid recorded values may be negative.

Check a fresh boundary case

Change A’s only known reading to −2. It remains −2 rather than zero or NULL.

Recreate the original setup in another fresh database, then apply the change and corrected query below. For an empty result, the table displays a labelled empty-result marker rather than a data record.

Boundary change and corrected query
UPDATE readings SET a=-2 WHERE id='A';
SELECT id,CASE WHEN a IS NULL THEN b WHEN b IS NULL THEN a ELSE max(a,b) END AS known_max FROM readings ORDER BY id;
Literal boundary result
idknown_max
A-2
B8
CNULL (unknown or absent)
Download editable CSV ↓

What is A’s highest recorded boundary value?

Show the worked answer

−2. No second reading exists; the result describes the known input only.

Related resources