Select query with multiple joins

Posted on

Question :

I have the following tables:

ORDERS:  OrderID(VARCHAR), CreatedDate(DATETIME), QuoteID(INT), BasketID(INT)
BASKETS: BasketID(NUMBER), ItemID(NUMBER)
ITEMS:   ItemID(NUMBER), QuoteID (NUMBER), ParcelID(NUMBER)
PARCELS: ParcelID(NUMBER), OrderID (VARCHAR)
QUOTE:   QuoteID(NUMBER), Price(NUMBER), Tax(NUMBER), Shipping(NUMBER)

I need to write a query that returns the shipping and tax price per item per parcel for a given order id.

I suck at SQL so I just need to have an example to understand completely on how to concatenate multiple tables.

Can someone help, please?

Thanks!

Answer :

This is probably an obtuse question but, if you are unfamiliar to SQL queries, is it “safe” for you to be writing code that concerns money? Customers really dislike it when numbers are wrong, accountants will shout and scream, and governments hate it even more …

That said, to join on multiple tables, you would generally use multiple join statements. Something like:

SELECT {Column A}, {Column B}, {Column C}, ...
  FROM {Table A} INNER JOIN {Table B} ON {Table A}.{Key} = {Table B}.{Key}
                 INNER JOIN {Table C} ON {Table B}.{Key} = {Table C}.{Key}
                 ...
 WHERE {Column X} = {id}

Note: Be sure to replace the items wrapped in {} with proper column and table names. Do not include the { or } characters.

This can be slightly different depending on which database engine you’re using.

Leave a Reply

Your email address will not be published. Required fields are marked *