Control Flow Functions (Slides)
Control Flow Functions (Slides)
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
2
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
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.
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
Start
…
IF(ISNULL(Hhc_rship),
‘Head’,
False ISNULL True
Hhc_rship (Hhc_rship)
)
AS New_hhc_rship
…;
Hhc_rship “Head”
5
Control flow functions
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.
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
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
… False
End
12
Control flow functions
|
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:
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
End
19
Control flow functions
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;