SQL: Output all names which belong to a category where all the countries within it are the same

前端 未结 3 1170
难免孤独
难免孤独 2021-01-28 06:59

I have the following table called Stores:

Name          |   Country   |   Category 
Pharmacy          Japan         Health
Green Vine        Italy         Dining         


        
3条回答
  •  死守一世寂寞
    2021-01-28 07:35

    You can use aggregation with a HAVING clause, that checks that the minimum country is the same as the maximum country for a category, i.e. all countries are the same (if there cannot be NULLs, which seems to be the case). Then inner join these categories.

    SELECT *
           FROM elbat t1
                INNER JOIN (SELECT t2.category
                                   FROM elbat t2
                                   GROUP BY t2.category
                                   HAVING max(t2.country) = min(t2.country)) x
                           ON x.category = t1.category;
    

提交回复
热议问题