I have 2 tables:
Table 1. options_ethnicity
with the following entries:
ethnicity_id ethnicity_name
1 White
2 Hispanic
3 African/American
Table 2. inquiries
with the following entries:
inquiry_id ethnicity_id
1 1
2 1
3 1
4 2
5 2
I want to generate a table that shows the number of inquires by ethnicity. My query so far looks like this:
SELECT options_ethnicity.ethnicity_name, COUNT('inquiries.ethnicity_id') AS count
FROM (inquiries
LEFT JOIN options_ethnicity ON
options_ethnicity.ethnicity_id = inquiries.ethnicity_id)
GROUP BY options_ethnicity.ethnicity_id
The query gives the correct answer but there is no column for African/American which has 0 results.
White 3
Hispanic 2
If I replace the LEFT JOIN with a RIGHT JOIN, I get all 3 ethnicity names, but the count for African/American is wrong.
White 3
Hispanic 2
African/American 1
Any help would be appreciated.
Here's an update to this post with what seems to be a working query:
SELECT
options_ethnicity.ethnicity_name,
COALESCE(COUNT(inquiries.ethnicity_id), 0) AS count
FROM options_ethnicity LEFT JOIN inquiries ON inquiries.ethnicity_id = options_ethnicity.ethnicity_id
GROUP BY options_ethnicity.ethnicity_id
UNION ALL
SELECT
'NULL Placeholder' AS ethnicity_name,
COUNT(inquiries.inquiry_id) AS count
FROM inquiries
WHERE inquiries.ethnicity_id IS NULL