What is SQL CASE Statement ?
Think of it as a smart tool that helps sort your data with custom rules – like how you might pick an outfit based on the weather. It's a straightforward way to apply 'if this, then that' logic to your data, making your SQL queries sharper and your results more tailored. Ready to learn how to make your data work smarter? Let's dive in.
Lets take an example from this data table we have "ProductID", "Name" and "ReorderPoint".
Now I'd like to create a new column that classifies the data according to the 'ReorderPoint' column. Here are the rules:
Using these rules, I've written an SQL query:
SELECT
ProductID, Name,
ReorderPoint,
case when ReorderPoint <= 300 then 'bad'
when ReorderPoint >= 700 then 'good'
else 'very bad'
end as resposne_customer
FROM adventure_works.Production_Product
Which categorise my data on basis of conditions provided
Note: I am inspired to create this series of articles to provide content that is easily comprehensible for those preparing for interviews. The primary reason for my writing is that it establishes a disciplined routine for me to write and learn simultaneously.