Search code examples
sqloracle-databaseoracle-sqldeveloperrelational-division

SQL: CUSTOMER that has bought a WORK from every ARTIST || 2 tables


So I have 2 tables that both contain IDs

work:

workID, artistID
1       6
2       7
3       8
4       6
5       6

trans:

CustomerID, workID
10          1 
11          2
12          3
10          4
10          5

So far I've only managed to put all the ID's together but the result I need is to show any customerID that has purchased a workID from every artistID:

SELECT
     trans.customerID, trans.workID, work.artistID
FROM
    trans
      INNER JOIN work
      ON trans.workID=work.workID
WHERE
    trans.customerID IS NOT null
ORDER BY
    trans.customerID;

Basically I have no idea what my next step should be, if anyone could point me in the right direction or explain a solution that would be great.

EDIT: added simplified data to tables


Solution

  • This should get you going:

    SELECT t.customerId 
    FROM 
      ( SELECT t.CustomerId, COUNT(DISTINCT artistId) as artists
        FROM trans t INNER JOIN work w ON w.workid = t.workid) t 
      INNER JOIN 
      ( SELECT COUNT(DISTINCT artistId) artists FROM work) a ON  t.artists = a.artists