Sql-Joins: Inner, Outer, Full: Session 6 (Week 4)
Sql-Joins: Inner, Outer, Full: Session 6 (Week 4)
OUTER, FULL
OrderID CustomerID OrderDate
10308 2 1996-09-18
SELECT Staff_ID, Staff_NAME, Staff_AGE, AMOUNT
FROM STAFF s, PAYMENT p
WHERE s.ID =p.STAFF_ID;
REVISE : INNER JOIN
Syntax:
SELECT * FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
• Inner join the cities table on the left to the countries table on the right, keeping all of the fields in both tables.
• You should match the tables on the country_code field in cities and the code field in countries.
SELECT cities.name AS city, countries.name AS country, region FROM cities INNER JOIN countries
ON cities.country_code = countries.code;
EXAMPLE 1
Use table aliasing as a shortcut. For tables you also use AS to add the alias
immediately after the table name with a space.
Notice that to select a field in your query that appears in multiple tables, you'll need to
identify which table/table alias you're referring to by using a . in your SELECT statement.
EXAMPLE 2
• Join the tables countries (left) and economies (right) aliasing countries AS
c and economies AS e.
• Specify the field to match the tables ON.
From this join, SELECT:
• c.code, aliased as country_code.
• name, year, and inflation_rate, not aliased.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT ID, NAME, AMOUNT, DATE FROM
CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Custo CustomerName ContactName Address City PostalCode Country
merID
1 Mary Po MP Amdheri Mumbai 400056 India
2 Pratiksha Rao Pratiksha Kukatpally Hyderabad 250215 India
3 Shristi Bansal Shristi Bansal Fateh Sagar Udaipur 350235 india
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table
(Orders).
Customer Table Order Table
ID NAME AGE SALARY O_ID DATE CUSTOMER_ID AMOUNT
1 ARYAN 51 56000 1 20-01-2012 2 3000
2 AROHI 21 25000 2 12/2/2012 2 2000
3 VINEET 24 31000
3 22-03-2012 3 4000
4 AJEET 23 32000
4 11/4/2012 4 5000
5 RAVI 23 42000
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Customers
Orders
Result :
ID NAME AMOUNT DATE
Returns all matched or unmatched rows. FULL OUTER JOIN can potentially return very large
result-sets! FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL JOIN ORDERS
ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
B1
A1
A N
TRY YOURSELF1 ! A M
m 2 p
2 n 3 q
4 o 5 r
A M A N
SELECT *
FROM A1 2 n 2 p
FULL OUTER JOIN B1 1 m NULL NULL
ON A1.A = B1.A;
4 o NULL NULL
NULL NULL 3 q
NULL NULL 5 r
MatchScore
Player Department_id Goals
Franklin 1 2
EXAMPLE : Alan
Priyanka
Rajesh
1
2
3
3
2
5
SELECT * FROM [TABLE1] CROSS JOIN [TABLE2];
OR Departments
Department_id Department_name
SELECT * FROM [ TABLE1] , [TABLE2] ; 1 IT
Player Department_id Goals Depatment_id Department_name
Franklin 1 2 1 IT
2 HR
Alan 1 3 1 IT 3 Marketing
Priyanka 2 2 1 IT
Rajesh 3 5 1 IT
Franklin 1 2 2 HR
Alan 1 3 2 HR
Priyanka 2 2 2 HR
Rajesh 3 5 2 HR
Franklin 1 2 3 Marketing
Alan 1 3 3 Marketing
Priyanka 2 2 3 Marketing
Rajesh 3 5 3 Marketing
SELF JOIN
The SQL SELF JOIN is used to join a table to itself as if the table were two
tables; temporarily renaming at least one table in the SQL statement.
A self join is a join in which a table is joined with itself (Unary relationships
To join a table itself means that each row of the table is combined with itself
and with every other row of the table.
Match customers that are from the same city and order them
alphabetically by city
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
SQL UNION Session 7