Chapter-5: SQL Case Statement/Expression with Examples | WHEN THEN ELSE in SQL | SQL Tutorial

preview_player
Показать описание


A CASE expression in SQL is a powerful and flexible way to perform conditional logic within a query. It allows you to evaluate a set of conditions and return different values based on those conditions. CASE expressions are often used in SELECT statements to create calculated columns or to control the flow of data based on certain criteria.

Here's a breakdown of the CASE expression:

Simple CASE Expression:

In a simple CASE expression, you specify an expression to evaluate, and then you define a list of conditions and the corresponding values to return if the expression matches each condition.
It's a way of performing a switch-case-like operation in SQL.
Searched CASE Expression:

In a searched CASE expression, you specify multiple conditions, and for each condition, you provide a Boolean expression that determines whether the condition is met. The first true condition encountered is the one that determines the result.
Result Values:

For both types of CASE expressions, you specify a result value or expression to return when a condition is met.
ELSE Clause:

You can also include an ELSE clause to provide a default value or expression to return when none of the conditions are met.
Here's a generic structure of a CASE expression:

sql
Copy code
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE default_result
END
CASE expressions are used in a variety of scenarios, such as creating custom labels for data, categorizing data into different groups, or performing calculations based on conditions. They are a valuable tool for data transformation and reporting in SQL.
Рекомендации по теме
Комментарии
Автор

Select employee number, first name, job title and job title abbreviation from employees table based on following conditions.
If job title is one among the below conditions, then job title abbreviation column should show below forms.
President then “P”
Sales Manager / Sale Manager then “SM”
Sales Rep then “SR”
Containing VP word then “VP”

rahulpawar
Автор

sir..in last 2 questions it has asked to give hike to full time employees but in result all employees are showing. do we have to mention status = 'fulltime'??

romitgame
Автор

The answer of the last question is wrong


TG_KaranJod