What is SQL CASE Statement ?

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".

Raw Data

Now I'd like to create a new column that classifies the data according to the 'ReorderPoint' column. Here are the rules:

  1. If the 'ReorderPoint' is 300 or less, it will be labeled as "bad".
  2. If the 'ReorderPoint' is 700 or more, it will be labeled as "good".

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


Categorised Data


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.


To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics