Reputation: 283
I'm very new to SQL and trying some multiple joins with SQL Server.
Given
c_slpn
:
"ID Name Age Salary"
'1 Abe 61 140000'
'2 Bob 34 44000'
'5 Chris 34 40000'
'7 Dan 41 52000'
'8 Ken 57 115000'
'11 Joe 38 38000'
c_cust
:
"ID Name City Industry Type"
'4 Comp1 pleasant J'
'6 Comp2 oaktown J'
'7 Comp3 jackson B'
'9 Comp4 Jackson B'
c_ordr
:
"Number order_date cust_id salesperson_id Amount"
'10 8/2/96 4 2 540'
'20 1/30/99 4 8 1800'
'30 7/14/95 9 1 460'
'40 1/29/98 7 2 2400'
'50 2/3/98 6 7 600'
'60 3/2/98 6 7 720'
'70 5/6/98 9 7 150'
I should find 'List salespeople that have an order with 'Comp1', but the script below is throwing an error:
Line 6 'Incorrect syntax near the keyword 'JOIN'.
Query:
SELECT *
FROM c_cust
JOIN C_ordr ON C_ordr.cust_id= c_cust.ID
WHERE NAME = 'Comp1'
JOIN c_slpn ON c_slpn.id=c_ordr.cust_id
Please help me..., I'm stuck :-p [This is my 1st question, am I missing something]
Upvotes: 1
Views: 10050
Reputation: 44581
WHERE
clause goes after JOIN
:
SELECT *
FROM c_cust
JOIN C_ordr ON C_ordr.cust_id = c_cust.ID
JOIN c_slpn ON c_slpn.id = c_ordr.salesperson_id
WHERE c_cust.NAME = 'Comp1'
You can also do it in the JOIN
clause like C_ordr.cust_id = c_cust.ID AND c_cust.NAME = 'Comp1'
, by the way.
Upvotes: 2
Reputation: 4185
not sure if it was just typo but your table name is inconsistent.
SELECT *
FROM c_cust
JOIN c_ordr
ON c_ordr.cust_id= c_cust.ID
JOIN c_slpn
ON c_slpn.id=c_ordr.cust_id
WHERE c_cust.Name= 'Comp1'
Upvotes: 0