SlideShare a Scribd company logo
1 of 33
ADVANCED
DATABASE SYSTEMS
Ellen Grace Porras
OVERVIEW
In this subject, you will learn how to retrieve data from multiple
tables using one SQL statement. You will see how tables can be
joined together and how similar results are obtained using
different approaches, including the joints and sub queries.
It is important that you understand how to query multiple tables
for generating appropriate reports in the creation of an
information system.
After studying our lesson, you should be able to:
• Learn the capabilities of SQL, in querying multiple tables by
joining these tables.
• Connect Database to Visual Basic
• Create a simple Database System
JOINING MORE THAN ONE TABLES
You already learned from your previous database course how to generate
reports from a single table. This time we will explore the capabilities of
SQL, in querying multiple tables by joining these tables.
Joining can be performed by combining two or more tables together by
finding rows that match the values in the common columns. These
common columns in joined tables are usually the primary key of the
parent table and the foreign key of the child table in a one-to-many
relationships.
4
5
SAMPLE TABLES
JOIN typically combines rows with equal values for the specified columns.
Usually, one table contains a primary key, which is a column or columns
that uniquely identify rows in the table (the cat_id column in the cat table).
The other table has a column or columns that refer to the primary key
columns in the first table (the cat_id column in the toy table). Such
columns are foreign keys.
The JOIN condition is the equality between the primary key columns in one
table and columns referring to them in the other table.
JOIN returns all rows that match the ON condition. JOIN is also
called INNER JOIN.
It is when you want to return only records having pair on both sides.
Example syntax:
SELECT *
FROM Toy
JOIN Cat
ON TOY.CAT_ID = CAT.CAT_ID;
7
RESULT
8
There is also another, older syntax, but it isn't recommended. List joined
tables in the FROM clause and place the conditions in the WHERE clause.
SELECT *
FROM Toy, Cat
WHERE Toy.Cat_ID = Cat.Cat_ID;
9
JOIN CONDITIONS
10
The JOIN condition doesn't have to be an equality – it can be any
condition you want. JOIN doesn't interpret the JOIN condition; it
only checks if the rows satisfy the given condition. To refer to a
column in the JOIN query, you have to use the full column
name: first the table name, then a dot (.) and the column name:
ON Cat.Cat_ID = Toy.Cat_ID
JOIN CONDITIONS
11
The JOIN condition doesn't have to be an equality – it can be any condition you want.
JOIN doesn't interpret the JOIN condition; it only checks if the rows satisfy the given
condition. To refer to a column in the JOIN query, you have to use the full column
name: first the table name, then a dot (.) and the column name:
ON Cat.Cat_ID = Toy.Cat_ID
You can omit the table name and use just the column name if the name of the column
is unique within all columns in the joined tables.
NATURAL JOIN
12
If the tables have columns with the same name, you can use NATURAL JOIN instead
of JOIN.
SELECT *
FROM Toy
NATURAL JOIN Cat;
The common column appears only once in the result table.
Note: NATURAL JOIN is rarely used in real life.
LEFT JOIN
13
LEFT JOIN returns all rows from the left table with matching rows from the right table.
Rows without a match are filled with NULLs. LEFT JOIN is also called LEFT OUTER
JOIN.
When you need all records from the left table no matter if they have pair in the right
table or not.
SELECT *
FROM Toy
LEFT JOIN Cat
ON Toy.Cat_ID=Cat.Cat_ID;
LEFT JOIN
14
15
RIGHT JOIN
RIGHT JOIN returns all rows from the right table with matching rows from the left table.
Rows without a match are filled with NULLs. RIGHT JOIN is also called RIGHT OUTER
JOIN.
SELECT *
FROM Toy
RIGHT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID;
16
FULL JOIN (FULL OUTER JOIN)
FULL JOIN returns all rows from the left table and all rows from the right table. It fills
the non-matching rows with NULLs. FULL JOIN is also called FULL OUTER JOIN.
SELECT *
FROM Toy
FULL JOIN Cat
ON Toy.Cat_ID = Cat.Cat_ID;
Note: Full Outer Join in MySQL
• MySQL doesn’t have FULL OUTER JOIN
• Simulate using LEFT and RIGHT JOINs
17
FULL JOIN (FULL OUTER JOIN)
Example Syntax:
SELECT *
FROM Toy
LEFT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID
UNION SELECT *
FROM Toy
RIGHT JOIN Cat
ON Toy.Cat_ID= Cat.Cat_ID
18
CROSS JOIN
CROSS JOIN returns all possible combinations of rows from the left and right tables.
Example Syntax:
SELECT *
FROM Toy
CROSS JOIN Cat;
Other syntax:
SELECT *
FROM Toy, Cat;
19
CROSS JOIN
COLUMN AND TABLE ALIASES
20
Aliases give a temporary name to a table or a column in a table.
Mom_ID Owner_ID
Owner As o
Owner_ID Owner_Name
1 John Smith
2 Danielle Davis
21
A column alias renames a column in the result. A table alias renames a table within the
query. If you define a table alias, you must use it instead of the table name everywhere in
the query. The AS keyword is optional in defining aliases.
Example Syntax:
SELECT o.owner_name, c.cat_name
FROM cat AS c
JOIN owner AS o
ON c.owner_id = o.owner_id;
22
SELF JOIN
You can join a table to itself, for example, to show a parent-child relationship.
Mom_ID Owner_ID Mom_ID Owner_ID
23
Each occurrence of the table must be given a different alias. Each column reference must
be preceded with an appropriate table alias.
Example Syntax:
SELECT child.cat_name AS child_name, mom.cat_name AS mom_name
FROM cat AS child
JOIN cat AS mom
ON child.mom_id = mom.cat_id;
24
MULTIPLE JOINS
To perform joining multiple tables, you need to understand the SQL JOIN
concepts clearly. You need to understand how different joins such as INNER
JOIN, LEFT JOIN, RIGHT JOIN, CROSS JOIN, and FULL JOIN work.
Moreover, understanding table relationships such as one-to-one, one-to-many,
and many-to-many as well as primary key and foreign key will help to understand
this topic. So, make sure that you have a good understanding of these points.
25
MULTIPLE JOINS
You can join more than two tables together. First, two tables are joined, then the third
table is joined to the result of the previous joining.
Owner As o
Owner_ID Owner_Name
1 John Smith
2 Danielle Davis
26
JOIN & JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
JOIN cat c
ON t.cat_id = c.cat_id
JOIN owner o
ON c.owner_id = o.owner_id;
27
JOIN & LEFT JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
JOIN cat c
ON t.cat_id = c.cat_id
LEFT JOIN owner o
ON c.owner_id = o.owner_id;
28
LEFT JOIN & LEFT JOIN
Example Syntax:
SELECT t.toy_name, c.cat_name, o.owner_name
FROM toy t
LEFT JOIN cat c
ON t.cat_id = c.cat_id
LEFT JOIN owner o
ON c.owner_id = o.owner_id;
ACTIVITY
30
OrderID PK Order_Name CustomerID
FK
10308 Keyboard 1001
10309 Mouse 1002
10310 Mouse Null
10311 Monitor 1004
Table: Orders
CustomerID CustomerName Address
1001 Jose Bautista Taguig City
1002 Allan Williams Pasay City
1003 Anna Marie Ruiz Mandaluyong City
Table: Customers
Use the following Join Statements:
1. Natural Join
2. Left Join
3. Right Join
4. Full Join
5. Cross Join
31
OrderID PK OrderItem FK CustomerID SupplierID FK OrderDate
10308 Mouse 1001 1014 2023-08-20
10309 Keyboard 1002 1037 2023-08-23
10310 Monitor 1003 1022 2023-08-29
Table: Orders
CustomerID PK CustomerName Address
1001 Jose Bautista Taguig City
1002 Allan Williams Pasay City
1003 Anna Marie Ruiz Mandaluyong City
Table: Customers
SupplierID PK SupplierName Address
1014 Electronico PPC
1037 Du Ek Sam Narra
1022 Teslinque PPC
Table: Suppliers
Use the following Join
Statements
1. Join & Join
2. Join & Left Join
3. Left join & Left Join
REFERENCES
32
• Hoffer, J. Rames, V., Topi, H. (2013) Modern Database Management, 11th Ed
New Jersey Pearson Education, Inc
• Price, J. (2008) Oracle Database l1g SQL: Master SOL and PL QL in the
Oracle Database. New York: McGraw-Hill.
• AMA Online University MIT 412. Advanced Database Systems
THANK YOU
Ellen Grace D. Porras
egporras@psu.palawan.edu.ph

