I’m learning SQL from a website. The question they asked was
Show the provinces that has more patients identified as ‘M’ than ‘F’.
Must only show full province_name
Table
My attempted answer using subqueries, rather than join (I am trying to learn subqueries rather than the right way which is to use "joins")
SELECT
(select pn.province_name where pn.province_id=p.province_id) AS Province
FROM
patients p,
province_names pn
group by
p.province_id
having
COUNT(case when p.gender = 'M' then 1 end) > count(case when p.gender = 'F' then 1 end)
But the output I get is
When I should actually get
Why are NULLS appearing?
2
Answers
perhaps province_names.province.name (TEXT) are NULL, so when you select them using
You could be returning NULL values from the start.
Consider using a coorlated subquery instead?
Though I’d prefer