ELSE
ELSE is a clause in SQL that is used within the CASE statement. It provides an alternative result when none of the preceding WHEN conditions are met.
Example
Output
Explanation
The given conditional code first checks if @num
is greater than 15. Since @num
is 10, the condition evaluates to false. Consequently, the control flows to the ELSE
part of the statement, and the phrase ‘The number is not more than 15’ is printed as the output.
Example
Output
Explanation
This SQL code checks the hire_date
of each record in the employee
table. If the hire_date
falls between 2015 and 2020, it outputs ‘Recent hire’. For all other dates, it outputs ‘Old hire’. The CASE clause is used within a SQL SELECT statement to specify a condition, the ELSE clause provides the alternative output when the condition is not met.
Example
Output
Explanation
In the provided code, the IF
statement tests whether 1 is greater than 2. Since the statement is false, the code in the ELSE
block (PRINT '1 is not greater than 2'
) is executed. Thus, the output ‘1 is not greater than 2’ is displayed.
Example
Output
Explanation
In the given example, we have declared a variable n
and assigned a value of 10. The IF-ELSE
statement checks if n
is greater than 20. If true, it outputs ‘The number is greater than 20’. However, if n
is not greater than 20, the ELSE
condition triggers and outputs ‘The number is not greater than 20’. Since n
is 10, it falls into the ELSE
condition, and we see ‘The number is not greater than 20’ in the output.
Example
Output
Explanation
In this example, the CASE statement is used to categorize employee’s salaries into ‘Low’, ‘Medium’, or ‘High’ salary brackets. The ELSE clause assigns ‘High’ to all remaining cases where salary is more than 80000.