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 Querying Relational Databases Subqueries Review and Practice

Is this a decent solution for challenge 2?

SELECT p.first_name, p.email, COUNT(p.id) AS Outstanding FROM patrons AS p
INNER JOIN (
  SELECT * FROM loans_north
  UNION ALL
  SELECT * FROM loans_south
) AS l ON p.id = l.patron_id
WHERE l.returned_on IS NULL
GROUP BY p.id
ORDER BY p.first_name;

2 Answers

Steven Parker
Steven Parker
229,744 Points

A solution that works is a good solution. :wink:

However, it's "best practice" to always include any SELECTed column that isn't in an aggregating function in the GROUP BY clause. Some database engines will give you an error if you don't.

Thanks Steven. Do you know what the error code would be?

Steven Parker
Steven Parker
229,744 Points

It depends on the specific database engine, but a typical example message might be:
  "ERROR: column "<column_name>" must appear in the GROUP BY clause or be used in an aggregate function"