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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:45:31+00:00 2026-06-06T13:45:31+00:00

I am newbie to SQL Joins. I have two tables Version Vid, VName, IsActive

  • 0

I am newbie to SQL Joins.
I have two tables

Version

Vid, VName, IsActive
1    V1     1
2    V2     0
3    V3     1

Sub-Version

SVid,Vid,  VName
1     1     0.1
2     1     0.2
3     2     0.1

In above tables each Version has many Sub-Version’s .

I need to fetch results from the above tables where the output should be like this.

Vid, VName, IsActive, SubVersionExists(Bit)
1    V1      1        1
2    V2      0        1
3    V3      1        0

in above result set the column name “SubVersionExists” represents if the version has SubVersion records in Sub-Version table.

Hope this explains the problem scenario well.

Thanks in advance.

  • 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-06T13:45:33+00:00Added an answer on June 6, 2026 at 1:45 pm

    Here is a version without joins, but using EXISTS (SELECT ... ) which returns a boolean value:

    http://sqlfiddle.com/#!5/712bd/9

    SELECT
      Version.Vid, Version.VName, Version.IsActive,
      EXISTS (
        SELECT NULL FROM SubVersion WHERE SubVersion.Vid = Version.Vid
      ) AS SubVersionExists
    FROM Version;
    

    Or, if your sql engine doesn’t convert booleans to 0/1, you can use CASE:

    SELECT
      Version.Vid, Version.VName, Version.IsActive,
      CASE WHEN
        EXISTS (SELECT NULL FROM SubVersion WHERE SubVersion.Vid = Version.Vid)
      THEN 1 ELSE 0 END   AS SubVersionExists
    FROM Version
    

    Another version with LEFT JOIN + GROUP BY, if you don’t want to use COUNT():

    SELECT
      Version.Vid, Version.VName, Version.IsActive,
      CASE WHEN SubVersionVids.Vid IS NOT NULL
           THEN 1 ELSE 0 END   AS SubVersionExists
    FROM      Version
    LEFT JOIN (
      SELECT   SubVersion.Vid
      FROM     SubVersion
      GROUP BY SubVersion.Vid
    ) AS SubVersionVids
      ON SubVersionVids.Vid = Version.Vid;
    

    Or you can use DISTINCT instead of GROUP BY:

    SELECT
      Version.Vid, Version.VName, Version.IsActive,
      CASE WHEN SubVersionVids.Vid IS NOT NULL
           THEN 1 ELSE 0 END   AS SubVersionExists
    FROM      Version
    LEFT JOIN (
      SELECT DISTINCT SubVersion.Vid
      FROM            SubVersion
    ) AS SubVersionVids
      ON SubVersionVids.Vid = Version.Vid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie, just learning SQL and have this question: I have two tables
I'm an SQL newbie. sorry. i have 3 tables: services, cities, city_services. i need
hello i'm newbie in sql (postgresql) i have 2 tables as result of 2
am a newbie to Oracle/PL SQL.I've 2 tables A and B. A has a
Dear All, please help me since I'm newbie in SQL Server. I have a
I'm brand new to SQL Server 2008, and have some newbie questions about the
Pardon the newbie question. My data model is really simple (two tables; 200 rows
At first - Im sql newbie, sorry for this (mbe typicall) question. I Have
I am a newbie to SQL. please help with this. I have the below
i'm a pl/sql newbie. now i have a question about oracle type . i

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.