2

I'm using the Northwind database, and I can't manage to get the following query to work -

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join Products (select Products.productID, Products.ProductName from Products)
        on [Order Details].productID = Products.productID
order by customers.customerID

I get an error saying that there's incorrect syntax near the select in line 7.

What I'm trying to do is that when joining the Products table, it won't bring all the columns but rather just the ProductName and ProductID.

Could somebody please explain what I'm doing wrong? Thanks!

2 Answers 2

2

You first need to specify the subquery and then the alias.

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join (select Products.productID, Products.ProductName from Products) Products 
        on [Order Details].productID = Products.productID
order by customers.customerID

Change

join Products (select Products.productID, Products.ProductName from Products) 

to

join (select Products.productID, Products.ProductName from Products) Products 
Sign up to request clarification or add additional context in comments.

Comments

2

You may want to try this:-

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join (select Products.productID, Products.ProductName from Products) Products
        on [Order Details].productID = Products.productID
order by customers.customerID

Comments

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.