How to find distinct columns in a nested subquery in SQL?

后端 未结 2 1188
时光说笑
时光说笑 2021-01-29 12:39

I need to find the distinct drinkers that ordered \'VODKA\' and \'WHISKY\'. I AM ONLY ALLOWED TO USE A NESTED QUERY. No other format is accepted.

I am quite new to sql

2条回答
  •  不知归路
    2021-01-29 13:15

    You could use an hanving for count distinct drink group by DRINKER

    SELECT DRINKERS.DRINKER
    FROM DRINKERS
    INNER JOIN ORDERS DRINKERS.DRINKER = ORDERS.DRINKER
    WHERE ORDERS.DRINK IN ( 'VODKA' , 'WHISKY')
    GROUP BY DRINKER
    HAVING COUNT(DISTINCT ORDERS.DRINK ) = 2 
    

    or if you need a nested

        SELECT DISTINCT DRINKER
        FROM DRINKERS
        WHERE EXISTS (SELECT DRINKER 
                      FROM ORDERS
                      WHERE DRINK IN ( 'VODKA' AND 'WHISKY')
                      GROUP BY DRINKER
                      HAVING COUNT(DISTINCT ORDERS.DRINK ) = 2 
                      );
    

提交回复
热议问题