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

Daniel Crittenden
Daniel Crittenden
9,308 Points

Querying Relational Databases: Subqueries Challenge Task 4 issues

For some reason, the software will not accept my response to task 4. What am I doing wrong? I tried manually typing in all columns from the Sale table as well.

In a car database there is a Sale table with columns, SaleID, CarID, CustomerID, LocationID, SalesRepID, SaleAmount and SaleDate and a Customer table with columns, CustomerID, FirstName, LastName, Gender and SSN. Use a subquery as a derived table to show all sales to female ('F') customers. Select all columns from the Sale table only.

SELECT s.* FROM Sale AS s INNER JOIN (SELECT CustomerID FROM Customer WHERE Gender = 'F') AS t ON s.CustomerID = t.CustomerID;

Note that if I select using the "*", I am technically pulling in the CustomerID from the Customer table, correct? So the actual correct answer should be the above versus the answer that the challenge accepts which is:

SELECT * FROM Sale AS s INNER JOIN (SELECT CustomerID FROM Customer WHERE Gender = 'F') AS t ON s.CustomerID = t.CustomerID;

Thank you. Dan

Nate Baker
Nate Baker
17,247 Points

I had this same issue. The challenge states "Select all columns from the Sale table ONLY." Well, to do that, you would need to SELECT Sale.*, right?

Instead, challenge only accepts SELECT *

Task 4 on Subqueries challenge: https://teamtreehouse.com/library/querying-relational-databases/subqueries/subqueries

1 Answer

Hi Dan,

This is a great question!

The best way to find out the answer is to open a database that has the same tables and columns as the challenge does and test your queries there.

I went to Using a Subquery to Create a Temporary Table (Part 2) and picked Launch SQL Playground. Once the database is open, type your SELECT s.* and SELECT * queries into the same query window and run it.

Compare the results of the two queries side-by-side. What do you see? The result sets are the same, right? Well...not quite. Look closely and you will see the SELECT * query produces one more column than the SELECT s.* query. The extra column is the CustomerID column from the Customer table, making for two CustomerID columns in your result set.

I don't know why the teacher wants this duplicate column in the result set but it at least explains why the error message says the query didn't return all columns.

Thanks for the question!

Cheers