100% found this document useful (1 vote)
29 views

Session 4 - Data Analysis For Complex Structures

This document provides an introduction to databases and the SQL programming language. It outlines 8 sessions that will cover topics like database design, data management using SQL, data analysis, and a final project presentation. Session topics include introduction to SQL, coding in SQL, database management techniques, data manipulation, analysis for complex structures, and data visualization. Homework review is also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
29 views

Session 4 - Data Analysis For Complex Structures

This document provides an introduction to databases and the SQL programming language. It outlines 8 sessions that will cover topics like database design, data management using SQL, data analysis, and a final project presentation. Session topics include introduction to SQL, coding in SQL, database management techniques, data manipulation, analysis for complex structures, and data visualization. Homework review is also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

WELCOME TO CFG

YOUR INTRODUCTION TO
DATABASES & SQL PROGRAMMING LANGUAGE

TECH OPENS UP LIMITLESS OPPORTUNITIES FOR GIRLS


R SE Y
O U NE
C UR
JO

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

• Joins – create complex data structures within DB

• Data subset unions

• SQL programming - subqueries


04
HOMEWORK • The main objective of week 3 homework was to write various queries
REVIEW using logical operators, keywords, aggregation and sorting
techniques to analyse sales data for a chain of stores.

• Let’s walk through and review the correct answers together.


• SQL JOIN combines columns from two or more tables, based on a related column
between them, in a single result set.

• There are many different types of JOINs:

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

• It returns rows when there is at least one match in both tables

INNER JOIN • Cannot deal with 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
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_name2> <alias2> email_address e


ON ON clause
<alias1>.<column_name> =
ON
<alias2>.<column_name>; c.customer_id =
e.email_address_customer_id;
• All rows from the left side with the matching rows from the right side will be
returned
LEFT OUTER*
JOIN • If there are no columns matching in the right 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

The ‘OUTER’ keyword is optional


X

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

The ‘OUTER’ keyword is optional


X

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

FROM <table_name1> <alias1> FROM customers c


RIGHT OUTER JOIN
RIGHT JOIN RIGHT JOIN
<table_name2> <alias2>
email_address e
ON ON clause
<alias1>.<column_name> = ON
<alias2>.<column_name>;
c.customer_id =
e.email_address_customer_id;
• Combines left outer join and right outer join

• 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!

• Type along with your instructor ☺.


• Simplest, but quite inefficient

• 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

• It is especially useful when we deal with a table that contains hierarchical


data.
EQUI JOIN NON-EQUI JOIN

Specific type of a JOIN Specific type of a JOIN


that uses the equal sign as that does not use the
the comparison equal sign as the
comparison
TYPES OF
JOINS
NATURAL JOIN KEYWORD JOIN

Joins two (or more) tables Uses keyword for joining


based on all columns in tables when columns have
the two tables with the the same name in both
same name tables
PRACTICE • We continue practicing our JOINs queries. Let’s look at the result produced by
the CROSS JOIN based on our fruit tables.

• Let’s create a small table with hierarchical data and perform a self join to see
how useful it can be.

• Type along with your instructor ☺.


• MySQL UNION operator allows to combine two or more result sets of queries into a single
result set.

• 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 must be enclosed with parentheses

• 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

• Subqueries can be nested within other subqueries

• Subqueries are used in WHERE, HAVING, FROM and SELECT clause


PRACTICE • We are going to practice more by writing queries with UNION operators (based
on the fruit tables).

• In addition to that, we are going to write our very first subquery using our
customer database.

• Type along with your instructor ☺.


• We will start doing this in class together as part of our practice and the task would be
to complete writing the queries, i.e. finishing writing SQL code for answering these
questions.

• 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

You might also like