0% found this document useful (0 votes)
9 views

Control Flow Functions (Slides)

un servy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Control Flow Functions (Slides)

un servy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Control flow functions

Control flow functions


Please do not copy without permission. © ExploreAI 2023.
Control flow functions

Data overview

| We will use the following Households_individuals table which contains certain information
about the individuals in all households in Kenya collected during a household survey in 2020.

Marital_
ID Hh_ID Sex Age Weight Schooling Current_ed Spouse Hhc_rship
status
3901 2401 Male 9 29.01 Yes Class 3 Single N/A Grandchild

3821 2789 Male 22 67.00 Yes Third year Single N/A Other relative

3961 2233 Female 35 59.00 Yes Masters Married Yes Partner

3741 2560 Female 14 45.22 Yes Class 8 Single N/A Child

3661 2934 Male 69 77.00 No N/A Married Yes NULL

3921 2006 Female 16 45.99 Yes Form 2 Single N/A Child

2
Control flow functions

Control flow functions

|
Control flow functions are used to implement conditional logic and control the flow of
execution within SQL queries. They allow us to perform different actions or return different
values based on specific conditions.

Start
Commonly used control flow functions in SQL are the:

● IF function
● CASE statement
False True
Condition

We can use control flow functions in various SQL


statements such as the SELECT, UPDATE, WHERE, Action_1 Action_2
ORDER BY, and the GROUP BY clause.

End

3
Control flow functions

IF function

|
The IF() function evaluates a given condition and returns a particular value if a condition is
TRUE, or another value if a condition is FALSE. It can be used to conditionally retrieve or assign
values in a table based on certain conditions.

Basic syntax Flowchart

Start
SELECT
IF(condition,
value_if_true, False True
value_if_false Condition
)
FROM
Table_name;
value_if_false value_if_true

End

4
Control flow functions

IF function example


The Hhc_rship column indicates the relationship that an individual has with the household head. If an individual is
the household head, their value has been set to NULL. Letʼs use the IF function below to assign these NULL values
the string value “Headˮ.

Start

IF(ISNULL(Hhc_rship),
‘Head’,
False ISNULL True
Hhc_rship (Hhc_rship)
)
AS New_hhc_rship
…;
Hhc_rship “Head”

Weʼre saying when Hhc_rship is NULL, then we set


the value to ‘Headʼ but when it is not NULL, we set it to
the original value.
End

5
Control flow functions

IF function example


Query Output

SELECT ID Hhc_rship New_hhc_rship


ID,
Hhc_rship, 3901 Grandchild Grandchild
IF(ISNULL(Hhc_rship),
‘Head’, 3821 Other relative Other relative
Hhc_rship
3961 Partner Partner
)
AS New_hhc_rship
3741 Child Child
FROM
Household_individuals; 3661 NULL Head

3921 Child Child

6
Control flow functions

CASE statement

| The CASE statement allows us to apply multiple conditions that lead to different sets of actions
within a SQL query.

There are two types of CASE statements: For both types of CASE statements, once a match is
found or a condition is true, the function stops reading
1. The simple CASE statement
and returns the corresponding result.
A list of values is compared to a given CASE
expression.
Optionally, an ELSE clause can be included, which
specifies the value to be returned if no match is found
2. The searched CASE statement
or no conditions are true.
A list of conditions is evaluated to be
either TRUE or FALSE. If there is no ELSE clause and no conditions are true,
NULL is returned.

7
Control flow functions

1. Simple CASE

|
In this form of the CASE function, a CASE expression is specified, and its value compared to
each of a set of values. When a match is found, the corresponding result is returned. If none of
the values matches the expression, the optional ELSE result is returned.

Basic syntax Case expression: The expression to be compared to


value_1, value_2,...value_N.
SELECT
CASE Case_Expression
WHEN value_1 THEN result_1 value_1, value_2, ...value_N: The values that are to be
WHEN value_2 THEN result_2 compared to the CASE expression in the same order as they
. are listed.
.
. result_1, result_2, ...result_N: The result to be
WHEN value_N THEN result_N returned once the corresponding value matches the CASE
ELSE result expression.
END AS Alias_name
FROM ELSE result: The result to be returned if no values are
Table_name; matched.

8
Control flow functions

1. Simple CASE
Start
Flowchart

CASE Case_Expression
True
WHEN value_1 THEN result_1 Case_Expression result_1
= value_1
WHEN value_2 THEN result_2
. False
.
True
. Case_Expression result_2
= value_2
WHEN value_N THEN result_N
ELSE result False
END AS Alias_name
True
…; Case_Expression result_N
= value_N

False

ELSE result

End

9
Control flow functions

2. Searched CASE

|
This form of the CASE function evaluates a series of conditions and returns a result based on
the first condition that evaluates to true. If none of the conditions is true, the optional ELSE
result is returned.

Basic syntax

SELECT condition_1, condition_2, ...condition_N: The


CASE conditions that are to be evaluated in the same order as
WHEN condition_1 THEN result_1 they are listed.
WHEN condition_2 THEN result_2
.
. result_1, result_2, ...result_N: The corresponding
. value to be returned for each condition if it evaluates to
WHEN condition_N THEN result_N true.
ELSE result
END AS Alias_name
FROM ELSE result: The result to be returned if no condition is
Table_name; true.

10
Control flow functions

2. Searched CASE
Start
Flowchart

CASE
WHEN condition_1 THEN result_1 True
condition_1 result_1
WHEN condition_2 THEN result_2
.
False
.
. True
condition_2 result_2
WHEN condition_N THEN result_N
ELSE result
False
END AS Alias_name
…; True
condition_N result_N

