Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara CASE WHEN in SELECT Clauses | CASE WHEN Basics and Categorization
Practice
Projects
Quizzes & Challenges
Quiz
Challenges
/
Mastering CASE WHEN in SQL

bookCASE WHEN in SELECT Clauses

Using CASE WHEN in the SELECT clause allows you to create dynamic, computed columns in your query results. This technique is powerful because it enables you to transform raw data into meaningful categories or labels directly as you retrieve it, without altering the underlying tables. For instance, you can instantly classify products as "Expensive" or "Affordable" based on their price, making your reports clearer and more actionable.

12345678
SELECT product, price, CASE WHEN price >= 500 THEN 'Expensive' ELSE 'Affordable' END AS price_category FROM sales;
copy

In this query, the CASE WHEN expression is placed inside the SELECT clause. For each row in the sales table, SQL checks the value of the price column. If the price is greater than or equal to 500, the computed column price_category will show "Expensive"; otherwise, it will display "Affordable". This computed column is generated on the fly, allowing you to instantly see the categorization alongside the original data.

12345678910
SELECT product, price, CASE WHEN price >= 1000 THEN 'Luxury' WHEN price >= 500 THEN 'Expensive' WHEN price >= 100 THEN 'Moderate' ELSE 'Budget' END AS price_tier FROM sales;
copy

1. In which clause is CASE WHEN most commonly used to create new columns?

2. Fill in the blanks to create a CASE WHEN that labels products as "Premium" if price is greater than or equal to 100.

question mark

In which clause is CASE WHEN most commonly used to create new columns?

Select the correct answer

question-icon

Fill in the blanks to create a CASE WHEN that labels products as "Premium" if price is greater than or equal to 100.

100 THEN 'Premium' ELSE 'Standard' END AS product_label FROM sales;
Premium

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookCASE WHEN in SELECT Clauses

Scorri per mostrare il menu

Using CASE WHEN in the SELECT clause allows you to create dynamic, computed columns in your query results. This technique is powerful because it enables you to transform raw data into meaningful categories or labels directly as you retrieve it, without altering the underlying tables. For instance, you can instantly classify products as "Expensive" or "Affordable" based on their price, making your reports clearer and more actionable.

12345678
SELECT product, price, CASE WHEN price >= 500 THEN 'Expensive' ELSE 'Affordable' END AS price_category FROM sales;
copy

In this query, the CASE WHEN expression is placed inside the SELECT clause. For each row in the sales table, SQL checks the value of the price column. If the price is greater than or equal to 500, the computed column price_category will show "Expensive"; otherwise, it will display "Affordable". This computed column is generated on the fly, allowing you to instantly see the categorization alongside the original data.

12345678910
SELECT product, price, CASE WHEN price >= 1000 THEN 'Luxury' WHEN price >= 500 THEN 'Expensive' WHEN price >= 100 THEN 'Moderate' ELSE 'Budget' END AS price_tier FROM sales;
copy

1. In which clause is CASE WHEN most commonly used to create new columns?

2. Fill in the blanks to create a CASE WHEN that labels products as "Premium" if price is greater than or equal to 100.

question mark

In which clause is CASE WHEN most commonly used to create new columns?

Select the correct answer

question-icon

Fill in the blanks to create a CASE WHEN that labels products as "Premium" if price is greater than or equal to 100.

100 THEN 'Premium' ELSE 'Standard' END AS product_label FROM sales;
Premium

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
some-alt