Skip to content
Astera Technology

Why does a sorted shortlist omit the highest-priority record?

Sort the full candidate set before choosing its top two.

Download complete case notes and answer keys ↓

Prepare the report input

Original shortlist selects two highest numeric priorities, tie by id. The bad subquery first takes two lowest ids and only then sorts that truncated set. A correct outer sort cannot restore C once it was discarded.

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: items
idpriority
A1
B2
C9
Download editable CSV ↓
Complete CREATE and INSERT fixture
CREATE TABLE items(id TEXT PRIMARY KEY,priority INTEGER NOT NULL); INSERT INTO items VALUES('A',1),('B',2),('C',9);

Compare misleading and corrected results

Misleading SELECT: reproduce the defect
SELECT id,priority FROM (SELECT id,priority FROM items ORDER BY id LIMIT 2) ORDER BY priority DESC,id;
Literal misleading result
idpriority
B2
A1
Download editable CSV ↓
Corrected SELECT
SELECT id,priority FROM items ORDER BY priority DESC,id LIMIT 2;
Literal corrected result
idpriority
C9
B2
Download editable CSV ↓

Bad output B (priority2), A (priority1) is sorted within the wrong subset. Correct output C (priority9), B (priority2) comes from ordering all three before LIMIT.

Check a fresh boundary case

Add D at priority ten. Correct leaders become D at priority10 and C at priority9.

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
INSERT INTO items VALUES('D',10);
SELECT id,priority FROM items ORDER BY priority DESC,id LIMIT 2;
Literal boundary result
idpriority
D10
C9
Download editable CSV ↓

Can sorting A and B afterward bring C back?

Show the worked answer

No. C is outside that subquery result. Selection must follow the full priority sort.

Related resources