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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:55:52+00:00 2026-05-15T23:55:52+00:00

How can I write an SQL query that returns a record only if ALL

  • 0

How can I write an SQL query that returns a record only if ALL of the associated records in a joined table satisfy some condition.

For example, if A has many B, I want to SELECT * FROM A WHERE all related B’s for a given A have B.some_val > value

I know this is probably a pretty basic question, so thanks for any help. Also, if it makes a difference, I’m using postgres.

Sam

  • 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-15T23:55:52+00:00Added an answer on May 15, 2026 at 11:55 pm

    Assuming no need for correlation, use:

    SELECT a.*
      FROM A a
     WHERE EXISTS(SELECT NULL
                    FROM B b
                  HAVING MIN(b.some_val) > a.val)
    

    If you do need correlation:

    SELECT a.*
      FROM A a
     WHERE EXISTS(SELECT NULL
                    FROM B b
                   WHERE b.id = a.id
                  HAVING MIN(b.some_val) > a.val)
    

    Explanation

    The EXISTS evaluates on a boolean, based on the first match – this makes it faster than say using IN, and — unlike using a JOIN — will not duplicate rows. The SELECT portion doesn’t matter – you can change it to EXISTS SELECT 1/0 ... and the query will still work though there’s an obvious division by zero error.

    The subquery within the EXISTS uses the aggregate function MIN to get the smallest B.some_val – if that value is larger than the a.val value, the a.val is smaller than all of the b values. The only need for a WHERE clause is for correlation – aggregate functions can only be used in the HAVING clause.

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

Sidebar

Related Questions

How can I write an efficient SQL query that returns columns form table A
I am trying to write a SQL query that returns rows from a table
How would one write a sql server DDL script that can: For each table
I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that
How can I write a query similar to this one in LINQ to SQL
I wonder about that can I write native SQL to add or delete operations
how can i write to a mysql table with ruby ? run SQL. for
How we can write a LINQ query for following select sql query: string brandid=1,2,3
I am trying to write a query for SQL Server 2005 but I can't
In SQL you have the ability to write a query that executes a between

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.