Advance Function SQL
CASE
function¶
The CASE statement goes through conditions and return a value when the first condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it will stop reading and return the result.
If no conditions are true, it will return the value in the ELSE clause.
If there is no ELSE part and no conditions are true, it returns NULL.
Syntax CASE¶
Example
IF()
function¶
The IF()
function returns a value if a condition is TRUE, or another value if a condition is FALSE.
Syntax IF()¶
Example
SET @var_name
User-defined Variables¶
You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another.
User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, ., _ , and $.
User variable names are not case-sensitive. Names have a maximum length of 64 characters.
One way to set a user-defined variable is by issuing a SET
statement:
For SET
, either =
or :=
can be used as the assignment operator.
When making an assignment in this way, you must use :=
as the assignment operator; =
is treated as the comparison operator in statements other than SET
.
Here ans example of the usage of this "user-defined variables", this is the solution to the challenge Occupations of Hacker Ranks