1

I am working on a problem that involves a sub select query. I have the databases set up in order to get the select query to work, but I'm relatively new to these subqueries and am a little lost. I need return band name and album titles of said bands with more than 1 album. Here is my code so far.

SELECT b.Name, a.Title
FROM Band as b, Album as a
WHERE (SELECT * 
       FROM Album as al
       WHERE al.BID = a.BID)
GROUP BY b.Name
HAVING Count(a.BID) > 1;

Any help will be greatly appreciated.

2
  • 1
    Why don't people read the documentation? It is explained in there! Commented Apr 9, 2014 at 23:56
  • What SQL product are you using? Commented Apr 10, 2014 at 0:28

1 Answer 1

1

try this

SELECT b.Name, a.Title 
FROM Band b 
INNER JOIN Album a ON b.BID=a.BID
WHERE a.BID IN (SELECT BID FROM Album GROUP BY BID HAVING COUNT(BID)>1)

This will work given that Album table has a BID as a foreign key for Band table

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

5 Comments

Seeing as I created the tables and the data within them, I know that it should return 1 band, and that band's 3 albums...It's doing neithe. Even with this code.
share the schema for these 2 tables (what columns they have), this will help in answering the question
Your code returned all the bands and their album, I'm trying to find only the bands that have more than 1 album.
sorry, misunderstood your question, thought you'd need to return all bands that have at least 1 album... here, I edited my answer, take a look
No problem, glad it helped! Also, if the answer helped you please get into habit of accepting answers otherwise people will not be helping you anymore if they see you're not doing it...

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.