Control Structures syntax: if then else case

PostreSQL tutorial – Control Structures

PostgreSQL Conditionals

Control Structures syntax: if then else, case when, return, exit, loop.

Syntax

IF expression 
THEN statement 
END IF;

IF expression 
THEN statement_1 
ELSE statement_2 
END IF; 

IF expression_1 
THEN statement_1 
ELSIF expression_2 
THEN statement_2 
ELSE statement_3 
END IF;

CASE expression_1 
WHEN expression_2 
THEN statement_1 
ELSE statement_2 
END CASE; 

CASE WHEN expression 
THEN statement_1 
ELSE statement_2 
END CASE;

PostgreSQL Loop

Syntax

LOOP statements END LOOP;

PostgreSQL For Loop

Syntax

FOR i IN query 
LOOP 
statements 
END LOOP;

Examples

IF

IF Else

IF Elsif Else

CASE

LOOP