CASE 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.
12345678SELECT product, price, CASE WHEN price >= 500 THEN 'Expensive' ELSE 'Affordable' END AS price_category FROM sales;
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.
12345678910SELECT 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;
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.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 4.17
CASE WHEN in SELECT Clauses
Veeg om het menu te tonen
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.
12345678SELECT product, price, CASE WHEN price >= 500 THEN 'Expensive' ELSE 'Affordable' END AS price_category FROM sales;
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.
12345678910SELECT 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;
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.
Bedankt voor je feedback!