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

  • Home
  • SEARCH
  • 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 4113364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:18:53+00:00 2026-05-20T22:18:53+00:00

I have an SQL script (currently running against SQLite, but it should probably work

  • 0

I have an SQL script (currently running against SQLite, but it should probably work against any DB engine) that uses the same subquery twice, and since it might be fetching a lot of records (the table has a couple of million rows) I’d like to only call it once.

A shortened pseudo-version of the query looks like this:

SELECT * FROM
    ([the subquery, returns a column of ids]) AS sq
[a couple of joins, that fetches things from other tables based on the ids]
WHERE thisorthat NOT IN ([the subquery again])

I tried just using the name (sq) in various ways (with/without parenthesis, with/without naming the column of sq etc) but to no avail.

Do I really have to repeat this subquery?

Clarification:
I am doing this in python and sqlite as a small demo of what can be done, but I would like my solution to scale as well as possible with as little modification as possible. In the real situation, the database will have a couple of million rows, but in my example there is just 10 rows with dummy data. Thus, code that would be well optimized on for example MySQL is absolutely good enough – it doesn’t have to be optimized specifically for SQLite. But as I said, the less modification needed, the better.

  • 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-20T22:18:54+00:00Added an answer on May 20, 2026 at 10:18 pm

    There is a WITH clause in standard SQL, however, I don’t know if it is supported by SQLlite – though of course worth a try:

    WITH mySubQuery AS
    (
      [the subquery code]
    )
    
    SELECT * FROM
        mySubQuery AS sq
        [a couple of joins, that fetches things from other tables based on the ids]
    WHERE thisorthat NOT IN (mySubQuery)
    

    That said, what you do here will likely be horribly slow for any data set that is more than a few thousand rows, so I’d try to remodel it if possible – NOT IN should be avoided in general, especially if you also have a couple of joins.

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

Sidebar

Related Questions

I have a .sql script file that I want to run once every 24
I have a SQL script that populates a temp column and then drops the
I have the Execute SQL Script package that contains the script to insert about
I have a noobie question: I need to write SQL script that returns 3
I have a SQL 2008 job that does database backups using a Powershell script.
I am currently trying to script a job on SQL Server 2005 that will
I have a web app that uses some backend servers (UNC, HTTP and SQL).
I have a SQL Server that's currently x86, and I'd like to upgrade it
I have a SQL 2005 instance that runs a job that uses a Powershell
I have an SQL script which creates a scheduled event: CREATE EVENT Daily_1200PM SCHEDULE

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.