SQL: How to select users that have purchased ONLY a certain product? -


say have table looks following

customer     purchases james        basketball james        football alex         basketball 

how select customers have purchased basketball? ie alex. or advice.

this 1 way using standard sql;

select customer table1 group customer having count(*) = 1   , max(purchases)='basketball' 

an sqlfiddle test with.

if customer may have purchased more 1 basketball, can replace count(*) count(distinct purchases).

if go database specific, more readable option may except or minus, here sql server version;

select customer table1 purchases='basketball' except select customer table1 purchases<>'basketball' 

...which same thing latter (may have purchased more 1 basketball) case.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -