Lecture 7 SQL Join
Lecture 7 SQL Join
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table containing the candidate key is called the
referenced or parent table.
"Persons" table:
"Orders" table:
MySQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the
following SQL syntax:
MySQL:
2. Left Join
Select all records from Table A, along with records from Table B for which the join condition is met (if at all).
3. Right Join
Select all records from Table B, along with records from Table A for which the join condition is met (if at all).
4. Full Join
Select all records from Table A and Table B, regardless of whether the join condition is met or not
Let's use the tables we introduced in the “What is a SQL join?” section to show examples of these joins in action.
The relationship between the two tables is specified by the customer_id key, which is the "primary key" in
customers table and a "foreign key" in the orders table:
Let’s say we wanted to get a list of those customers who placed an order and the details of the order they placed. This
would be a perfect fit for an inner join, since an inner join returns records at the intersection of the two tables.
Syntax
Note that only George Washington, John Adams and Thomas Jefferson placed orders, with Thomas Jefferson
placing two separate orders on 3/14/1760 and 9/03/1790.
Left Join
If we wanted to simply append information about orders to our customers table, regardless of whether a customer
placed an order or not, we would use a left join. A left join returns all records from table A and any matching records
from table B.
Syntax
Or
Right Join
Right join is a mirror version of the left join and allows to get a list of all orders, appended with customer information.
Syntax
Note that since there were no matching customer records for orders placed in 1795 and 1787, the first_name
and last_name fields are NULL in the resulting set.
Also note that the order in which the tables are joined is important. We are right joining the orders table to the
customers table. If we were to right join the customers table to the orders table, the result would be the same
as left joining the orders table to the customers table.
Why is this useful? Simply adding a “where first_name is NULL” line to our SQL query returns a list of all orders
for which we failed to record information about the customers who placed them:
Full Join
Finally, for a list of all records from both tables, we can use a full join.
Syntax
**The issue is that XAMPP uses MySQL (or MariaDB), which does not support FULL OUTER JOIN directly.
-- FULL OUTER JOIN (MySQL-compatible): Combines LEFT and RIGHT joins using UNION
SELECT emp_name, age, salary, dept_name, location
FROM employees
LEFT JOIN departments
ON employees.dept_id = departments.dept_id
UNION
SELECT emp_name, age, salary, dept_name, location
FROM employees
RIGHT JOIN departments
ON employees.dept_id = departments.dept_id;