Solved

SQL Query

Posted on 2013-11-08
3
212 Views
Last Modified: 2013-11-11
I want to write a query that selects multiple columns from one table and 1 column from another table.  Both tables have the same primary key.  

Also, Im not sure if it makes a difference but, the 1 column I want to select from the second table also happens to be the foreign key on the second table (it doesnt exist on the first table).


Can you give me simple syntax for this query?
0
Comment
Question by:tike55
3 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 167 total points
Comment Utility
It would be easier if you posted the structure of the two tables fully but basically it would be something like :

Select a.column1, a.column2, a.column3, b.column2
From table1 a, table2 b
Where a.column1 = b.column1

The above select assumes that column1 in each table is the primary key that exists in both tables.
The 'a' and 'b' prefixes refer to the aliases used for the tables as specified in the from clause.
The which clause provides the join between the two tables on the shared primary key.
The required field in Table 2 is referenced in the Select list as b.Column2 which is an alias for table2.Column2
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 166 total points
Comment Utility
While jacko72's code will work, in SQL Server it is more common to use the join syntax:

Select a.column1, a.column2, a.column3, b.column2
From table1 a inner join table2 b on a.column1 = b.column1
0
 
LVL 31

Accepted Solution

by:
awking00 earned 167 total points
Comment Utility
select a.col_x, a.col_y, a.col_z, b.fk
from tablea a inner join tableb b
on a.pk = b.pk
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to fix this error 14 45
Extract XML Data from using TSQL 5 28
t-sql month question 8 40
SQL Server 2012 Row Selection 2 29
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now