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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:31+00:00 2026-06-04T04:15:31+00:00

I see SELECT EXISTS used a lot like: if db.query( SELECT EXISTS ( SELECT

  • 0

I see SELECT EXISTS used a lot like:

    if db.query("""
        SELECT EXISTS (
            SELECT 1 FROM checkout
            WHERE checkout_id = %s
        )
    """ % checkout_id).getresult()[0][0] == 't':

vs. what i prefer:

    if db.query("""
        SELECT 1 FROM checkout
        WHERE checkout_id = %s
        LIMIT 1
    """ % checkout_id).getresult():

Which one is preferred and why?

P.S. i am using Python and PosgreSQL.

cert=> explain SELECT EXISTS (SELECT 1 FROM checkout WHERE checkout_id = 3);
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 Result  (cost=4.03..4.03 rows=1 width=0)
   InitPlan
     ->  Index Scan using checkout_pkey on checkout  (cost=0.00..4.03 rows=1 width=0)
           Index Cond: (checkout_id = 3)
(4 rows)

cert=> explain SELECT 1 FROM checkout WHERE checkout_id = 3 limit 1;
                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Limit  (cost=0.00..4.03 rows=1 width=0)
   ->  Index Scan using checkout_pkey on checkout  (cost=0.00..4.03 rows=1 width=0)
         Index Cond: (checkout_id = 3)
(3 rows)

My point is, why getting a row from the result and check if it’s first column is true, if i can just check if there are any rows at all, meaning the same?

  • 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-04T04:15:32+00:00Added an answer on June 4, 2026 at 4:15 am

    PostgreSQL seems to clever enough to treat both statements alike as you can clearly see in your execution plans.

    My tests with a local table with ~150000 rows and ~100 selected out of that by the condition also show the same behaviour

    The bottomline is: it doesn’t really matter which one you use, but you should be aware that other DBMS might not behave in the same way.

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

Sidebar

Related Questions

When I attempt to do something like SELECT Max(ObjectId) FROM Objects; I see that
i want to select the most recent entry from a table and see if
When I execute a sql statement like Select ..., I can only see ...100%
DELETE FROM keywords WHERE NOT EXISTS (SELECT keywords_relations.k_id FROM keywords_relations WHERE keywords.k_id = keywords_relations.k_id)
I have this query: SELECT p.prodno AS id, proddesc AS label FROM product p
I'm trying to do the following: IF NOT EXISTS ( SELECT * FROM [tbl_web_company]
Using IE8's developer mode, I see that select lists are implemented using partial postbacks.
Is there a way to restrict what I see when I select the View
I see that when I select text in a text-box with the help of
I often see two styles, INSERT select and insert into select, what are the

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.