DBMS-Retrieving Record in Multiple Tables: Ruben A. Parazo Faculty, Department of Computer Studies
DBMS-Retrieving Record in Multiple Tables: Ruben A. Parazo Faculty, Department of Computer Studies
Multiple Tables
Ruben A. Parazo
Faculty, Department of Computer Studies
SQL JOIN
• A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.
• Different types of Joins
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table
SQL INNER JOIN
• The INNER JOIN keyword selects records that have matching values in
both tables.
• Syntax
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example: Inner Join
Orders Table
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
Customers Table
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y Ana Trujillo Avda. de la México D.F. 05021 Mexico
helados Constitución 2222
3 Antonio Moreno Taquería Antonio Mataderos 2312 México D.F. 05023 Mexico
Moreno
Con’tn
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Orders.OrderID Customers.CustomerName
10308 Ana Trujillo Emparedados y
helados
Examples of Select statement joining multiple
tables
1. SELECT a.docid,a.dname, b.desc,c.tday,c.sit_time
FROM doctors a
INNER JOIN specialize b ON a.docid=b.docid
INNER JOIN timeschedule c ON a.docid=c.docid;
2. SELECT a.docid,a.dname, b.desc,c.tday,c.sit_time
FROM doctors a
INNER JOIN specialize b ON a.docid=b.docid
INNER JOIN timeschedule c ON a.docid=c.docid
WHERE a.docid=1 AND c.tday='WED';
SQL LEFT JOIN Keyword
• The LEFT JOIN keyword returns all records from the left table (table1),
and the matched records from the right table (table2). The result is
NULL from the right side, if there is no match.
• Syntax
• SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Cont’n: Example
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
Output
Customers.CustomerName Orders.OrderID
Output
Customers.CustomerName Orders.OrderID
null 37
null 77
SQL FULL JOIN Keyword
• Combines the results of both left and right outer joins.
• The joined table will contain all records from both the tables and fill in
NULLs for missing matches on either side.
• Note: FULL OUTER JOIN can potentially return very large result-sets!
• Tip: FULL OUTER JOIN and FULL JOIN are the same.
• Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Cont’n: Example
Customers.CustomerName Orders.OrderID