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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:41:03+00:00 2026-06-06T21:41:03+00:00

How can I do such query in Postgres? IF (select count(*) from orders) >

  • 0

How can I do such query in Postgres?

IF (select count(*) from orders) > 0
THEN
  DELETE from orders
ELSE 
  INSERT INTO orders values (1,2,3);
  • 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-06T21:41:05+00:00Added an answer on June 6, 2026 at 9:41 pm
    DO
    $do$
    BEGIN
       IF EXISTS (SELECT FROM orders) THEN
          DELETE FROM orders;
       ELSE
          INSERT INTO orders VALUES (1,2,3);
       END IF;
    END
    $do$
    

    There are no procedural elements in standard SQL. The IF statement is part of the default procedural language PL/pgSQL. You need to create a function or execute an ad-hoc statement with the DO command.

    You need a semicolon (;) at the end of each statement in plpgsql (except for the final END).

    You need END IF; at the end of the IF statement.

    A sub-select must be surrounded by parentheses:

        IF (SELECT count(*) FROM orders) > 0 ...
    

    Or:

        IF (SELECT count(*) > 0 FROM orders) ...
    

    This is equivalent and much faster, though:

        IF EXISTS (SELECT FROM orders) ...
    

    Alternative

    The additional SELECT is not needed. This does the same, faster:

    DO
    $do$
    BEGIN
       DELETE FROM orders;
       IF NOT FOUND THEN
          INSERT INTO orders VALUES (1,2,3);
       END IF;
    END
    $do$
    

    Though unlikely, concurrent transactions writing to the same table may interfere. To be absolutely sure, write-lock the table in the same transaction before proceeding as demonstrated.

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

Sidebar

Related Questions

how can I execute such query in django: SELECT * FROM keywords_keyword WHERE id
Supposing a query such as: SELECT * FROM tableA; How can I prepend a_
I have such a query: SELECT ( SELECT name FROM text WHERE id =
Let's consider such query: SELECT * FROM ( SELECT YEAR(OrderDate) [Year], MONTH(OrderDate) [Month], SubTotal
In SQL, you could write a query such as: SELECT NAME, QUOTA, SALES FROM
How can I group my results in a query such as this: SELECT (
I have a query such as this: select a.id, a.color, a.shade from colors a
Say I have a query such as: SELECT * FROM Table_1 JOIN Table_2 ON
Is it possible to specify an XPath query such that it can count the
How can I test a LINQ Query such as the following: var vUser =

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.