CASE will execute SQL code when certain conditions are met (similar to an IF/ELSE statement).
GENERAL SYNTAX - TYPE 1:
CASE
WHEN condition 1 THEN result1
WHEN condition 2 THEN result2
ELSE some other result
END;
GENERAL SYNTAX - TYPE 2:
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
ELSE some other result
END;
With Type 1, you can specify multiple different conditions. With Type 2, you can only specify one condition.
COALESCE evaluates a number of arguments and will return the first result that is not NULL. It is useful for substituting NULL values with another value.
Example:
| Item | Price | Discount |
|---|---|---|
| A | 100 | 20 |
| B | 300 | NULL |
| C | 200 | 10 |
| Item | final |
|---|---|
| A | 80 |
| B | NULL |
| C | 190 |
| Item | final |
|---|---|
| A | 80 |
| B | 300 |
| C | 190 |
CAST lets you convert from one data type into another.
NULLIF take two inputs.
You can create a VIEW to quickly see the results of a commonly used query.
https://www.postgresql.org/docs/13/sql-copy.html