PostgreSQL Conditional Expressions

I want to read more about postgreSQL conditional expressions so that I can write better SQL queries faster.

Date Created:
2 459

References



Notes


CASE

The SQL CASE expression is a generic conditional expression, similar to if/else statements in other programming languages:

CASE WHEN condition THEN result [WHEN ...] [ELSE result] END

CASE clauses can be used wherever an expression is valid. Each condition is an expression that returns a boolean result. If the condition's result is true, the value of the CASE expression is the result that follows the condition, and the remainder of the CASE expression is not processed. If the ELSE clause is omitted and no condition is true, the result is null. The data types of all the result expressions must be convertible to a single output type. There is a simple form of CASE expression that is a variant of the general form above:

CASE expression WHEN value THEN result [WHEN ...] [ELSE result] END

The first expression is computed, then compared to each of the value expressions in the WHEN clauses until one is found that is equal to it. If not match is found, the result of the ELSE clause is returned. A CASE expression does not evaluate any subexpressions that are not needed to determine the result. For example, this is a possible way of avoiding a division-by-zero failure:

SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;

COALESCE

COALESCE(value [, ...])

The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute default values when data is retrieved for display. The arguments must all be convertible to a common data type, which will be the type of the result. COALESCE only evaluates the arguments that are needed to determine the result; that is, arguments to the right of the first non-null argument are not evaluated.

NULLIF

NULLIF(value1,value2)
NULLIF(value,' (none) ') -- IF value is ' (none) ', NULL is returned

The NULLIF function returns a null value if value1 equals value2; otherwise it returns value. This can be used to perform the inverse operation of COALESCE. The two arguments must be comparable types.

GREATEST and LEAST

GREATEST(value [,...])
LEAST(value [,...])

The GREATEST and LEAST functions select the largest r smallest value from a list of any number of expressions. The expressions must all be convertible to a common data type, which will be the type of the result.

NULL values in the list are ignored. The result will be NULL only if all the expressions evaluate to NULL.

Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC