Session 4 - Data Analysis For Complex Structures
Session 4 - Data Analysis For Complex Structures
YOUR INTRODUCTION TO
DATABASES & SQL PROGRAMMING LANGUAGE
SESSION 7 SESSION 8
Database design Project
and data presentations
visualisation
SESSION 6
Data management
using SQL coding
techniques
SESSION 5 SESSION44
MODULE
Data manipulation Data analysis for for
analysis
with SQL complex structures
programming
language
SESSION 3
Data analysis
SESSION 1 SESSION 2
Introduction to SQL SQL coding and
programming Database
language and Data management
Science technique
DATA ANALYSIS FOR
COMPLEX STRUCTURES
MODULE 1: HTML
JOIN
• Inner Join • Self and Equi Join
• Outer Join • Cross Join
o Left Outer Join • Natural Join
o Right Outer Join • Straight Join
o Full Outer Join • Keyword Join
• Most typical JOIN, it joins column in first table to second table
Table 1 Table 2
ID VALUE ID VALUE
1 First 1 First
2 Second 2 Second
3 Third 3 Third
4 Fourth 6 Sixth
5 Fifth 7 Seventh
X
EX
A
NT
AM
SY
PL
E
What are our customers’ email addresses?
SELECT SELECT
<alias1>.<column_name>, c.name, c.surname,
<alias1>.<column_name>,
<alias2>.<column_name> e.email_address
FROM customers c
FROM <table_name1> <alias1>
INNER JOIN INNER JOIN INNER JOIN
Table 1 Table 2
ID VALUE ID VALUE
1 First 1 First
2 Second 2 Second
3 Third 3 Third
4 Fourth 6 Sixth
5 Fifth 7 Seventh
EX
A
NT
AM
SY
PL
What are our customers’ email addresses
E
including those that we don’t have an email
address for?
SELECT SELECT
<alias1>.<column_name>, c.name, c.surname,
<alias1>.<column_name>,
<alias2>.<column_name> e.email_address
FROM customers c
FROM <table_name1> <alias1>
LEFT JOIN LEFT JOIN LEFT OUTER JOIN
<table_name2> <alias2> email_address e
ON
<alias1>.<column_name> =
ON ON clause
<alias2>.<column_name>; c.customer_id =
e.email_address_customer_id;
• Opposite of the LEFT OUTER JOIN, i.e. all rows from the right side with the
matching rows from the left side will be returned
RIGHT OUTER*
JOIN • If there are no columns matching in the left table, it returns NULL values
Table 1 Table 2
ID VALUE ID VALUE
1 First 1 First
2 Second 2 Second
3 Third 3 Third
4 Fourth 6 Sixth
5 Fifth 7 Seventh
EX
A
NT
AM
SY
PL
What are the email addresses we have,
E
including those email that we don’t have a
matching customer for?
SELECT SELECT
<alias1>.<column_name>,
<alias1>.<column_name>, c.name, c.surname,
<alias2>.<column_name> e.email_address
• Returns rows from either table when the conditions are met and returns a null
value when there is no match
FULL OUTER*
JOIN • MySQL DOES NOT SUPPORT FULL OUTER JOIN SYNTAX!
• But we can simulate this join by combining LEFT and RIGHT joins with UNION
(don’t worry about it now – we learn more about UNIONs soon!
Table 1 Table 2
ID VALUE ID VALUE
1 First 1 First
2 Second 2 Second
3 Third 3 Third
4 Fourth 6 Sixth
The ‘OUTER’ keyword is optional 5 Fifth 7 Seventh
• We are going to create two simple tables and join them together using
PRACTICE different types of joins.
• The most important thing for us right now is to understand the join mechanism
and the SQL syntax that we need to write to perform a join.
• Let’s create two tables aka two fruit baskets and see how we can join them!
• There is no WHERE clause nor any condition for a join, hence all rows
from both tables are used
CROSS
JOIN • It returns a Cartesian Product – multiples of the record number of both
tables
Table 1 Table 2
ID VALUE ID VALUE
1 First 1 First
2 Second 2 Second
3 Third 3 Third
4 Fourth
• You can JOIN the table on itself! Sometimes it can be useful.
SELF JOIN
• No special syntax, but we must alias the table used with different aliases.
• We will have the same table on left and right side of this join.
Table
• Let’s create a small table with hierarchical data and perform a self join to see
how useful it can be.
• By default, UNION operator removes duplicate rows even if we don’t specify the DISTINCT
operator explicitly.
UNION
OPERATORS
• UNION ALL does not remove duplicate rows
• A subquery is a nested query where the results of one query can be used in
another query via a relational operator or aggregation function
• A subquery can have only one column in the SELECT clause if used in WHERE
clause
SUBQUERY
• An ORDER BY clause is not allowed in a subquery
• In addition to that, we are going to write our very first subquery using our
customer database.
• We are going to use the database called parts. These queries are a bit more
challenging than usual: they require joins and nested subqueries.
HOMEWORK
WRITE THE FOLLOWING QUERIES
1. Find the name and status of each supplier who supplies project J2
2. Find the name and city of each project supplied by a London-based supplier
3. Find the name and city of each project NOT supplied by a London-based supplier
4. Find the supplier name, part name and project name for each case WHERE a
supplier supplies a project with a part, BUT ALSO the supplier city, project city
AND part city are the same.
REFERENCE
MATERIALS
QUICK • JOINS are one of the staples in the SQL world. It is one of the most common
SUMMARY actions to perform when working with databases.
• There are many different types of joins and they are useful in its own way
(applicable for different tasks)
• JOINS make the relational model come to life by associating 2 tables together