Master SQL CASE WHEN 3 Conditions with Examples

SQL is a powerful query language used to manage and manipulate relational databases. One of its most useful features is the ability to make decisions within queries using the CASE statement. Today, we'll delve into a specific use of CASE: managing three conditions in a single statement.

SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial

Before diving into the three-condition CASE, let's quickly recap the basic concept. The SQL CASE statement executes different actions based on different conditions. It's like an 'if, else if, else' structure in programming, designed to simplify and clarify complex decision-making processes.

Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts

The Anatomy of a CASE Statement

A basic CASE statement has the following syntax:

SQL Cheatsheet In 2026 🚀
SQL Cheatsheet In 2026 🚀

```sql CASE expression WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE resultn END ```

The expression is tested against each WHEN condition. If a condition is true, the corresponding result is returned. If no conditions are met, the result of the ELSE clause is returned.

One Condition: The Simple CASE

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

The most straightforward use of CASE involves a single condition. Here's an example that assigns a grade based on a test score:

```sql SELECT score, CASE WHEN score >= 90 THEN 'A' ELSE 'B' END AS grade FROM students; ```

In this example, students scoring 90 or above get an 'A', while everyone else gets a 'B'.

Two Conditions: Nesting CASEs

SQL CTE Explained with Examples | Common Table Expression Tutorial
SQL CTE Explained with Examples | Common Table Expression Tutorial

If you need to evaluate two conditions, you can nest CASE statements. Let's refine the previous example to also distinguish between 'A' and 'A-':

```sql SELECT score, CASE WHEN score >= 90 THEN CASE WHEN score >= 97 THEN 'A' ELSE 'A-' END ELSE 'B' END AS grade FROM students; ```

In this case, if the score is 90 or above, the nested CASE statement decides whether to assign an 'A' or 'A-'. If not, a 'B' is given.

Combining Three Conditions: The CASE Triangle

SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

Adding a third condition to a CASE statement requires a different approach, as nesting two CASE statements can quickly become unwieldy. A better strategy is to explicitly list out the possible outcomes and their corresponding conditions.

Using UNION for Three Conditions

SQL CASE WHEN Explained Simply
SQL CASE WHEN Explained Simply
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Clauses Cheat Sheet 📊 | Easy Guide
SQL Clauses Cheat Sheet 📊 | Easy Guide
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
SQL Cheat Sheet for Beginners | SELECT, JOINS, GROUP BY, Queries & Commands Quick Revision
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners
SQL CAST() Explained with Examples | Convert Data Types in SQL
SQL CAST() Explained with Examples | Convert Data Types in SQL
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL queries don
SQL queries don
7 SQL Commands Every Beginner Must Know
7 SQL Commands Every Beginner Must Know
WHOLE SQL CHEAT SHEET
WHOLE SQL CHEAT SHEET
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
a poster with the words and numbers in it
a poster with the words and numbers in it
SQL Cheat Sheet for Beginners & Interviews
SQL Cheat Sheet for Beginners & Interviews
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL Commads
SQL Commads
SQL Joins Cheat Sheet Every Developer Should Save
SQL Joins Cheat Sheet Every Developer Should Save
CQF - SQL
CQF - SQL
SQL in 60 days
SQL in 60 days

One way to combine three conditions is by using a UNION clause to unite three separate CASE statements. For instance, let's define grades for scores 90-100 as 'A', 'A-', and 'B+' (for score 85):

First CASE:

```sql SELECT score, CASE WHEN score >= 97 THEN 'A' ELSE 'A-' END AS grade FROM students; ```

Second CASE:

```sql SELECT score, CASE WHEN score >= 90 AND score < 97 THEN 'A-' ELSE 'B+' END AS grade FROM students; ```

Third CASE:

```sql SELECT score, CASE WHEN score < 90 THEN 'B' END AS grade FROM students; ```

Combining these with UNION gives:

```sql SELECT score, grade FROM ( SELECT score, CASE WHEN score >= 97 THEN 'A' ELSE 'A-' END AS grade FROM students UNION ALL SELECT score, CASE WHEN score >= 90 AND score < 97 THEN 'A-' ELSE 'B+' END AS grade FROM students UNION ALL SELECT score, CASE WHEN score < 90 THEN 'B' END AS grade FROM students ) QUERY; ```

This approach works, but it's not the most elegant or maintainable solution. A cleaner way is to use the CASE expression in a SELECT statement with the WHEN and THEN clauses layered.

The Multi-Branched CASE Statement

For three conditions, you can use the following syntax:

```sql SELECT score, CASE WHEN score >= 97 THEN 'A' WHEN score >= 90 AND score < 97 THEN 'A-' ELSE 'B' END AS grade FROM students; ```

Here, if the score is 97 or above, the grade is 'A'. If it's between 90 and 97, it's 'A-', and if none of the above apply, the student gets a 'B'.

The elegance of this method lies in its simplicity. It's clearer and easier to maintain than using UNION, and better handles a range of values (in this case, 90-97) for a single grade. It's a great illustration of how SQL can simplify complex logic with a straightforward language construct.

The power of SQL lies in its versatility and robustness. The CASE statement is a prime example of this - through simple syntax, it enables complex decision-making in a clear and efficient manner. Whether you're handling one, two, or three conditions, the CASE statement is an invaluable tool in your SQL arsenal.

Related Articles

W3schools Javascript Href W3schools Sql Remove W3schools Sql Set W3s Sql Tutorial Trigger Sql W3s Can You Do Multiple Inner Joins In Sql Trigger Use In Sql Multiple Sql Joins In One Query W3 Sql Set Pl Sql W3s