Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3845170
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:12:11+00:00 2026-05-19T16:12:11+00:00

I just wondered if anyone has run into a case in SQLite (3.7.4) where

  • 0

I just wondered if anyone has run into a case in SQLite (3.7.4) where a query would return one set of results, and when it becomes a subquery the results are completely different? I found the problem in a more complex query, but here’s a simpler example that demonstrates the same behavior:

Database setup:

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);

Initial query:

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

This returns a|2, the second row from the results as you would expect given that we’re sorting on the letter then the number. However, here’s what I did not expect:

Initial query as a subquery:

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

This returns 1, which is not at all what I expected. What I expected to see is 2. My understanding of how a subquery works is that it should return the same results as if the inner query was materialized, and the outer query was applied against those results (even though I realize that databases go to extreme lengths not to materialize results until necessary).

Is my assumption incorrect? I tested the same query in PostgreSQL and MySQL and it worked as I expected (i.e. it returned 2). What it looks like to me is that I’ve hit a bug in how SQLite collapses subqueries, but I’m not sure.

Just to reiterate, the above example is simplified from what I’m actually doing. I’m not just using DISTINCT on a subquery that returns a single row, but rather it returns many rows, some of which have the same value for a column hence my need for DISTINCT. The above example is the simplest way I could think of to demonstrate what’s happening.

EDIT: I was able to disable the incorrect subquery folding by adding OFFSET 0 to the inner query, e.g.

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

I’ll be reporting this as a bug through the SQLite mailing list, and this as a work-around.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:12:11+00:00Added an answer on May 19, 2026 at 4:12 pm

    I can verify that it happens with SQLite add-on for Firefox as well.

    If it is any consolation, this form works:

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

    I believe the SQLite spec ignores the LIMIT clause in inner queries and migrates it outside. Without the limit:

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

    It returns

    1
    2
    (2 rows)
    

    Interesting to note that this also returns the correct results

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

    The two plans can be compared using EXPLAIN.
    DESCRIBE is adding a lot of operations, in-lining and optimizing the inner query (incorrectly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if anyone has any tricks (or tools) they use to visualize joins.
I just wondered if anyone knew of a simple script available that will do
I think I've found a bug in safari. I wondered if anyone has encountered
Has anyone integrated ELMAH into their SharePoint environment? I suppose it's possible as it's
Just wondered if anyone could point me in the right direction of storing multiple
Just wondered if anyone knew a way to animate leaves falling in the wind?
Just wondered if anyone could point me in the right direction here? I'm trying
Just wondered if the above is true or whether reference assignments are split into
Just wondered if any LINQ guru might be able to shed light on how
I just wondered, why most Delphi examples use FillChar() to initialize records. type TFoo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.