Search code examples
sqlsqlitesubquerysql-limit

Is there a way to limit a query based on the result of another in sqlite?


I'm searching for something similar to this question, but for sqlite3. Is this possible?


Solution

  • From SELECT/The LIMIT clause:

    Any scalar expression may be used in the LIMIT clause, so long as it evaluates to an integer or a value that can be losslessly converted to an integer...

    You can use directly the integer result of a subquery in the LIMIT clause:

    SELECT * 
    FROM table1
    ORDER BY id
    LIMIT (SELECT COUNT(*) FROM table2);
    

    See a simplified demo.