David Folksman
David Folksman

Reputation: 235

What is wrong with my join in this query?

Im practicing basic SQL with this site http://www.sqlishard.com/Exercise

Here is the question:

S5.0 - INNER JOIN

Now that we can pull data out of a single table and qualify column names, let's take it a step further. JOIN statements allow us to 'join' the rows of several tables together using a condition to define how they match one another. SELECT [columns] FROM FirstTable INNER JOIN SecondTable ON FirstTable.Id = SecondTable.FirstTableId

Try using the INNER JOIN syntax to SELECT all columns from the Customers and Orders tables where the CustomerId column in Orders matches the Id column in Customers. Since both tables have an Id column, you will need to qualify the Customers id in the WHERE clause with either the table name or a table alias.

Here is my answer:

SELECT *
FROM Customers AS c
INNER JOIN Orders AS o ON c.ID = o.ID
WHERE o.CustomerID = c.ID

The site says im wrong? Could anyone explain where i'm going wrong?

EDIT: I see now I dont need the WHERE clause, but the question states..

you will need to qualify the Customers id in the WHERE clause with either the table name or a table alias.

Hence my confusion. Thanks none the less.

Upvotes: 0

Views: 157

Answers (2)

Robert
Robert

Reputation: 25753

Try this way:

SELECT c.ID,o.ID
FROM Customers AS c
INNER JOIN Orders AS o ON o.CustomerID = c.ID

or using where clause

SELECT *
FROM Customers AS c, Orders AS o
where o.CustomerID = c.ID

Upvotes: 3

marcadian
marcadian

Reputation: 2618

If you use JOIN.. ON, you do not need where clause

Upvotes: 3

Related Questions