The document discusses different types of SQL joins including inner, left, right, and full outer joins that are used to combine data from multiple tables. It also covers unions which combine rows from different tables and remove or retain duplicate rows. Examples are provided for each type of join and union using SELECT statements to retrieve data from tables in a database.
The document discusses different types of SQL joins including inner, left, right, and full outer joins that are used to combine data from multiple tables. It also covers unions which combine rows from different tables and remove or retain duplicate rows. Examples are provided for each type of join and union using SELECT statements to retrieve data from tables in a database.
The document discusses different types of SQL joins including inner, left, right, and full outer joins that are used to combine data from multiple tables. It also covers unions which combine rows from different tables and remove or retain duplicate rows. Examples are provided for each type of join and union using SELECT statements to retrieve data from tables in a database.
-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)
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:
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 ----------------------------------------------------------------------------------- ---------------------------------