SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Data from 3 different tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

clausenmausen
Starting Member

USA
2 Posts

Posted - 11/17/2013 :  00:42:45  Show Profile  Reply with Quote
Hi!
This is probably a very common SQL task but I'm hoping someone can help me out. I'm playing around with the AdventureWorks database in SQL Server Management Studio. Let's say I want to get a table that has FirstName, LastName, City and State for all employees that live in the state of Oregon. These are the tables.

Person
BusinessEntityID
FirstName
LastName

Address
AddressID
City
StateProvinceID

StateProvince
StateProvinceID
Name (of State)

BusinessEntityAddress
BusinessEntityID
AddressID

Another way I could explain what I need is with this obvious wrong code:

SELECT
Person.FirstName,
Person.LastName,
Address.City,
StateProvince.Name
WHERE
StateProvince.Name = 'Oregon'
;

What's the correct way to achieve this in one statement?

khtan
In (Som, Ni, Yak)

Singapore
17435 Posts

Posted - 11/17/2013 :  01:06:09  Show Profile  Reply with Quote
SELECT
           Person.FirstName,
           Person.LastName,
           Address.City,
           StateProvince.Name
FROM       Person 
INNER JOIN BusinessEntityAddress ON Person.BusinessEntityID = BusinessEntityAddress.BusinessEntityID 
INNER JOIN Address               ON BusinessEntityAddress.AddressID = Address.AddressID
INNER JOIN StateProvince         ON Address.StateProvinceID = StateProvince.StateProvinceID     
WHERE
           StateProvince.Name = 'Oregon'



KH
Time is always against us

Go to Top of Page

clausenmausen
Starting Member

USA
2 Posts

Posted - 11/17/2013 :  01:30:36  Show Profile  Reply with Quote
Perfect! Thank you so much.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000