Problem with an inner query with SQL using the IN operator

294 Views Asked by At

So I'm learning SQL at Khan Academy and I can't get my code to work properly to complete the third step of the Playlist maker challenge. I'm supposed to use a nested subquery with an IN operator. I pasted my code below.

The first two sql statements are valid for the steps 1 and 2 of the challenge. The third and fourth statements are my attempts at completing the third step. How do I correct it to complete the challenge?

  1. SELECT title FROM songs WHERE artist = "Queen";

  2. SELECT name FROM artists WHERE genre = 'Pop';

  3. SELECT name FROM artists WHERE genre IN ( SELECT title FROM songs WHERE artist = 'Queen');

  4. SELECT title FROM songs WHERE artist IN ( SELECT name FROM artists WHERE artist LIKE 'Pop');

1

There are 1 best solutions below

2
g.d.d.c On

Guessing a little bit, because you don't provide schema data here, but 3. should probably be:

Select name from artists where genre in (select genre from songs where artist = 'Queen')

Which would return all artists that also have a song in any genre where Queen wrote a song. Similarly, for 4.:

Select title from songs where artist in (select name from artists where genre like 'Pop%')

Ought to give you all song titles for all artists if their genre starts with 'Pop'.