Join from one table to another, typically from a primary key in one table to a foreign key in another.
You might use an inner join in the following circumstances:
- When you confident there is a foreign key for each row in the main table. The example below assumes every customer will also have a salutation.
- When you want to retrieve rows but want to ignore those without a foreign key. In the example below only customers with a salutation will be returned. Customer rows with no salutation will be omitted from the results.
-- 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
inner 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