CASE

SQL CASE Keyword

The CASE keyword in SQL is used to create conditional logic in a query. It allows you to return different values based on specific conditions — similar to an IF-THEN-ELSE statement.

Tutorials dojo strip

SQL CASE Syntax

SQL

You can also use CASE inside an ORDER BY clause:

SQL

SQL CASE Example with Appointments Table

The following SQL statement shows the appointment ID, status, and a label that changes depending on the value of the status column. This query returns all appointment records and adds a new column status_label based on the value of status.

SQL

SQL CASE Example in ORDER BY Clause (Patients Table)

This example orders the patients by last_name, but if last_name is NULL, it will order by first_name instead. This ensures that even rows with missing last names are properly sorted using first names.

SQL

SQL CASE Labs

Tutorials dojo strip