Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL CASE Expressions: If-else in Select Query

PostgreSQL supports CASE expression which is the same as if/else statements of other programming languages.

The CASE expression can be used with SELECT, WHERE, GROUP BY, and HAVING clauses.

Syntax
CASE
	WHEN <condition1> THEN <result1>
	WHEN <condition2> THEN <result2>
	….
	[ELSE <else_result>]
END

In the above syntax, every condition is a boolean expression that evaluates to be either true or false. The CASE expression evaluates a list of conditions in sequence.

  • If the condition evaluates to true, the CASE expression will return the corresponding result set for that condition and stop evaluating the next expression.
  • If the condition evaluates to false, the CASE expression keeps on evaluating the next condition until it finds the expression to be evaluated as true.
  • If of all the conditions evaluates to be false, then it returns else_result that is in the ELSE clause. The ELSE clause is optional. If the ELSE clause is not defined for CASE expression, then it will return NULL.

The data types of all the result expressions must be convertible to a single output type, otherwise CASE expression will raise error.

Let's use the following Employee table to understand the CASE expression.

Sample Table

Simple CASE Expression

The following query is using simple CASE expression, where it checks the value of the column and returns the resultset as per value.

Example: Simple CASE Expression
SELECT emp_id, first_name, last_name,
	CASE gender 
		WHEN 'M' THEN 'Male'
		WHEN 'F' THEN 'Female'
	END gender
FROM Employee;

Note that in the above CASE expression, the ELSE case is not specified, so for emp_id = 4, it shows gender as null.

General CASE Expression with ELSE

Let's use the CASE expression to do a salary analysis of employees where salary will be categorized as Low, Average, Very Good, and No Data, based on the following range:

  • 'Low' if salary is < 50000
  • 'Average' if salary > 50000 AND salary <= 100000
  • 'High' if salary > 100000
  • 'No Data' if salary is null
Example: CASE Expression
SELECT emp_id, first_name, last_name, salary,
	CASE WHEN salary <= 50000 THEN 'Low'
		WHEN salary > 50000 AND salary <= 100000  THEN 'Average'
		WHEN salary > 100000  THEN 'High'
		ELSE 'No Data'
	END salary_analysis
FROM Employee;

In the above query, the CASE expression is evaluated on the Salary column of the Employee table. Note that we use column alias salary_analysis as CASE expression column.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.