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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:36:58+00:00 2026-05-23T18:36:58+00:00

I am trying to write a query using the following example tables: Company CompanyID

  • 0

I am trying to write a query using the following example tables:

Company

  • CompanyID
  • Name
  • County

CompanyRelation

  • ParentCompanyID
  • ChildCompanyID

CompanySpecialty

  • CompanyID
  • Specialty

The CompanyRelation table is used because each company can have many subcompanies under it and each subcompany can have many companies over it.

Ideally, what I want to be able to find is all companies by a certain county and/or by a certain specialty. Now, normally, I would just join Company to CompanySpecialty and filter on those two fields, but here is where it gets tricky: the subcompanies don’t have counties assigned to them, so if I filter on the county, all the subcompanies will be excluded.

If I filter the companies down to a county, I would like to display all subcompanies that are associated to that company, regardless of the fact that the subcompany does not have an address.

To get around this, I have to bring in the CompanyRelation table.

Example data:

CompanyID---Name---County
1-----------ABC----King
2-----------BCD----Pierce
3-----------DEF----NULL
4-----------EFG----NULL

ParentCompanyID---ChildCompanyID
1-----------------1
1-----------------3
2-----------------2
2-----------------4

CompanyID---Specialty
1-----------Vehicles
2-----------Vehicles
3-----------Vehicles
4-----------Vehicles

Using this data, say I want to find all companies in King county that deal with vehicles. In my results, I would expect to see Company 1 and Company 3.

How can I write a query to accomplish this?

  • 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-23T18:36:59+00:00Added an answer on May 23, 2026 at 6:36 pm

    The Diagram would be like this (with Company being the primary / parent table, and all others depending on it):

    Company Specialty <– Company <– Relationship <– Company –> Company Specialty

    Converting it to SQL would be like this:

    SELECT
       --whatever fields you want, as long as you prefix with the table alias (p., ps., dr., c., cs.)
    FROM
          Company Parent P --parent
    
       Left Join CompanySpecialty PS
       ON P.CompanyID = PS.CompanyID
    
       Left Join CompanyRelation CR
       ON P.CompanyID = CR.ParentCompanyID
    
       Left Join Company C
       ON CR.ChildCompanyID = C.ComapnyID
    
       Left Join Company Specialty CS
       ON C.CompanyID = CS.CompanyID
    WHERE
       (PS.Specialty = 'Vehicle' AND P.County = 'King')
       OR
       (CS.Specialty = 'Vehicle' AND C.County = 'King')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to query 2 tables using the following sql statement in an
I am trying to write a query that connects 3 tables. The first table
I am using the following query to get the count from multiple tables:- SELECT
I'm trying to write a script to query ActiveMQ through the shell (using the
I am using MS-SQL and I am trying to write a query which fetches
I am using following tables... stats (id, game, user_id, rank, score, dnt) teams(id, name,
While learning Prolog, I'm trying to solve the following problem, using accumulators: Write a
i am trying to write a query using left function in access to take
I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but
I'm trying write a query to find records which don't have a matching record

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.