PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL CASE

PostgreSQL CASE

In this tutorial, we will show you how to use the PostgreSQL CASE conditional expression to form conditional query.

General PostgreSQL CASE expression

The PostgreSQL CASE expression is the same as IF/ELSE statement in other programing languages. PostgreSQL provides two forms of the CASE expression. The following illustrates the general form of the CASE statement:

1
2
3
4
5
6
CASE
      WHEN condition_1  THEN result_1
     WHEN condition_2  THEN result_2
     [WHEN ...]
     [ELSE result_n]
END

In this general form, each condition is an expression that returns a boolean value, either true or false. If the condition evaluates to true, it returns the result which follows the condition, and all other CASE branches do not process at all. If all conditions evaluate to false, the CASE expression will return the result in the ELSE part. If you omit the ELSE clause, the CASE expression will return null.

Notice that all result expressions must have data types that can be convertible to a single data type e.g., string, numeric, etc.

Let’s take a look at the film table. Suppose you want to assign a price segment to a film as:

  • mass if the rental rate is 0.99
  • economic if the rental rate is 1.99
  • luxury if the rental rate is 4.99

And you want to know the number of films that belongs mass, economic or luxury price segment. In this case, you can use the CASE expression to construct the query as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SELECT
SUM (
CASE
WHEN rental_rate = 0.99 THEN
1
ELSE
0
END
) AS "Mass",
SUM (
CASE
WHEN rental_rate = 2.99 THEN
1
ELSE
0
END
) AS "Economic",
SUM (
CASE
WHEN rental_rate = 4.99 THEN
1
ELSE
0
END
) AS "Luxury"
FROM
film;

The result of the query is as follows:

postgresql case example

We used CASE expression to return 1 or 0 if the rental rate meets the requirement. And then we applied SUM function to calculate the total of films for each price segment.

Simple PostgreSQL CASE expression

PostgreSQL provides another form of the CASE expression called simple form as follows:

1
2
3
4
5
6
7
8
9
CASE expression
WHEN value_1 THEN
result_1
WHEN value_2 THEN
result_2
[WHEN ...]
ELSE
result_n
END;

PostgreSQL first evaluates the expression and then compare it with each value in the WHEN clause until it finds the value that matches the returned value of the expression. If PostgreSQL does not find any matches, it returns the result_n in the ELSE part, or NULL value if the ELSE part is omitted. This is similar to the switch statement in other programming languages such as C/C++, Java, etc.

We can rewrite the general CASE expression using the simple one as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SELECT
SUM (
CASE rental_rate
WHEN 0.99 THEN
1
ELSE
0
END
) AS "Mass",
SUM (
CASE rental_rate
WHEN 2.99 THEN
1
ELSE
0
END
) AS "Economic",
SUM (
CASE rental_rate
WHEN 4.99 THEN
1
ELSE
0
END
) AS "Luxury"
FROM
film;

It produces the same result.

postgresql case example

In this tutorial, you have learned how to use both forms of PostgreSQL CASE expression to form complex query.

Related Tutorials

  • PostgreSQL NULLIF
Previous Tutorial: PostgreSQL SUM Function
Next Tutorial: PostgreSQL COALESCE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.