You are on page 1of 2

UNIONS:

-Union: it combines tables vertically (up to down)


-Joins: it combines tables horizontally (left to right)
-Union by default combines and remove duplicate rows.
-If don't want to remove duplicate rows, Use UNION ALL.

SELECT City, Country FROM Customers -- simple UNION


WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

SELECT City, Country FROM Customers -- UNION ALL


WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
-----------------------------------------------------------------------------------
---------------------------------
JOINS:
-INNER JOIN: selects records that have matching values in both tables.
-LEFT JOIN: returns all records from the left table, and the matching records from
the right table.
-RIGHT JOIN: returns all records from the right table, and the matching records
from the left table.
-FULL OUTER JOIN: returns all records whether match or not.
-----------------------------------------------------------------------------------
---------------------------------
INNER JOIN: (3 table join)

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName


FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
-----------------------------------------------------------------------------------
---------------------------------
LEFT JOIN:

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
-----------------------------------------------------------------------------------
---------------------------------
RIGHT JOIN:

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
-----------------------------------------------------------------------------------
---------------------------------
FULL OUTER JOIN:

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
-----------------------------------------------------------------------------------
---------------------------------
SELF JOIN: ( https://www.youtube.com/watch?v=ck8mVDOOCCg )
-A self join is a join of one table (with itself).
-It is, a table referencing itself.
-when we self join, we always use alias b/c table name is same.
-It is commonly use when the data stored in table in a hierarchy fashion.
-By hierarchy, it means an employee table has 3 columns (id, name, head_id) and
head_id is actually the employee id.
-So how to get the head name of every employee ???
-So we join the table with itself to get the names.

SELECT emp.name, head.name FROM


employee emp LEFT JOIN employee head ON
emp.id = head.head_id
-----------------------------------------------------------------------------------
---------------------------------

You might also like