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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:38:03+00:00 2026-06-18T01:38:03+00:00

I have a Contacts table and a Businesses table, they are joined via a

  • 0

I have a Contacts table and a Businesses table, they are joined via a Contacts_vs_Businesses table (it’s a many-to-many relationship).

I want to query the two tables; if a contact is related to two businesses, I want to return:

  • A row with all contact details and all details of Business A;
  • A row with all contact details and all details of Business B
  • A row with all contact details and NO business details at all (as if I’d just done a basic SELECT on the first table)

Contacts Table

ID Contact_Name Contact_Phone
1  Jez Clark    01234 567 890
2  Someone Else 01254 648 654

Businesses Table

ID Business_Name   Business_Address
1  A Company       24, A Street, A Town
2  Another Company 43, Another Street, Another Town

Contacts_vs_Businesses

Contact_ID Business_ID
1          1
1          2
2          2

I want to return:

Contact_Name Contact_Phone Business_Name   Business_Address
Jez Clark    01234 567 890 A Company       24, A Street, A Town
Jez Clark    01234 567 890 Another Company 43, Another Street, Another Town
Jez Clark    01234 567 890 NULL            NULL

I’m on SQL Server 2008 R2.

How would I go about this (I’m guessing it’s something REALLY easy…)? I’ve tried the various permutations of OUTER and INNER and LEFT/RIGHT joins, but none seem to give me that last line of results.

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-06-18T01:38:04+00:00Added an answer on June 18, 2026 at 1:38 am

    If I’m understanding your question correctly, for any contact that is associated with 2 businesses, you want to show that contact with each business, followed by a NULL business, resulting in 3 records?

    Try something like this using GROUP BY to get counts and UNION to return the NULL record:

    SELECT C.Contact_Name, C.Contact_Phone, B.Business_Name, B.BusinessAddress
    FROM Contacts C
      INNER JOIN Contacts_vs_Businesses CB ON C.Id = CB.Contact_ID
      INNER JOIN Businesses B ON CB.Business_Id = B.Id
      INNER JOIN (SELECT Contact_ID, COUNT(*) cnt FROM Contacts_vs_Businesses GROUP BY Contact_ID) CB2 ON C.Contact_ID = CB2.Contact_Id      
    WHERE CB2.cnt = 2
    UNION
    SELECT C.Contact_Name, C.Contact_Phone, NULL, NULL
    FROM Contacts
       INNER JOIN Contacts_vs_Businesses CB ON C.Contact_ID = CB.Contact_Id      
    GROUP BY C.Contact_Name, C.Contact_Phone
    HAVING Count(*) = 2
    

    Good luck.

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

Sidebar

Related Questions

I am using Entity Framework and I have two tables, a contact relationship table
I have two tables - 'business' and 'business_contacts'. The business_contact table has a many-to-one
I have two tables: contacts and contact_temps. The contact_temps table mirrors the contacts table.
I have two tables. Contacts table - contact_id, contact_email, contact_name, etc Opt out table
I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed
I have two tables in my database: user and media_contact . The media_contact table
I have a table (contacts) and a table (connection) in SQLite, they are pretty
I have a database with 3 tables in it. They are Projects, Contacts and
I shall start with the structure of my tables. I have a Contacts table
I have got 2 tables: Contacts and Users . Contacts table contains user_id which

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.