SQL SQL MOC


The JOIN command combines the results from multiple tables.

customers

customerIdfirstNamelastNameaddresscitycountry
1UrsaVasquezP.O. Box 878, 8416 Nullam St.WorcesterUnited States
2QuynMeyerP.O. Box 670, 7155 Tincidunt St.PriceCanada
3OrliKlein4981 Gravida St.Barrow-in-FurnessUnited Kingdom
4TallulahHines6279 Pellentesque StreetOmahaUnited States
5JoelRossP.O. Box 842, 4634 Egestas AvenueClovenfordsUnited Kingdom
6CharlotteRamos794-1654 A Rd.AkronUnited States
7DennisAveryP.O. Box 506, 4804 Molestie AvenueMatlockUnited Kingdom
8IgorMalone6627 Porttitor Rd.IrvineUnited Kingdom
9ConnorWitt5979 Vel St.TainUnited Kingdom
10KarenMarquezAp 524-1173 Metus. RoadAnnapolis RoyalCanada

orders

orderIddatecurrencytotalcustomerId
12020-11-14$1116
22020-07-07£9584
32021-02-18£7212
42020-05-25$8344
52020-07-10£471
62021-02-27£5874
72021-03-04£19810
82020-09-03$2003
92020-11-17£7263
102020-12-29$2005

INNER

INNER JOIN retrieves rows that meet a condition in both tables

This query will result in the same as WHERE does. So, any customer that doesn’t have a customerId in the orders table is not shown.

SELECT orders.orderId, customers.firstName, customers.lastName, orders.currency, orders.total
FROM orders
INNER JOIN customers ON orders.customerId = customers.customerId;
customerIdfirstNamelastNameorderIdcurrencytotal
6CharlotteRamos1$111
4TallulahHines2£958
2QuynMeyer3£721
4TallulahHines4$834
1UrsaVasquez5£47
4TallulahHines6£587
10KarenMarquez7£198
3OrliKlein8$200
3OrliKlein9£726
5JoelRoss10$200

LEFT

LEFT JOIN returns all results from first table and matching results from the second table. This query would list every customer and the details of the order if available or NULL.

SELECT customers.customerId, customers.firstName, customers.lastName, orders.orderId, orders.currency, orders.total
FROM customers
LEFT JOIN orders ON customers.customerId = orders.customerId
ORDER BY customers.customerId ASC;
customerIdfirstNamelastNameorderIdcurrencytotal
1UrsaVasquez5£47
2QuynMeyer3£721
3OrliKlein8$200
3OrliKlein9£726
4TallulahHines2£958
4TallulahHines4$834
4TallulahHines6£587
5JoelRoss10$200
6CharlotteRamos1$111
7DennisAveryNULLNULLNULL
8IgorMaloneNULLNULLNULL
9ConnorWittNULLNULLNULL
10KarenMarquez7£198

RIGHT

RIGHT JOIN is the same as LEFT JOIN, but includes all the results from the second table.

SELECT customers.customerId, customers.firstName, customers.lastName, orders.orderId, orders.currency, orders.total
FROM orders
RIGHT JOIN customers ON customers.customerId = orders.customerId
ORDER BY customers.customerId ASC;

Produces same result as LEFT JOIN.

FULL

FULL JOIN returns the results from both tables but is not supported by MySQL or MariaDB.

It can however be implemented using UNION.

SELECT customers.customerId, customers.firstName, customers.lastName, orders.orderId, orders.currency, orders.total
FROM customers
LEFT JOIN orders ON customers.customerId = orders.customerId
UNION
SELECT customers.customerId, customers.firstName, customers.lastName, orders.orderId, orders.currency, orders.total
FROM customers
RIGHT JOIN orders ON customers.customerId = orders.customerId
ORDER BY customerId;