🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to PostgreSQL Notes
Topic #42

PostgreSQL CASE


CASE

The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement).

Once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

Example

SELECT product_name,
CASE

WHEN price < 10 THEN 'Low price product'
  WHEN price > 50 THEN 'High
price product'
ELSE
  'Normal product'
END
FROM
products;

With an Alias

When a column name is not specified for the "case" field, the parser uses case as the column name.

To specify a column name, add an alias after the END keyword.

Example

SELECT product_name,
CASE

WHEN price < 10 THEN 'Low price product'
  WHEN price > 50 THEN 'High
price product'
ELSE
  'Normal product'
END AS "price category"
FROM
products;

You can read more about aliases in our PostgreSQL AS chapter.

Want to go beyond the notes?

Join CodingNow 2.0's PostgreSQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

PostgreSQL CASE – FAQs

Quick answers about learning PostgreSQL CASE in PostgreSQL.

This free note from CodingNow 2.0 explains PostgreSQL CASE in PostgreSQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every PostgreSQL topic on CodingNow 2.0, including PostgreSQL CASE, is 100% free with no signup required.
With focused practice, most students grasp PostgreSQL CASE in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now