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

The Archive Base Latest Questions

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

How can I write an efficient SQL query that returns columns form table A

  • 0

How can I write an efficient SQL query that returns columns form table A plus a bit column that specifies whether the PK of table A exists in table B? I am using MS SQLServer 2005. Thanks.

  • 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-15T03:55:56+00:00Added an answer on May 15, 2026 at 3:55 am

    It depends if the foreign table relationship is 1:1 or 1:Many. If is 1:1 the you can use a normal join:

    select A.*, case when b.id is null then 0 else 1 end
    from A 
    left join B on A.id = B.id;
    

    If the relation is 1:Many then the join would multiply the result, so you have to restrict it, and there are several ways. A simple way is using outer apply and top:

    select A.*, case when b.id is null then 1 else 0 end
    from A
    outer apply (
     select top (1) id from B where A.id = B.id) as b;
    

    As for performance, almost all solution proposed will perform the same, provided there is an appropriate index on B(id) in place.

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

Sidebar

Related Questions

I need to write a table-valued function in T-SQL that would take a zip
Does anyone know if it is possible to write a query that can produce
I need to write a SQL query for a report. A user can select
Can anyone please tell me how I can write a bash shell script that
Can someone please explain how you can write a url pattern and view that
Learning how to write JPA query. Please advise me whether it possible to write
When I write SQL queries, I find myself often thinking that there's no way
I'd like to know how can write an efficient version of quicksort where list
I need help writing a T-SQL query that will generate 52 rows of data
I have date range data in SQL DB table that has these three (only

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.