ePrivacy and GPDR Cookie Consent by Cookie Consent

Join from the first table (left) to the second (joined) table. The key thing here is that all the records from the left table are returned and records from the joined table are only returned if they exist.

Looking at customers, if when collecting their details on a website we didn't enforce they supply their title, we would use a left join from customers to salutations when viewing the rows.

Any returned rows without a matching salutation would return null for any projected salutation fields (salutation_name in the example below).

-- joining customers table to the salutations table
select ts.salutation_name-- from the salutations table select salutation_name
, tc.first_name-- from the customers table select first_name
, tc.last_name-- from the customers table select last_name
from tbl_customers tc-- select from the customers table, add a table label of 'tc' to make references easier above
left join tbl_salutations ts-- join to the salutations table, again providing a unique table label
on tc.salutation_id = ts.salutation_id-- join on salutation_id where the customer table salutation_id matches the salutations table salutation_id
                            


#SQL joins#SQL

Want to get started?

Contact James on 01244 722 302
Chester, Cheshire, U.K.

--