More Related Content

Similar to Advanced Database Systems.pptx

Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3
Ali MasudianPour
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql Statments
SHC
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
sagaroceanic11
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 

Similar to Advanced Database Systems.pptx (20)

SQL COMMANDS WITH EXAMPLES.pptx
SQL COMMANDS WITH EXAMPLES.pptxSQL COMMANDS WITH EXAMPLES.pptx
SQL COMMANDS WITH EXAMPLES.pptx
 
Les05
Les05Les05
Les05
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 
MY SQL
MY SQLMY SQL
MY SQL
 
Rapid postgresql learning, part 3
Rapid postgresql learning, part 3Rapid postgresql learning, part 3
Rapid postgresql learning, part 3
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql Statments
 
Joins.ppt
Joins.pptJoins.ppt
Joins.ppt
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
MS SQLSERVER:Joining Databases
MS SQLSERVER:Joining DatabasesMS SQLSERVER:Joining Databases
MS SQLSERVER:Joining Databases
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Sq lite module7
Sq lite module7Sq lite module7
Sq lite module7
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 

More from EllenGracePorras

Structured Query Language (SQL).pptx
Structured Query Language (SQL).pptxStructured Query Language (SQL).pptx
Structured Query Language (SQL).pptx
EllenGracePorras
 

