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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:25:11+00:00 2026-05-27T13:25:11+00:00

postgreSQL question…I have an update query below which updates a column with the results

  • 0

postgreSQL question…I have an update query below which updates a column with the results from a subquery, however in some cases the subquery will return null which throws the ‘not null’ constraint on the column, how can I get it to NOT update if the subquery returns null?

I have tried EXISTS but this only seems to work on a WHERE clause?

UPDATE user_stats as stats
SET ave_price = (
    SELECT AVG(l.price)
    FROM lengths as l, user_sessions as us
    WHERE l.product_type = 'car'
    AND l.session_id = us.session_id
    AND stats.user_id = us.user_id
)
  • 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-27T13:25:11+00:00Added an answer on May 27, 2026 at 1:25 pm

    coalesce, nvl, ifnull in most db engines will do a conditional statement that says take the first non-null value in the string in this case when the subselect returns null it will set the ave_price = to itself.

    UPDATE user_stats as stats
    SET ave_price = coalesce((
        SELECT AVG(l.price)
        FROM lengths as l, user_sessions as us
        WHERE l.product_type = 'car'
        AND l.session_id = us.session_id
        AND stats.user_id = us.user_id
    ),ave_price)
    

    This doesn’t prevent the udpate as requested, but it has a similar effect on the data.

    For more info on coalesce see: PostgreSQL

    To actually prevent the update you would needto add a where clause on the update and re-execute the sub query such as:

        UPDATE user_stats as stats
        SET ave_price = (
            SELECT AVG(l.price)
            FROM lengths as l, user_sessions as us
            WHERE l.product_type = 'car'
            AND l.session_id = us.session_id
            AND stats.user_id = us.user_id)
    WHERE (SELECT AVG(l.price)
            FROM lengths as l, user_sessions as us
            WHERE l.product_type = 'car'
            AND l.session_id = us.session_id
            AND stats.user_id = us.user_id) is not null
    

    Logically executing the subquery twice would impact performance twice; whereas the coalesce only requires execution once. There’s always multiple ways to do things and depending on requirements, one must choose which option serves them best.

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

Sidebar

Related Questions

Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
I'm new to postgreSQL and I have a simple question: I'm trying to create
I have the same question as #1895500 , but with PostgreSQL not MySQL. How
I have a question regarding postgresql sequences. For instance, for bigserial datatype, is it
i have a LAPP (linux, apache, postgresql and php) environment, but the question is
I have a little silly question. I have installed a PostgreSQL DB Server, but
I have a table in PostgreSQL with the following structure & data: Question |
Although this question fancies PostgreSQL, it is still a general DB question. I have
I have a question about a particular query I'd like to execute against a
I am a newbie and have some question on using jdbc with java: What

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.