False

ELSE result

End

11
Control flow functions

CASE statement example


Start
Suppose we want to categorise the
individuals in our table into different age
True Age_group =
groups representing the various stages of Age <= 12
“0-12”
life. We can use the following CASE statement
False
to perform the categorisation.
True Age_group =
Age <= 19
“13-19”

… False

CASE Age <= 39


True Age_group =
“20-39”
WHEN Age <= 12 THEN “0-12”
WHEN Age <= 19 THEN “13-19” False

WHEN Age <= 39 THEN “20-39” Age <= 59


True Age_group =
“40-59”
WHEN Age <= 59 THEN “40-59”
ELSE ”60+” False

END AS Age_group Age_group =


“60+”
…;

End

12
Control flow functions

CASE statement example


Query Output

SELECT ID Age Age_group


ID,
Age, 3901 9 0-12
CASE
WHEN Age <= 12 THEN “0-12” 3821 22 20-39
WHEN Age <= 19 THEN “13-19”
WHEN Age <= 39 THEN “20-39” 3961 35 20-39
WHEN Age <= 59 THEN “40-59”
ELSE ”60+” 3741 14 13-19
END AS Age_group
FROM
3661 69 60+
Household_individuals;
3921 16 13-19
Control flow functions

Nested conditional statements

|
This refers to the use of one or more conditional statements, like the IF and CASE control flow
functions, within another conditional statement. This enables us to introduce multiple levels of
conditions or to define the logic in a more granular way.

Nested conditional statements can exist in many different variations. We will look at examples of the following:

1. Nested IF statement: 2. Nested IF and CASE statement:

The use of an IF function inside of another IF


The use of an IF function inside of a CASE statement.
function.

Say we wish to analyse the distribution of students at Suppose we want to categorise all the individuals
the various education levels at that time. We can use based on their age and gender into the following
the following nested IF condition to specify the groups: "Young female", "Young male", "Adult female",
education level for each individual that is currently and "Adult male." Letʼs use the following nested IF
enrolled in school. and CASE statement to perform the categorisation.
14
Control flow functions

Nested IF example


IF(Current_ed IN (“PP1”, PP2”),
“Pre-school”,
IF(Current_ed IN (“Class 1”, “Class 2”, “Class 3”, “Class 4”, “Class 5”, “Class 6”,
“Class 7”, “Class 8”),
“Primary”,
IF(Current_ed IN (“Form 1”, “Form 2”, “Form 3”, “Form 4”),
“Secondary”,
IF(Current_ed IN (“First year”, “Second year”, “Third year”, “Fourth year”,
“Fifth year”, “Sixth year”, “Masters”, “PHD”),
“Tertiary”,
“Other”
)
)
)
) AS Ed_level
…;
Control flow functions

Nested IF example
Start


IF(Current_ed IN (“PP1”, PP2”), True
Condition “Pre-school”
“Pre-school”,
IF(Current_ed IN (“Class 1”, “Class 2”, “Class 3”, “Class 4”
, “Class 5”, “Class 6”, “Class 7”, “Class 8”), False

“Primary”, True
Condition “Primary”
IF(Current_ed IN (“Form 1”, “Form 2”, “Form 3”, “Form
4”),
False
“Secondary”,
True
IF(Current_ed IN (“First year”, “Second year”, Condition “Secondary”
“Third year”, “Fourth year”, “Fifth year”,
“Sixth year”, “Masters”, “PHD”), False
“Tertiary”, True
“Other” Condition “Tertiary”
)
) False
)
“Other”
) AS Ed_level
…;
End

16
Control flow functions

Nested IF example
Query

SELECT
Schooling,
Current_ed,
IF(Current_ed IN (“PP1”, PP2”),
“Pre-school”,
IF(Current_ed IN (“Class 1”, “Class 2”, “Class 3”, “Class 4”, “Class 5”, “Class 6”, “Class 7”, “Class 8”),
“Primary”,
IF(Current_ed IN (“Form 1”, “Form 2”, “Form 3”, “Form 4”),
“Secondary”,
IF(Current_ed IN (“First year”, “Second year”, “Third year”, “Fourth year”, “Fifth year”, “Sixth year”,
“Masters”, “PHD”),
“Tertiary”,
“Other”
)
)
)
) AS Ed_level

FROM
Household_individuals
WHERE
Schooling == “Yes”;
Control flow functions

Nested IF example
Output

Schooling Current_ed Ed_level

Yes Class 3 Primary

Yes Third year Tertiary

Yes Masters Tertiary

Yes Class 8 Primary

Yes Form 2 Secondary


Control flow functions

Nested IF and CASE statements example


Start

CASE
WHEN age < 18 THEN True True
Age < 18 Sex = 'Female’ “Young
IF(Sex = “Female”, female”

“Young female”, False


“Young male”)
ELSE “Young male”
False
IF(Sex = “Female”,
“Adult female”, True
“Adult
“Adult male”) Sex = 'Female’
female”
END AS Age_category
False
…;
“Adult
male”

End

19
Control flow functions

Nested IF and CASE statements example


Query Output

SELECT
Sex, Sex Age Age_group
Age,
Male 9 Young male
CASE
WHEN age < 18 THEN
Male 22 Adult male
IF(Sex = “Female”,
“Young female”, Female 35 Adult female
“Young male”)
ELSE Female 14 Young female
IF(Sex = “Female”,
“Adult female”, Male 69 Adult male
“Adult male”)
END AS Age_category Female 16 Young female
FROM
Household_individuals;

You might also like