SQLite

View Ticket
Login
Ticket Hash: 752e1646fcc7b649184e49783577a7feb5f7fc9c
Title: Wrong result if DISTINCT used on subquery which uses ORDER BY.
Status: Fixed Type: Code_Defect
Severity: Severe Priority: Immediate
Subsystem: Unknown Resolution: Open
Last Modified: 2011-02-11 20:54:35
Version Found In:
Description:
Wrong result if DISTINCT used on subquery which uses ORDER BY.
CREATE TABLE "test" ("letter" VARCHAR(1) PRIMARY KEY, "number" INTEGER NOT NULL);

INSERT INTO "test" ("letter", "number") VALUES('b', 1); INSERT INTO "test" ("letter", "number") VALUES('a', 2); INSERT INTO "test" ("letter", "number") VALUES('c', 2);

Then the following query

SELECT "letter", "number" FROM "test" ORDER BY "letter", "number" LIMIT 1;

returns "a|2", the second row from the results as you would expect. However, if this query is used as the subquery of a SELECT DISTINCT

SELECT DISTINCT "number" FROM (SELECT "letter", "number" FROM "test" ORDER BY "letter", "number" LIMIT 1) AS "test";

then it returns "1" instead of the expected "2".

If query optimizations are disabled, the SELECT DISTINCT returns "2".

This is most likely an issue with the query flattener.