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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:31:03+00:00 2026-05-30T08:31:03+00:00

I already have a working solution to the following problem, but I worry that

  • 0

I already have a working solution to the following problem, but I worry that it is silly or inefficient.

There is a Thing table with columns (id, attributes...) and also a ThingVersion table with columns (id, thing_id, version_attributes...) The problem is to select from Thing where exactly one corresponding ThingVersion row exists.

At the moment I have something like

SELECT Thing.id AS id, Foo.whatever
FROM Thing JOIN Foo ON Thing.id=Foo.thing_id
WHERE Thing.id IN (
    SELECT thing_id FROM ThingVersion TV
    WHERE 1 = (
        SELECT COUNT(*)
        FROM ThingVersion TV2
        WHERE TV2.thing_id = TV.thing_id)
    )
ORDER BY Foo.whatever

It seems to give the correct results, and as an emphatic non-guru it seems self-explanatory, but — three selects?!?! — I can’t help but feel there must be a better way.

Is there?

  • 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-30T08:31:05+00:00Added an answer on May 30, 2026 at 8:31 am

    You can use a HAVING clause in your subquery:

    SELECT Thing.id AS id, Foo.whatever
      FROM Thing JOIN Foo ON Thing.id=Foo.thing_id
     WHERE Thing.id IN
            ( SELECT thing_id
                FROM ThingVersion TV
               GROUP BY thing_id
              HAVING COUNT(*) = 1
            )
     ORDER BY Foo.whatever
    ;
    

    You can also eliminate the JOIN in the main query:

    SELECT thing_id AS id, whatever
      FROM Foo
     WHERE thing_id IN
            ( SELECT thing_id
                FROM ThingVersion TV
               GROUP BY thing_id
              HAVING COUNT(*) = 1
            )
     ORDER BY whatever
    ;
    

    (I’m assuming that every value that appears in both Foo.thing_id and ThingVersion.thing_id must certainly appear Thing.id.)

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

Sidebar

Related Questions

I'm trying to page a table, and while I have paging already working, it
I have a database table using an enum. This is already working with hibernate
I have a problem and due to that reason i am working on a
I have the following question: I am working on a JQuery script that turns
I'm working on a video codec for OMAP3430. I already have code written in
I'm already new in C++CLI , although I have been working on VB and
I already have a deploy.rb that can deploy my app on my production server.
We already have things like static analysis that tells us what's wrong with our
There has to be a easy solution to this. I have been trying to
I have been working for a while on a project with the following components:

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.