0

I need to show the names of all artists which have at least one female member each.

The Members table looks as follows:

Members
-------
MemberID, Firstname, Lastname, Address, Gender

The Artists table looks as follows:

Artists
-------
ArtistID, Artistname, City

The tables are related using the xRefArtistsMembers table, which is as follows:

xRefArtistsMembers
------------------
MemberID, ArtistID

I have formulated the following subquery:

select
  ar.artistname from artists ar
where
  ar.artistid in
  (
    (
      select
        x.artistid
      from
        xrefartistsmembers x
    )
    in
    (
      select
        m.memberid
      from
        members m
    )
  );

which does not compile. Again, my question is: I need to show the names of all artists which have at least one female member each. With using only subqueries.

7
  • 2
    You don't have a column that indicates the gender of member. Commented Nov 22, 2013 at 4:12
  • one female member each ? how will you find .. Im not seeing any Gender column Commented Nov 22, 2013 at 4:13
  • Let me apologize, I accidently forgot to include that crucial peice of record data. Commented Nov 22, 2013 at 4:14
  • Is this homework? Why can't you use a join? Commented Nov 22, 2013 at 4:15
  • What does your title mean? Do you have to use sub-queries instead of JOIN? Commented Nov 22, 2013 at 4:15

1 Answer 1

1

Try

SELECT *
  FROM artists a
 WHERE EXISTS
(
  SELECT *
    FROM xRefArtistsMembers x
   WHERE artistid = a.artistid
     AND EXISTS
  (
    SELECT * 
      FROM members 
     WHERE memberid = x.memberid
       AND gender = 'female'
  )
)

Here is SQLFiddle demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. For some reason the following query: select * from artists ar where artistname in (select gender from members where members.gender = 'F'); For some reason produces an EMPTY SET>
You're quite welcome. ;) How can the query you mentioned in the comment above produce anything when you compare artistname in outer select to gender in inner subquery..?
Very correct. Ok Thanks I think I understand more simply by the 'EXISTS' statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.