Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте CASE WHEN in SELECT Clauses | CASE WHEN Basics and Categorization
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

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4
some-alt