Control Flow and Joins
MySQL
Session 7
Dr. Hetal Gandhi
Senior Facilitator
Email:
[email protected]Welcome to the Digital Regenesys course in
R Programming Session 7
Control Flow and Joins
We will be starting shortly …
REGENESYS INTEGRATED LEADERSHIP AND
MANAGEMENT MODEL
Holistic focus on the individual (SQ,
EQ, IQ, and PQ)
Interrelationships are dynamic
between individual, team, institution
and the external environment
(systemic)
Strategy affects individual, team,
organisational, and environmental
performance
Delivery requires alignment of strategy,
structure, systems and culture
REGENESYS GRADUATE ATTRIBUTES
Recap
Aggregate Queries
Date and Time Functions
Agenda
Strings Functions for queries
Control Flows
• IF
• IFNULL
• NULLIF
• CASE
Joins
Resources needed
MySQL Workbench and server
Portal resources
Date, Time and
String Functi ons in
MySQL
Date Functi ons
https://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm
https://www.javatpoint.com/mysql-date-time
CURDATE(), CURRENT_DATE(), CURRENT_DATE
This function returns the current date
CURTIME(), CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP This function returns the current time
NOW()This function returns the current date and time
DAYOFMONTH()This function returns the day of the month (1-31)
DAYOFWEEK()This function returns the weekday index of the argument
DAYOFYEAR()This function returns the day of the year (1-366)
Queries using Date Time Functi ons
Retrieve current time
SELECT CURTIME() AS Cur_Time;
Retrieve current date and time
SELECT NOW() AS Current_DateTime;
Retrieve employees and extract the year of joining
SELECT PersonID, FirstName, LastName, DateOfJoining,
YEAR(DateOfJoining) AS Joining_Year FROM empd;
Retrieve employees and extract the day of joining
SELECT PersonID, FirstName, LastName, DateOfJoining,
DAY(DateOfJoining) AS Joining_Day FROM empd;
Retrieve employees and find how many years they
have been in the company
SELECT PersonID, FirstName, LastName, DateOfJoining,
TIMESTAMPDIFF(MONTH, DateOfJoining, CURDATE())
AS Months_In_Company FROM empd;
String Functi ons
https://www.tutorialspoint.com/mysql/mysql-string-functions.htm
ASCII() Returns numeric value of left-most character
CONCAT()Returns concatenated string
FORMAT() Returns a number formatted to specified number of decimal places
LENGTH()Returns the length of a string in bytes
LTRIM()Removes leading spaces
REPLACE()Replaces occurrences of a specified string
REVERSE()Reverses the characters in a string
Queries using String Functi ons
Retrieve ASCII value of the first character of each employee's first name
SELECT FirstName, ASCII(FirstName) AS ASCII_Value FROM empd;
Concatenate FirstName and LastName with a space in between
SELECT FirstName, LastName, CONCAT(FirstName, ' ', LastName) AS Full_Name FROM empd;
Format Age with two decimal places
SELECT FirstName, Age, FORMAT(Age, 2) AS Age_Formatted FROM empd;
Find the length of each employee's FirstName
SELECT FirstName, LENGTH(FirstName) AS Name_Length FROM empd;
Remove leading spaces from the Address column
SELECT Address, LTRIM(Address) AS Trimmed_Address FROM empd;
Replace 'St' with 'Street' in Address
SELECT Address, REPLACE(Address, 'St', 'Street') AS Updated_Address FROM empd;
Reverse the characters in LastName
SELECT LastName, REVERSE(LastName) AS Reversed_LastName FROM empd;
Control Flows in
MySQL
Control Flow Statement
• Can use control conditions in SQL query
• The control flow function evaluates the condition specified in it
• The output generated by them can be a true, false, static value,
or column expression
IF Clause
The IF function
returns a value YES when the given condition evaluates to true,
where as
returns a NO value when the condition evaluates to false
IF Clause
Check if Age is greater than 30, return 'Senior' otherwise
'Junior’
SELECT FirstName, Age, IF(Age > 30, 'Senior', 'Junior') AS
Category FROM empd;
Check if City is 'New York', return 'USA' otherwise 'Other
Country’
SELECT FirstName, City, IF(City = 'New York', 'USA', 'Other
Country') AS Location FROM empd;
IFNULL
The IFNULL function accepts two expressions, and if the first
expression is not null, it returns the first arguments. If the first
expression is null, it returns the second argument.
IFNULL (Expression1, Expression2)
It returns expression1 when the expression1 is not null.
Otherwise, it will return expression2.
IFNULL
If Address is NULL, replace it with 'Not Provided’
SELECT FirstName, IFNULL(Address, 'Not Provided') AS
Address_Info FROM empd;
If Age is NULL, assume default age as 25
SELECT FirstName, IFNULL(Age, 25) AS Updated_Age FROM
empd;
NULLIF
The NULLIF function accepts two expressions, and if the first
expression is equal to the second expression, it returns the NULL.
Otherwise, it returns the first expression.
NULLIF (Expression1, Expression2)
It returns Null when expression1 is equal to expression2.
Otherwise, it will return expression1.
NULLIF
Compare Age and 30, return NULL if they are equal,
otherwise return Age
SELECT FirstName, Age, NULLIF(Age, 30) AS Age_Check
FROM empd;
Compare FirstName and LastName, return NULL if they are
the same, otherwise return FirstName
SELECT FirstName, LastName, NULLIF(FirstName, LastName)
AS Name_Check FROM empd;
CASE
MySQL CASE expression is a part of the control flow function that
provides us to write an if-else or if-then-else logic to a query.
CASE value
WHEN [compare_value] THEN result
[WHEN [compare_value] THEN result ...]
[ELSE result]
END
It returns the result when the first compare_value comparison becomes
true. Otherwise, it will return the else clause.
CASE
Categorize employees based on Age
SELECT FirstName, Age,
CASE
WHEN Age < 30 THEN 'Young'
WHEN Age BETWEEN 30 AND 40 THEN 'Middle-Aged'
ELSE 'Senior'
END AS Age_Category
FROM empd;
What is the purpose of control flow
statements in SQL queries?
To evaluate conditions in a query
To generate true or false values
To perform mathematical calculations
A. To retrieve data from multiple tables
Correct answer: A
Joins in MySQL
Joins
• Used to combine rows from two or more tables, based on a
related column between them.
Types of joins
• CROSS JOIN: Returns all records from both tables
• INNER JOIN: Returns records that have matching values in both tables
• LEFT JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT JOIN: Returns all records from the right table, and the matched
records from the left table
• FULL OUTER JOIN: Returns all records from both the tables
Customers
CID Name Country ………
Cross Join
1 John Germany ……… Customers Orders
2 Tom USA ………
CID Name Country …… ID CI Order # Order Date ……
3 Smith India ……… D
4 James USA ……… 1 John Germany … 1 2 OD111 2022-09- …
5 Dennis Kenya ……… 1 08
6 Thomas South Africa ……… 1 John Germany … 2 3 OD222 2022-07- …
2 06
1 John Germany … 3 5 OD444 2021-10- …
4 20
Orders
1 John Germany … 4 7 OD555 2022-11- …
ID CID Order # Order Date … 5 10
2 Tom USA …… 1 2 OD111 2022-09- …
… 1 08
1 2 OD1111 2022-09-08 … 2 Tom USA …… 2 3 OD222 2022-07- …
… 2 06
2 3 OD2222 2022-07-06 …
- - - - - - - - -
3 5 OD4444 2021-10-20 …
4 7 OD5555 2022-11-10 …
Customers
CID Name Country ………
1 John Germany ………
2 Tom USA ……… Inner Join
3 Smith India ………
4 James USA ……… Customers Orders
5 Dennis Kenya ………
6 Thomas South Africa ………
CID Name Country …… ID CID Order # Order Date ………
Orders 2 Tom USA …… 1 2 OD1111 2022-09-08 ……
ID CID Order # Order Date …
3 Smith India …… 2 3 OD2222 2022-07-06 ……
1 2 OD1111 2022-09-08 …
2 3 OD2222 2022-07-06 … 5 Dennis Kenya …… 3 5 OD4444 2021-10-20 ……
3 5 OD4444 2021-10-20 …
4 7 OD5555 2022-11-10 …
Customers
CID Name Country ………
Left Join
1 John Germany ……… Customers Orders
2 Tom USA ………
3 Smith India ……… CI Name Country …… ID CID Order # Order Date ………
D
4 James USA ………
5 Dennis Kenya ……… 1 John German …… NULL NULL NULL NULL NULL
y
6 Thomas South Africa ………
2 Tom USA …… 1 2 OD1111 2022-09-08 ………
3 Smith India …… 2 3 OD2222 2022-07-06 ………
Orders
4 James USA …… NULL NULL NULL NULL NULL
ID CID Order # Order Date …
5 Dennis Kenya …… 3 5 OD4444 2021-10-20 ………
1 2 OD1111 2022-09-08 …
6 Thom South …… NULL NULL NULL NULL NULL
2 3 OD2222 2022-07-06 … as Africa
3 5 OD4444 2021-10-20 …
4 7 OD5555 2022-11-10 …
Customers
CID Name Country ………
1 John Germany ………
2 Tom USA ………
3 Smith India ……… Right Join
4 James USA ……… Customers Orders
5 Dennis Kenya ………
6 Thomas South Africa ……… CID Name Country ……… ID CID Order # Order Date ………
2 Tom USA ……… 1 2 OD1111 2022-09-08 ………
Orders 3 Smith India ……… 2 3 OD2222 2022-07-06 ………
ID CID Order # Order Date …
5 Denni Kenya ……… 3 5 OD4444 2021-10-20 ………
s
1 2 OD1111 2022-09-08 … NUL NULL NULL NULL 4 7 OD5555 2022-11-10 ………
L
2 3 OD2222 2022-07-06 …
3 5 OD4444 2021-10-20 …
4 7 OD5555 2022-11-10 …
Customers Full Outer Join
CID Name Country ………
1 John Germany ………
Customers Orders
2 Tom USA ……… CID Name Country …… ID CI Order # Order Date ……
3 Smith India ……… D
4 James USA ……… 1 John Germany …
5 Dennis Kenya ………
6 Thomas South Africa ……… 2 Tom USA … 1 2 OD111 2022-09- ……
1 08
3 Smith India … 2 3 OD222 2022-07- ……
2 06
Orders 4 James USA …
ID CID Order # Order Date …
5 Dennis Kenya … 3 5 OD444 2021-10- …
4 20
1 2 OD1111 2022-09-08 … 6 Thomas South …
Africa
2 3 OD2222 2022-07-06 …
4 7 OD555 2022-11- …
3 5 OD4444 2021-10-20 … 5 10
4 7 OD5555 2022-11-10 …
Types of joins
Acti vity
Which of the following JOIN types retrieves all rows from the left table and
matching rows from the right table, filling in NULLs for unmatched rows?
A. INNER JOIN
B. LEFT JOIN
C. RIGHT JOIN
D. FULL OUTER JOIN
Acti vity
In a scenario where Table A has 100 rows and
Table B has 120 rows, an INNER JOIN between
them can return at the most ___________
records.
Summary
String Functions for Queries
Control Flows
Joins and its types
Contents for the next session
Retrieve data from .csv or excel file.
Python MYSQLConnector to perform operations on the
database
Project Introduction
Thank You