More from EllenGracePorras (18)

Lesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptxLesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptx
 
Lesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptxLesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptx
 
Geographic Information System(GIS).pptx
Geographic  Information System(GIS).pptxGeographic  Information System(GIS).pptx
Geographic Information System(GIS).pptx
 
Geographic Information Systems (GIS).pptx
Geographic  Information Systems (GIS).pptxGeographic  Information Systems (GIS).pptx
Geographic Information Systems (GIS).pptx
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Advanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptxAdvanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptx
 
Advanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxAdvanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptx
 
Advanced Database Systems - Presentation 2.pptx
Advanced Database Systems - Presentation 2.pptxAdvanced Database Systems - Presentation 2.pptx
Advanced Database Systems - Presentation 2.pptx
 
Advanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptxAdvanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptx
 
Structured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxStructured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptx
 
SQL Where Clause.pptx
SQL Where Clause.pptxSQL Where Clause.pptx
SQL Where Clause.pptx
 
SQL Statements.pptx
SQL Statements.pptxSQL Statements.pptx
SQL Statements.pptx
 
Information Management
Information ManagementInformation Management
Information Management
 
Lesson 2 HCI 2.pptx
Lesson 2 HCI 2.pptxLesson 2 HCI 2.pptx
Lesson 2 HCI 2.pptx
 
Lesson 2 HCI 2.pdf
Lesson 2 HCI 2.pdfLesson 2 HCI 2.pdf
Lesson 2 HCI 2.pdf
 
.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptx.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptx
 
CC 1/L Introduction.pptx
CC 1/L Introduction.pptxCC 1/L Introduction.pptx
CC 1/L Introduction.pptx
 
Structured Query Language (SQL).pptx
Structured Query Language (SQL).pptxStructured Query Language (SQL).pptx
Structured Query Language (SQL).pptx
 

