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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:32:07+00:00 2026-05-16T14:32:07+00:00

Due to 0 responses, I’m guessing that my LEFT JOIN question got into too

  • 0

Due to 0 responses, I’m guessing that my LEFT JOIN question got into too much detail about a database that was too esoteric. I’ve already programmed around the issue, but I’d still like to know how to join in a similar scenario:

Assume a basic surrogate key strategy (each table has an id field that just auto-increments), as well as a foreign key to its obvious parent. Words in all caps can be considered tables.

Say you have a Database containing DOGS. Example: Wolfie, Winston, Butch, and Benny

Each DOG has FLEAs. (for simplicity lets make it so that one flea lives on only one dog and leave this a 1 to many relationship). The fleas have id’s as names or whatever, along with what color they are.

Each FLEA will BITE it’s DOG host several times, and that is stored in this database, and recorded daily.
Fields id(PK), flea id(FK), date, times_bitten.

Say you want to get the total number of times each dog was bitten (this is easy)

SELECT Dog.Name, sum(Bite.times_bitten)
FROM Dog, Flea, Bite
WHERE Dog.id = Flea.Dog_id and Bite.id = Flea.Bite_id
GROUP BY Dog.Name

Let’s say that you were to add criteria to the “WHERE” clause limiting it to “Brown” fleas, and no “Brown” Fleas ever bit Benny.

SELECT Dog.Name, sum(Bite.times_bitten)
FROM Dog, Flea, Bite
WHERE Dog.id = Flea.Dog_id and Bite.id = Flea.Bite_id and Flea.color = "Brown"
GROUP BY Dog.Name

Benny is not in the result

How would you rewrite the query so that Benny’s name would still show up, with either a 0(preferably) or NULL in the sum filed, rather than just having Benny eliminated altogether from the result?

This seems like its about multiple left outer joins.. but with multiple tables like this, the documentation that’s readily findable doesn’t seem to answer a question involving 3 tables with a value filter in the middle of the 3 tables.

Does anyone have advice on how to rewrite this to allow for multiple left outer joins, or have some other method of keeping all of the dog’s names in the query even if the sum = 0?

  • 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-16T14:32:08+00:00Added an answer on May 16, 2026 at 2:32 pm

    This should work:

    SELECT Dog.Name, COALESCE(SUM(Bite.times_bitten), 0) AS times_bitten
    FROM Dog
    LEFT JOIN Flea
      ON Flea.Dog_id = Dog.id
        AND Flea.color = "Brown"
    LEFT JOIN Bite
      ON Bite.id = Flea.Bite_id
    GROUP BY Dog.Name
    

    By using LEFT JOINs, you will pull all the Dog records, even those without corresponding Fleas (for which the columns from the join will be NULL). You can use COALESCE to set times_bitten to 0 if no records are found (otherwise it would be NULL).

    You probably also want to group by Dog.id instead of Dog.Name (unless it is impossible for there to be multiple dogs with the same name?)

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

Sidebar

Related Questions

Not certain if this will get much response due to the newness of Windows
Due to a lack of response to my original question , probably due to
Due to continuing crash problems, I'm about to uninstall and reinstall my copy of
Due to a weird request, I can't put null in a database if there
I recently switched my hosting provider and due to the time zone that the
I assume that public or private static targets must have reduced memory usage, due
Due to repetitive errors with one of our Java applications: Engine engine_0: Error in
Due to the lack of clientaccesspolicy.xml, there appears to be problems with using Amazon
Due to company constraints out of my control, I have the following scenario: A
Due to the nature of the live server I deploy to, my mail settings

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.