Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Understanding CASE WHEN Syntax | CASE WHEN Basics and Categorization
Mastering CASE WHEN in SQL

bookUnderstanding CASE WHEN Syntax

Conditional logic is an essential part of working with data in SQL. Often, you need to categorize, flag, or transform your data based on specific conditions—such as labeling high and low sales, applying discounts, or grouping results. Unlike traditional programming languages, SQL does not use if statements for this purpose. Instead, you use the CASE WHEN expression to introduce branching logic directly into your queries. This allows you to return different values in your result set depending on the data in each row.

123456789
SELECT id, product, quantity, CASE WHEN quantity >= 10 THEN 'High' ELSE 'Low' END AS sales_label FROM sales;
copy

In the query above, each part of the CASE WHEN syntax plays a specific role in adding conditional logic to your SQL statements. The CASE keyword begins the conditional block. Each WHEN clause specifies a condition to evaluate—in this example, whether quantity is greater than or equal to 10. If this condition is met, the query returns the string "High" for that row. The ELSE clause provides a fallback value, returning "Low" when the condition is not satisfied. Finally, the END keyword closes the CASE block, and the result is given an alias sales_label for clarity in the output. This structure allows you to dynamically label or categorize your data based on any logic you define.

12345678910
SELECT id, product, quantity, CASE WHEN quantity >= 15 THEN 'High' WHEN quantity >= 8 THEN 'Medium' ELSE 'Low' END AS sales_label FROM sales;
copy

1. What is the primary purpose of the CASE WHEN statement in SQL?

2. Which keyword is used to end a CASE WHEN block?

3. Fill in the blanks for a basic CASE WHEN statement that labels sales as Bulk if quantity >= 50, and Standard otherwise.

question mark

What is the primary purpose of the CASE WHEN statement in SQL?

Select the correct answer

question mark

Which keyword is used to end a CASE WHEN block?

Select the correct answer

question-icon

Fill in the blanks for a basic CASE WHEN statement that labels sales as Bulk if quantity >= 50, and Standard otherwise.

SELECT id, product, quantity, quantity >= 50 'Bulk' 'Standard' AS sale_type FROM sales;
id | product | quantity | sale_type
---+------------+----------+-----------
1 | Laptop | 5 | Standard
2 | Smartphone | 10 | Standard
3 | Tablet | 7 | Standard
4 | Headphones | 15 | Standard
5 | Monitor | 3 | Standard
6 | Keyboard | 12 | Standard
7 | Mouse | 20 | Standard
8 | Printer | 2 | Standard
9 | Laptop | 4 | Standard
10 | Smartphone | 8 | Standard
11 | Tablet | 6 | Standard
12 | Monitor | 5 | Standard
13 | Headphones | 10 | Standard
14 | Keyboard | 9 | Standard
15 | Mouse | 18 | Standard

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookUnderstanding CASE WHEN Syntax

Stryg for at vise menuen

Conditional logic is an essential part of working with data in SQL. Often, you need to categorize, flag, or transform your data based on specific conditions—such as labeling high and low sales, applying discounts, or grouping results. Unlike traditional programming languages, SQL does not use if statements for this purpose. Instead, you use the CASE WHEN expression to introduce branching logic directly into your queries. This allows you to return different values in your result set depending on the data in each row.

123456789
SELECT id, product, quantity, CASE WHEN quantity >= 10 THEN 'High' ELSE 'Low' END AS sales_label FROM sales;
copy

In the query above, each part of the CASE WHEN syntax plays a specific role in adding conditional logic to your SQL statements. The CASE keyword begins the conditional block. Each WHEN clause specifies a condition to evaluate—in this example, whether quantity is greater than or equal to 10. If this condition is met, the query returns the string "High" for that row. The ELSE clause provides a fallback value, returning "Low" when the condition is not satisfied. Finally, the END keyword closes the CASE block, and the result is given an alias sales_label for clarity in the output. This structure allows you to dynamically label or categorize your data based on any logic you define.

12345678910
SELECT id, product, quantity, CASE WHEN quantity >= 15 THEN 'High' WHEN quantity >= 8 THEN 'Medium' ELSE 'Low' END AS sales_label FROM sales;
copy

1. What is the primary purpose of the CASE WHEN statement in SQL?

2. Which keyword is used to end a CASE WHEN block?

3. Fill in the blanks for a basic CASE WHEN statement that labels sales as Bulk if quantity >= 50, and Standard otherwise.

question mark

What is the primary purpose of the CASE WHEN statement in SQL?

Select the correct answer

question mark

Which keyword is used to end a CASE WHEN block?

Select the correct answer

question-icon

Fill in the blanks for a basic CASE WHEN statement that labels sales as Bulk if quantity >= 50, and Standard otherwise.

SELECT id, product, quantity, quantity >= 50 'Bulk' 'Standard' AS sale_type FROM sales;
id | product | quantity | sale_type
---+------------+----------+-----------
1 | Laptop | 5 | Standard
2 | Smartphone | 10 | Standard
3 | Tablet | 7 | Standard
4 | Headphones | 15 | Standard
5 | Monitor | 3 | Standard
6 | Keyboard | 12 | Standard
7 | Mouse | 20 | Standard
8 | Printer | 2 | Standard
9 | Laptop | 4 | Standard
10 | Smartphone | 8 | Standard
11 | Tablet | 6 | Standard
12 | Monitor | 5 | Standard
13 | Headphones | 10 | Standard
14 | Keyboard | 9 | Standard
15 | Mouse | 18 | Standard

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
some-alt