
The CASE statement in PostgreSQL allows you to evaluate conditions and return values based on those conditions. It’s useful when you want to add conditional logic to queries. For example, grading student scores or classifying records based on thresholds.
This article demonstrates how to use the PostgreSQL CASE statement to generate categorized results from database tables. You’ll create a sample schema with student records and marks, then use a CASE statement to assign grades based on score ranges.
Before you begin:
sudo privileges.psql client is installed on your server.CASE Statement in PostgreSQLThe CASE statement in PostgreSQL is a conditional expression used to add logic within a SQL query. It evaluates conditions sequentially and returns a result based on the first condition that evaluates to true. This is especially useful when transforming or classifying data during query execution.
A typical CASE block contains the following components:
CASE: Begins the conditional logic block.WHEN: Defines a condition to evaluate.THEN: Specifies the value to return if the corresponding WHEN condition is true.END: Closes the CASE expression.Here is a basic example of the CASE syntax within a SELECT query:
You can use any standard SQL comparison operators in your conditions, including:
=: Equal to<: Less than<=: Less than or equal to>: Greater than>=: Greater than or equal toTo demonstrate the CASE statement in action, start by creating a sample database with two tables: students and marks.
Log in to your PostgreSQL server using the psql client. Replace the hostname, port, and username with your actual credentials.
Create a new database named school.
Connect to the newly created school database.
Create a students table with columns for the student ID, first name, and last name.
Insert sample student records into the students table.
View the students table data to verify the new values.
Output.
Create a marks table to store student scores.
Insert sample scores into the marks table.
Retrieve data from the marks table.
Output.
CASE Statement to Grade StudentsYou can use the CASE expression to evaluate each student's score and assign a grade dynamically. By joining the students and marks tables, you can generate a report that includes names, scores, and computed grade categories.
Run the following SQL query:
This query performs a LEFT JOIN to match each score with the corresponding student record. The CASE block evaluates the score column and returns:
Distinction for scores 75 and aboveCredit for scores between 70 and 74Pass for scores between 40 and 69Fail for scores below 40Output.
This example demonstrates how CASE enables clear, conditional logic directly within a SELECT query, making your SQL both powerful and easy to maintain.
In this article, you created a sample PostgreSQL database and used the CASE statement to classify student scores with readable and maintainable SQL logic. This conditional technique simplifies query structure, especially for grading or rule-based categorization. For more advanced logic, you can explore PostgreSQL features like lookup tables, IF statements, or AI-powered solutions such as semantic search with pgvector. To learn more, visit the official PostgreSQL conditional expressions documentation.
0 Comments
Be the first to comment and share your perspective with the community.