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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:22:26+00:00 2026-06-03T16:22:26+00:00

I admit to being an absolute SQL Noob here (I can do inner joins

  • 0

I admit to being an absolute SQL Noob here (I can do inner joins but that’s about as “complicated” as my knowledge goes with SQL), but I am hoping for some help/insight/ideas on how to best achieve something. I have a table of companies, and a subtable of individuals for those companies. The company can have a mailing address, but it is possible that an individual would have a different mailing address to override the normal company address. I have SQL server 2008 in my environment. My thought was to have the SQL query loop through twice in the following manner

SELECT tblIndividuals.FirstName,
       tblIndividuals.LastName,
       tblIndividuals.CompanyName,
       tblIndividuals.MailingAddress1,
       tblIndividuals.MailingAddress2,
       tblIndividuals.MailingAddress3,
       tblIndividuals.MailingAddress4,
       tblIndividuals.City,
       tblIndividuals.[State],
       tblIndividuals.Zip
FROM tblIndividuals
INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName
WHERE tblIndividuals.ChristmasList=1
  AND tblIndividuals.MailingAddress1 IS NOT NULL
  SELECT tblIndividuals.FirstName,
         tblIndividuals.LastName,
         tblIndividuals.CompanyName,
         tblCompanies.MailingAddress1,
         tblCompanies.MailingAddress2,
         tblCompanies.MailingAddress3,
         tblCompanies.MailingAddress4,
         tblCompanies.City,
         tblCompanies.[State],
         tblCompanies.Zip
  FROM tblIndividuals
  INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName WHERE tblIndividuals.ChristmasList=1
  AND tblIndividuals.MailingAddress1 IS NULL
ORDER BY tblIndividuals.CompanyName

The thought was that this way the code would loop through once, grabbing all of the individuals on the Christmas List that have a mailing address that overrides the company address, then loop through grabbing all of the individuals on the Christmas List that use the default company address, finally sorting all of the results by company name. Those of you much more well versed in SQL than I am know that this doesn’t work as I hoped, instead just running two queries. Would any one much more well versed in SQL be willing to lend some insight here in a better way to achieve my ultimate goal? Any help would be greatly appreciated. Thank you for your time in advance.

edit: I know the first query doesn’t really need the inner join, I just had copied/pasted from the second query and didn’t change it. >.<

  • 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-03T16:22:29+00:00Added an answer on June 3, 2026 at 4:22 pm
    select tblIndividuals.FirstName, tblIndividuals.LastName, tblIndividuals.CompanyName, 
    COALESCE(tblIndividuals.MailingAddress1, tblCompanies.MailingAddress1) AS MailingAddress1, 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress2 ELSE tblIndividuals.MailingAddress2 END AS MailingAddress2, 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress3 ELSE tblIndividuals.MailingAddress3 END AS MailingAddress3, 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress4 ELSE tblIndividuals.MailingAddress4 END AS MailingAddress4, 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.City ELSE tblIndividuals.City END AS City, 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.[State] ELSE tblIndividuals.[State] END AS [State], 
    CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.Zip ELSE tblIndividuals.Zip END AS Zip
    from tblIndividuals INNER JOIN tblCompanies
    On tblIndividuals.CompanyName = tblCompanies.CompanyName
    where tblIndividuals.ChristmasList=1
    order by tblIndividuals.CompanyName
    

    A few points for the future. COALESCE means “First in this list that isn’t null”. The CASE statement usage shouldn’t be hard to figure out.

    Finally, using the CompanyName as the primary key is a bad idea as the CompanyName can change presumably. Here’s a good primer on database design: http://database-programmer.blogspot.com/2008/01/database-skills-third-normal-form-and.html

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

Sidebar

Related Questions

I admit I am being lazy here but is anyone aware of a free
I'll be the first to admit that my overall knowledge of low level programming
My login redirect is being built with an absolute url but I need to
I will admit that there is already a question exactly along these lines here
I admit, I don't know too much about javascript, mostly I just steal and
I admit that I have almost none experience of unittesting. I did a try
OK so I admit right off the top that this is a bit screwy
I must admit that I am incredibly jealous of those developers who happen to
I'll admit the title is a bit confusing but it was hard to come
I have a rather simple informational website that's being run by Django. Its' pages(views)

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.