Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

John Yzaguirre
John Yzaguirre
22,025 Points

SQL Playground subquery practice HELP

Help. Not making much sense. This is what I came up with. It does everything he wants it to do..maybe I am misunderstanding the goal.

SELECT title AS "All Titles", NULL AS "Count of books with same title"  FROM 
(
  SELECT title FROM books_north
  UNION ALL
  SELECT title FROM books_south
)
UNION ALL
SELECT NULL, COUNT(*) FROM books_north AS n
JOIN
books_south AS s
ON n.title = s.title;

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points
  SELECT title FROM books_north
  UNION ALL
  SELECT title FROM books_south

You have this part, which will return all of the books from both locations. Now, you have to make it so your outer query is able to count up how many times each book appears in the results (from your alias I think you know what the query is after). Take a look at using a GROUP BY clause, rather than trying to use any joins.

SELECT title, COUNT(*) as total_books
FROM (SELECT title FROM books_north UNION ALL SELECT title FROM books_south)
[rest goes here]
John Yzaguirre
John Yzaguirre
22,025 Points

This is definitely helpful, thank you. The way the challenge is asked a little confusing since it says "total," which is why my original query has a number at the end that is just a total of all titles that appear in both tables.