SQL Query Help

Posted on 2011-09-08
Medium Priority
Last Modified: 2012-05-12
I need some help with a query I am trying to formulate - here is the scenario - I have 2 tables Table A has a FK to Table B - I need data from Table A provided rows for that data exist in Table B so this query does the job

SELECT TableA.FK1 FROM TableA INNER JOIN TableB ON TableA.FK1 = TableB.FK1

Now once I have the common FK I need to pull some data from table B - TableB has FK's to 2 other tables - how do I write one query to get all the data I need i.e that from table A and table B ?

Question by:abuyusuf35
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 36507230

SELECT TableC.Field1, TableD.Field1
INNER JOIN TableB ON TableA.FK1 = TableB.FK1
INNER JOIN TableC ON TableB.FK2 = TableC.FK2
INNER JOIN TableD on TableB.FK3 = TableD.FK3

Expert Comment

ID: 36507237
You can nest joins. Let's assume your other tables are TableC and TableD:

SELECT TableA.FK1, TableB.FK_C, TableB.FK_D
FROM TableA INNER JOIN TableB ON TableA.FK1 = TableB.FK1
    INNER JOIN TableC ON TableB.FK_C = TableC.FK_C
    INNER JOIN TableD ON TableB.FK_D = TableD.FK_D

Open in new window

LVL 18

Accepted Solution

Matthew Kelly earned 2000 total points
ID: 36507238
You can just add the TableB columns as a comma separated list between the SELECT and FROM

INNER JOIN TableB ON TableA.FK1 = TableB.FK1

More examples: http://blogs.msdn.com/b/alikl/archive/2010/07/20/sql-server-select-query-amp-join-types.aspx

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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