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 8548085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:24:48+00:00 2026-06-11T13:24:48+00:00

SELECT commandid FROM results WHERE NOT EXISTS ( SELECT * FROM generate_series(0,119999) WHERE generate_series

  • 0
SELECT commandid 
FROM results 
WHERE NOT EXISTS (
    SELECT * 
    FROM generate_series(0,119999) 
    WHERE generate_series = results.commandid 
    );

I have a column in results of type int but various tests failed and were not added to the table. I would like to create a query that returns a list of commandid that are not found in results. I thought the above query would do what I wanted. However, it does not even work if I use a range that is outside the expected possible range of commandid (like negative numbers).

  • 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-06-11T13:24:49+00:00Added an answer on June 11, 2026 at 1:24 pm

    Given sample data:

    create table results ( commandid integer primary key);
    insert into results (commandid) select * from generate_series(1,1000);
    delete from results where random() < 0.20;
    

    This works:

    SELECT s.i AS missing_cmd
    FROM generate_series(0,1000) s(i)
    WHERE NOT EXISTS (SELECT 1 FROM results WHERE commandid = s.i);
    

    as does this alternative formulation:

    SELECT s.i AS missing_cmd
    FROM generate_series(0,1000) s(i)
    LEFT OUTER JOIN results ON (results.commandid = s.i) 
    WHERE results.commandid IS NULL;
    

    Both of the above appear to result in identical query plans in my tests, but you should compare with your data on your database using EXPLAIN ANALYZE to see which is best.

    Explanation

    Note that instead of NOT IN I’ve used NOT EXISTS with a subquery in one formulation, and an ordinary OUTER JOIN in the other. It’s much easier for the DB server to optimise these and it avoids the confusing issues that can arise with NULLs in NOT IN.

    I initially favoured the OUTER JOIN formulation, but at least in 9.1 with my test data the NOT EXISTS form optimizes to the same plan.

    Both will perform better than the NOT IN formulation below when the series is large, as in your case. NOT IN used to require Pg to do a linear search of the IN list for every tuple being tested, but examination of the query plan suggests Pg may be smart enough to hash it now. The NOT EXISTS (transformed into a JOIN by the query planner) and the JOIN work better.

    The NOT IN formulation is both confusing in the presence of NULL commandids and can be inefficient:

    SELECT s.i AS missing_cmd
    FROM generate_series(0,1000) s(i)
    WHERE s.i NOT IN (SELECT commandid FROM results);
    

    so I’d avoid it. With 1,000,000 rows the other two completed in 1.2 seconds and the NOT IN formulation ran CPU-bound until I got bored and cancelled it.

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

Sidebar

Related Questions

Please, help me with join results of commands (MS SQL): SELECT name,value FROM table1
I'm using this sql command to get column names : select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS
I am able to retrieve information from a SQLite database in PHP, but not
I have an sql command that is: SELECT * FROM `tattoos` ORDER BY id
$table = alabama; $sql = SELECT distinct zip FROM {$table} where zip is not
I run this command : select * from LIST where JCODE = 8 and
Im trying from postgres console this command: select sim.id as idsim, num.id as idnum
for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I am creating a file from a SELECT query using sqlplus with SPOOL command.
MySqlCommand command = connection.CreateCommand(); command.CommandText = string.Format(SELECT * FROM characters WHERE account_id = '{0}',

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.