# Expressions & Functions

> Calculate new values in a query with arithmetic, text functions and COALESCE, and learn how NULL spreads.

Subject: SQL | Lesson 11 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/expressions-and-functions
Previous: [DISTINCT](https://codewithanimation.in/sql/distinct-values.md) | Next: [CASE Expressions](https://codewithanimation.in/sql/case-expressions.md)

## Compute values inside the query

An expression combines values with operators and functions to make a new value. You can use one in the select list, in `WHERE`, or in `ORDER BY`.

Arithmetic uses `+`, `-`, `*`, `/` and `%` (the remainder). Text uses `||` to join pieces, and functions such as `upper()`, `lower()` and `length()`.

`coalesce(a, b, c)` returns the first argument that is not `NULL`. It is the standard way to give a default.

## Let the database do the work

Computing in the query means every program that reads the data gets the same answer, and you send one finished value over the network instead of several raw ones.

## Three small examples

```sql
SELECT name,
       points / 2 AS half,
       name || ' (' || team || ')' AS label,
       coalesce(points, 0) AS points_or_zero
FROM players;
```

Asha has 31 points, so `half` is 15, not 15.5. When both numbers are whole, `/` gives a whole number, cut toward zero. If you want decimals, one side must be a decimal number: `5.0 / 2` gives `2.5000000000000000`.

`length()` counts characters, so `length('Asha')` is 4. `upper('Asha')` is `ASHA`.

## Common mistake: NULL spreads

If any value in a calculation is `NULL`, the whole result is `NULL`. `points + 10` is NULL for Chen. The same is true of `||`: `name || ' scored ' || points` is NULL for Chen, so the whole label disappears.

`concat()` is different: it ignores `NULL` and joins the rest. `coalesce(points, 0)` replaces a NULL with 0 before you calculate.

## Tip: Use COALESCE for display, not to hide missing data

`coalesce(points, 0)` is great for a report. But 0 points and unknown points are different facts, so do not store the 0 back unless you mean it.

## Try it: Compute a new column

Pick an expression and read the `result` column for every player. Compare `||` with `concat()` on the players who have no points.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/expressions-and-functions

## Check yourself

### Question 1

In PostgreSQL, what is `5 / 2` when both numbers are whole numbers?

- A. 2
- B. 2.5
- C. 3

Correct answer: A

Integer division drops the fraction, so 5 / 2 is 2.

- Why not B: Whole numbers divide to a whole number. Use 5.0 / 2 for 2.5000000000000000.
- Why not C: The result is cut toward zero, not rounded up.

### Question 2

Chen's points are NULL. What is `name || ' scored ' || points` for Chen?

- A. NULL
- B. Chen scored
- C. Chen scored 0

Correct answer: A

A NULL anywhere in a || chain makes the whole result NULL.

- Why not B: That is what concat() would give, because concat() ignores NULL.
- Why not C: SQL does not turn NULL into 0 by itself. coalesce(points, 0) would.

### Question 3

What does `coalesce(points, 0)` return for a player whose points are NULL?

- A. 0
- B. NULL
- C. An error, because the types differ

Correct answer: A

coalesce returns its first argument that is not NULL, here the 0.

- Why not B: coalesce skips NULL arguments and returns the first one that has a value.
- Why not C: Both arguments are numbers, so it works.

## Remember this

- Expressions combine values with operators and functions, and are worked out once per row.
- Two whole numbers divide to a whole number: 5 / 2 is 2.
- A NULL in arithmetic or in || makes the whole result NULL.
- concat() ignores NULL, and coalesce() returns its first argument that is not NULL.
