SQL CASE – A Detailed Overview and Understanding SQL CASE Expression

Deep dive into the SQL CASE expression. You will explore its syntax, uses, and provide examples to illustrate its applications.

SQL, or Structured Query Language, is the standard language for dealing with Relational Databases. SQL can be used to insert, search, update, and delete database records. But beyond these basic operations, SQL also offers some powerful features, one of which is the CASE expression.

What is a SQL CASE Expression?

The CASE expression in SQL is a conditional expression, similar to “if…else” statements found in other programming languages. It allows us to perform conditional logic in our SQL queries, where you can change the data that is returned based on specific conditions. It’s a way to make your SQL statements more powerful and flexible.

SQL CASE Expression Syntax

The SQL CASE expression has two formats: the simple CASE expression and the searched CASE expression.

Simple CASE Expression

Here’s the syntax for a simple CASE expression:

CASE input_expression 
    WHEN expression [THEN result_expression] [...n] 
    [ 
      ELSE else_result_expression 
    ] 
END

In this form, the input_expression is compared to the when_expression to check for equivalency. If they are equal, the corresponding result_expression is returned. If no matches are found and an ELSE clause is provided, the else_result_expression is returned. If an ELSE clause isn’t provided and no match is found, a NULL value is returned.

Searched CASE Expression

And here’s the syntax for a searched CASE expression:

CASE
    WHEN boolean_expression THEN result_expression [...n] 
    [ 
      ELSE else_result_expression 
    ] 
END

In this form, SQL evaluates each boolean_expression in order. When a boolean_expression evaluates to true, SQL returns the corresponding result_expression. If no boolean_expression evaluates to true and an ELSE clause is provided, the else_result_expression is returned. If an ELSE clause isn’t provided and no boolean_expression is true, a NULL value is returned.

SQL CASE Expression: An Example

To better understand how the SQL CASE expression works, let’s consider an example. Assume you have a students table that stores information about students, and it looks something like this:

| StudentID | FirstName | LastName | Grade |
|-----------|-----------|----------|-------|
| 1         | John      | Doe      | 85    |
| 2         | Jane      | Smith    | 90    |
| 3         | Mike      | Johnson  | 76    |
| 4         | Lisa      | Brown    | 88    |

Suppose you want to categorize students based on their grades: ‘Excellent’ for grades above 90, ‘Good’ for grades between 80 and 90, and ‘Fair’ for grades below 80. You can use a CASE expression to accomplish this.

Here’s how you might write that query:

SELECT 
  FirstName, 
  LastName, 
  Grade,
  CASE 
    WHEN Grade >= 90 THEN 'Excellent'
    WHEN Grade >= 80 AND Grade < 90 THEN 'Good'
    ELSE 'Fair'
  END AS GradeStatus
FROM 
  students;

In the query above, the CASE expression checks each student’s grade and assigns a grade status of ‘Excellent’, ‘Good’, or ‘Fair’ based on the value of the grade. This newly created data is then presented in a column labeled GradeStatus.

Running this query against our sample data, the result would look something like this

| FirstName | LastName | Grade | GradeStatus |
|-----------|----------|-------|-------------|
| John      | Doe      | 85    | Good        |
| Jane      | Smith    | 90    | Excellent   |
| Mike      | Johnson  | 76    | Fair        |
| Lisa      | Brown    | 88    | Good        |

Conclusion

The SQL CASE expression is an incredibly versatile tool that allows for complex, conditional logic to be written directly within your SQL queries. By understanding and leveraging the CASE expression, you can create more powerful, dynamic, and flexible queries to extract valuable insights from your data.

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science