Recently uploaded

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Advanced Database Systems.pptx

  • 2. OVERVIEW In this subject, you will learn how to retrieve data from multiple tables using one SQL statement. You will see how tables can be joined together and how similar results are obtained using different approaches, including the joints and sub queries. It is important that you understand how to query multiple tables for generating appropriate reports in the creation of an information system.
  • 3. After studying our lesson, you should be able to: • Learn the capabilities of SQL, in querying multiple tables by joining these tables. • Connect Database to Visual Basic • Create a simple Database System
  • 4. JOINING MORE THAN ONE TABLES You already learned from your previous database course how to generate reports from a single table. This time we will explore the capabilities of SQL, in querying multiple tables by joining these tables. Joining can be performed by combining two or more tables together by finding rows that match the values in the common columns. These common columns in joined tables are usually the primary key of the parent table and the foreign key of the child table in a one-to-many relationships. 4
  • 6. JOIN typically combines rows with equal values for the specified columns. Usually, one table contains a primary key, which is a column or columns that uniquely identify rows in the table (the cat_id column in the cat table). The other table has a column or columns that refer to the primary key columns in the first table (the cat_id column in the toy table). Such columns are foreign keys. The JOIN condition is the equality between the primary key columns in one table and columns referring to them in the other table.
  • 7. JOIN returns all rows that match the ON condition. JOIN is also called INNER JOIN. It is when you want to return only records having pair on both sides. Example syntax: SELECT * FROM Toy JOIN Cat ON TOY.CAT_ID = CAT.CAT_ID; 7
  • 9. There is also another, older syntax, but it isn't recommended. List joined tables in the FROM clause and place the conditions in the WHERE clause. SELECT * FROM Toy, Cat WHERE Toy.Cat_ID = Cat.Cat_ID; 9
  • 10. JOIN CONDITIONS 10 The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret the JOIN condition; it only checks if the rows satisfy the given condition. To refer to a column in the JOIN query, you have to use the full column name: first the table name, then a dot (.) and the column name: ON Cat.Cat_ID = Toy.Cat_ID
  • 11. JOIN CONDITIONS 11 The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret the JOIN condition; it only checks if the rows satisfy the given condition. To refer to a column in the JOIN query, you have to use the full column name: first the table name, then a dot (.) and the column name: ON Cat.Cat_ID = Toy.Cat_ID You can omit the table name and use just the column name if the name of the column is unique within all columns in the joined tables.
  • 12. NATURAL JOIN 12 If the tables have columns with the same name, you can use NATURAL JOIN instead of JOIN. SELECT * FROM Toy NATURAL JOIN Cat; The common column appears only once in the result table. Note: NATURAL JOIN is rarely used in real life.
  • 13. LEFT JOIN 13 LEFT JOIN returns all rows from the left table with matching rows from the right table. Rows without a match are filled with NULLs. LEFT JOIN is also called LEFT OUTER JOIN. When you need all records from the left table no matter if they have pair in the right table or not. SELECT * FROM Toy LEFT JOIN Cat ON Toy.Cat_ID=Cat.Cat_ID;
  • 15. 15 RIGHT JOIN RIGHT JOIN returns all rows from the right table with matching rows from the left table. Rows without a match are filled with NULLs. RIGHT JOIN is also called RIGHT OUTER JOIN. SELECT * FROM Toy RIGHT JOIN Cat ON Toy.Cat_ID= Cat.Cat_ID;
  • 16. 16 FULL JOIN (FULL OUTER JOIN) FULL JOIN returns all rows from the left table and all rows from the right table. It fills the non-matching rows with NULLs. FULL JOIN is also called FULL OUTER JOIN. SELECT * FROM Toy FULL JOIN Cat ON Toy.Cat_ID = Cat.Cat_ID; Note: Full Outer Join in MySQL • MySQL doesn’t have FULL OUTER JOIN • Simulate using LEFT and RIGHT JOINs
  • 17. 17 FULL JOIN (FULL OUTER JOIN) Example Syntax: SELECT * FROM Toy LEFT JOIN Cat ON Toy.Cat_ID= Cat.Cat_ID UNION SELECT * FROM Toy RIGHT JOIN Cat ON Toy.Cat_ID= Cat.Cat_ID
  • 18. 18 CROSS JOIN CROSS JOIN returns all possible combinations of rows from the left and right tables. Example Syntax: SELECT * FROM Toy CROSS JOIN Cat; Other syntax: SELECT * FROM Toy, Cat;
  • 20. COLUMN AND TABLE ALIASES 20 Aliases give a temporary name to a table or a column in a table. Mom_ID Owner_ID Owner As o Owner_ID Owner_Name 1 John Smith 2 Danielle Davis
  • 21. 21 A column alias renames a column in the result. A table alias renames a table within the query. If you define a table alias, you must use it instead of the table name everywhere in the query. The AS keyword is optional in defining aliases. Example Syntax: SELECT o.owner_name, c.cat_name FROM cat AS c JOIN owner AS o ON c.owner_id = o.owner_id;
  • 22. 22 SELF JOIN You can join a table to itself, for example, to show a parent-child relationship. Mom_ID Owner_ID Mom_ID Owner_ID
  • 23. 23 Each occurrence of the table must be given a different alias. Each column reference must be preceded with an appropriate table alias. Example Syntax: SELECT child.cat_name AS child_name, mom.cat_name AS mom_name FROM cat AS child JOIN cat AS mom ON child.mom_id = mom.cat_id;
  • 24. 24 MULTIPLE JOINS To perform joining multiple tables, you need to understand the SQL JOIN concepts clearly. You need to understand how different joins such as INNER JOIN, LEFT JOIN, RIGHT JOIN, CROSS JOIN, and FULL JOIN work. Moreover, understanding table relationships such as one-to-one, one-to-many, and many-to-many as well as primary key and foreign key will help to understand this topic. So, make sure that you have a good understanding of these points.
  • 25. 25 MULTIPLE JOINS You can join more than two tables together. First, two tables are joined, then the third table is joined to the result of the previous joining. Owner As o Owner_ID Owner_Name 1 John Smith 2 Danielle Davis
  • 26. 26 JOIN & JOIN Example Syntax: SELECT t.toy_name, c.cat_name, o.owner_name FROM toy t JOIN cat c ON t.cat_id = c.cat_id JOIN owner o ON c.owner_id = o.owner_id;
  • 27. 27 JOIN & LEFT JOIN Example Syntax: SELECT t.toy_name, c.cat_name, o.owner_name FROM toy t JOIN cat c ON t.cat_id = c.cat_id LEFT JOIN owner o ON c.owner_id = o.owner_id;
  • 28. 28 LEFT JOIN & LEFT JOIN Example Syntax: SELECT t.toy_name, c.cat_name, o.owner_name FROM toy t LEFT JOIN cat c ON t.cat_id = c.cat_id LEFT JOIN owner o ON c.owner_id = o.owner_id;
  • 30. 30 OrderID PK Order_Name CustomerID FK 10308 Keyboard 1001 10309 Mouse 1002 10310 Mouse Null 10311 Monitor 1004 Table: Orders CustomerID CustomerName Address 1001 Jose Bautista Taguig City 1002 Allan Williams Pasay City 1003 Anna Marie Ruiz Mandaluyong City Table: Customers Use the following Join Statements: 1. Natural Join 2. Left Join 3. Right Join 4. Full Join 5. Cross Join
  • 31. 31 OrderID PK OrderItem FK CustomerID SupplierID FK OrderDate 10308 Mouse 1001 1014 2023-08-20 10309 Keyboard 1002 1037 2023-08-23 10310 Monitor 1003 1022 2023-08-29 Table: Orders CustomerID PK CustomerName Address 1001 Jose Bautista Taguig City 1002 Allan Williams Pasay City 1003 Anna Marie Ruiz Mandaluyong City Table: Customers SupplierID PK SupplierName Address 1014 Electronico PPC 1037 Du Ek Sam Narra 1022 Teslinque PPC Table: Suppliers Use the following Join Statements 1. Join & Join 2. Join & Left Join 3. Left join & Left Join
  • 32. REFERENCES 32 • Hoffer, J. Rames, V., Topi, H. (2013) Modern Database Management, 11th Ed New Jersey Pearson Education, Inc • Price, J. (2008) Oracle Database l1g SQL: Master SOL and PL QL in the Oracle Database. New York: McGraw-Hill. • AMA Online University MIT 412. Advanced Database Systems
  • 33. THANK YOU Ellen Grace D. Porras egporras@psu.palawan.edu.ph