I have a table called releases, part of a ‘music’ database. The table consists of two columns; cd_id, band_id
I want to list the cd_id and the number of bands involved for each cd where two or more band_id are associated with one cd_id value.
Could you help me phrase this? Thank you in advance.
This is an aggregate
COUNT()with aHAVINGclause:The
COUNT(*)coupled with theGROUP BY cd_idreturns the number ofband_idpercd_id, and in order to limit these to releases with 2+band_id, aHAVINGclause specifiesnumbands(the alias given to the count value)>= 2.Note, this assumes each
band_idis only listed once percd_id. If that isn’t the case, to get unique bands per cd, useinstead of
COUNT(*) AS numbands.