How to Join in SQL

by Charalambos Pavlou

Firstly lets start with an inner join, the main thing to remember with joins is you need to sign your tables a unique identifier for example below "TIL_PLAYGROUND"."KEEP"."Superstore_Orders" s, has been assigned the letter s and below where we specify the other table we want to join on using the command INNER JOIN "TIL_PLAYGROUND"."KEEP"."Superstore_People" p and we assign this the letter p. now that both tables have been assigned a letter we can join on Region as they both have Region in common. this is done by using the unique identifiers we gave to the tables. So  s."Region" = p."Region" as you can see it has to be the identifier . "the field you want to join on".

The same logic applies also for LEFT JOIN and FULL JOIN as shown above but here we are taking the count distinct of order ID so we need to clarify which one is being used in the select section. COUNT (DISTINCT s."Order ID") AS "Order Returned" as you can see we need to clarify which one we need by putting the unique identifier in front. one thing to remember is you always say which fields you want before calling the table so same logic applies here you put the unique letter before in the calling section then assign the table that unique identifier.

Here we actually need a triple join which complicates things, so as before with order ID we just call the identifier within the count distinct, our original join is on p and s but then we want to do another FULL JOIN on q to get this to work we need to add s."Region" in the select section and then join on q."Region" = s."Region" otherwise this wont work.

Avatar

Charalambos Pavlou

Fri 29 Jul 2022

Thu 28 Jul 2022

Wed 27 Jul 2022

Tue 26 Jul 2022