---
title: "Explain the purpose of the CASE statement in SQL and provide an example."  
description: "Explain the purpose of the CASE statement in SQL and provide an example."  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-07-01  
canonical: https://www.mindstick.com/forum/158910/explain-the-purpose-of-the-case-statement-in-sql-and-provide-an-example  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Explain the purpose of the CASE statement in SQL and provide an example.

[Explain the purpose](https://www.mindstick.com/forum/159480/explain-the-purpose-of-the-virtual-keyword-and-virtual-functions-in-c-plus-plus) of the CASE [statement in SQL](https://www.mindstick.com/forum/33706/what-is-the-difference-between-having-clause-and-group-by-statement-in-sql-server) and provide an example.

## Replies

### Reply by Aryan Kumar

Sure. The [CASE statement](https://www.mindstick.com/blog/290/case-statement-in-sql-server) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) is used to evaluate a condition and return a value based on the outcome of the evaluation. The CASE statement can be used to perform simple if-then-else logic, or it can be used to perform more complex conditional logic.

The syntax for the CASE statement is:

SQL

```plaintext
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
```

- `condition1`, `condition2`, `conditionN` are the conditions that you want to evaluate.
- `result1`, `result2`, `resultN` are the values that you want to return if the corresponding condition is true.
- `result` is the value that you want to return if none of the conditions are true.

For example, the following query will return the grade of a student based on their score:

SQL

```plaintext
SELECT
  CASE
    WHEN score >= 90 THEN 'A'
    WHEN score >= 80 THEN 'B'
    WHEN score >= 70 THEN 'C'
    WHEN score >= 60 THEN 'D'
    ELSE 'F'
  END AS grade
FROM students;
```

The CASE statement will first evaluate the `score` column. If the score is greater than or equal to 90, then the CASE statement will return the value `A`. If the score is greater than or equal to 80, but less than 90, then the CASE statement will return the value `B`. And so on.

If the score is less than 60, then none of the conditions will be true. In this case, the CASE statement will return the value `F`.

The CASE statement is a powerful tool that can be used to perform conditional logic in SQL. It can be used to simplify your queries and make them more readable.

Here are some other examples of how the CASE statement can be used:

- To return a different value based on the day of the week.
- To return a different value based on the value of a column.
- To return a different value based on the user input.

The CASE statement is a versatile tool that can be used in a variety of ways. If you are working with conditional logic in your SQL queries, then the CASE statement is a tool that you should definitely know about.


---

Original Source: https://www.mindstick.com/forum/158910/explain-the-purpose-of-the-case-statement-in-sql-and-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
