Introduction
Hi Ninjas!! Welcome to another blog on PostgreSQL. Today we will learn about Conditionals and Control Flow in PostgreSQL. PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS). It is used to store data securely.

Conditionals
The conditional statements are used to make decisions based on specific conditions in a query.
Let's look at the conditionals in PostgreSQL.

Let's look into details of each of them.
CASE
CASE is used to form conditional queries. It is similar to the if-else statement.
Syntax:
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
[WHEN ...]
[ELSE result_n]
END
Example:
Consider the table:

Select SUM( CASE When Department="CSE" THEN 1 ELSE 0 END) as CSE from student;
The query above is to find the sum of students from CSE department.
Output:

COALESCE
The COALESCE function is used to handle the Null Values in a table. This function allows assigning specific values to Null Values during Database query. COALESCE conditional function can accepts number of arguments at a time ,If all the arguments are NULL in that case it returns the fist argument as not null.
Syntax:
COALESCE (argument_1, argument_2, …);
Example:
Consider the Following table:

Select Name, COALESCE(Marks, 0) as Marks from student;
In this example row 11 contains a null value. Using COALESCE on column marks the null value is replaced with 0.
Output:

Example:
Consider table Placement_cell :
Here St_id 1011 contains a NULL value in the company column.

select company,coalesce(company,'NoINFO') from Placement_cell;
This query replaces the NULL value in company column to ‘NoINFO’.
OUTPUT:

NULLIF()
The NULLIF() is a type of conditional in PostgreSQL. It compares two arguments from a particular table record and returns NULL only when the values of the arguments are equal; else, it returns the value of the first argument.
Syntax:
NULLIF(argument1,argument2);
Example:
Consider the table:

Select Name, NULLIF(Marks, Roll) from student;
Output:

EXAMPLE:
Let us consider a table Placement_cell with St_id ,name, status,Incampus, and company as columns. NULLIF conditional is applied to check the status and Incampus placement details of students.

NULLIF(status,Incampus);
OUTPUT:

NULLIF(argument1,argument2)
NULLIF compares the values of both the arguments ,and returns NULL if the values of the arguments are equal else returns the value of argument1.
CAST
The CAST operator is used to modify the data type of the values.
Syntax:
CAST ( expression AS DataType );
Example:
SELECT CAST(525.25 AS int);
Output:
525