SQL Query with table joins

Posted on 2014-08-22
Last Modified: 2014-08-22

What would the SQL query be to select all fields from the table shown in the image.
I used Access to get the image but want to understand how someone would ma manually
write the SQL script to pull data from multiple joined tables.

Question by:murbro
    LVL 1

    Accepted Solution

    First, the mapping for the tables is wrong.  Everything is going to feed into Jobs.  I would assume that you have a ClientID in Jobs.  If you have multiple contacts you may have to have multiple links with the Contacts using aliases for the Contacts table.  Alternatively you could create a juntion table that links Contact_ID to Job ID.  The same thing is true for the materials table.  A junction table would be most appropriate for multiple Materials per job linking Job ID and Materials ID.

    Author Comment

    Hi. Thanks. I am really just looking for an example of 3 or four tables that are all linked together. Jobs does have a ClientID. I just need some direction on linking multiple tables
    LVL 1

    Assisted Solution

    Select(whatever you want to select in the tables)
    from Jobs j
    inner join Clients c on j.ClientID=c.ClientID
    inner join Materials m on m.materialsID = j.materialsID
    inner join Contacts s on s.Contact_ID = j.Contact_ID

    If you are not always getting a Client or contact, then you may have to do outer joins.
    LVL 22

    Assisted Solution

    by:Steve Wales
    As a general example of joining multiple tables into a single query, let's assume we have  tables:

    Suppliesr - supplier_no, supplier_name
    orders - order_no, supplier_no, order_date
    order_items - order_no, order_item_no, stock_item
    stock - stock_item, stock_item_name

    If you wanted to pull all the details for an order:

    select a.order_no, a.supplier_no, a.order_date, b. order_item_no, b.stock_item, d.stock_item_name
    from orders a
    join order_items b on a.order_no = b.order_no
    join suppliers c on a.supplier_no = c.supplier_no
    join stock d on b.stock_item = d.stock_item

    Open in new window

    Start with the base query on the table that drives your query

    Add in tables with joins one at a time to build your query.

    Author Closing Comment

    Thanks very much for the help

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    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.

    745 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

    15 Experts available now in Live!

    Get 1:1 Help Now