Jed Rembold
Wednesday, September 10, 2025
SELECT statements on values
directly if needed, omitting a reference to a table!
FROM portion of the
statementSELECT CAST('Jan 25, 2022' AS DATE);
SELECT 'aardvark' > 'Zebra';| Operator | Description | Example |
|---|---|---|
+ |
Addition | 4 + 5 |
- |
Subtraction | 10 - 5 |
* |
Multiplication | 2 * 4 |
/ |
Division | 20 / 5 |
| Operator | Description | Example |
|---|---|---|
% |
Modulo (remainder) | 7 % 5 |
^ |
Exponentiation | 3 ^ 2 |
|/ |
Square root | |/9 |
||/ |
Cube root | ||/27 |
! |
Factorial | 5! |
@ |
Absolute value | @ -42 |
pg_typeof() function can be useful at
times if you need to check a data typeSQL follows your standard order of operations
If you want some other ordering, you need to use parentheses to group things accordingly, as operations in parentheses happen first
SELECT 4 ^ (9 % 2 + 1);What would be the output of the below selection?
SELECT (1 + 2) ^ (9 % (10 - 4) / 2) * 2::REAL;
| Function | Description |
|---|---|
sqrt(num) |
Square root |
abs(num) |
Absolute value |
factorial(num) |
Factorial of a number |
sin(num) |
Sine of radians |
sind(num) |
Sine of degrees |
asin(num) |
Inverse sine in radians |
degrees(num) |
Convert radians to degrees |
log(num) |
Base 10 logarithm |
ln(num) |
Base e logarithm |
round(num,scale) |
Round number to scale decimal places |
You may have noticed that the default column names when selecting a calculation can be nonsensical
SQL provides a way for you to name a column on the fly, using the
AS keyword
Technically forms what is commonly called an alias
Syntax:
SELECT (|||calculation|||) AS |||alias name|||;You can then refer to that column name later in your command if you want or need
ORDER BY or (later)
GROUP BYWHERE or (later)
HAVINGSELECT |||column 1|||, |||column 2|||, |||column 1 - column 2||| FROM |||tablename|||;


NULL values
are ignored in the calculations| Function | Description |
|---|---|
avg(col) |
Finds the average or mean of a column |
sum(col) |
Computes the sum of a column |
count(*) |
Computes the number of input rows in the table |
count(col) |
Computes the number of non-null input rows in that column |
max(col) |
Finds the maximum value in a column |
min(col) |
Finds the minimum value in a column |
WHERE directly off of aggregated values| Function | Output Type |
|---|---|
avg(col) |
int or numeric → numeric |
| float of any type → double precision | |
sum(col) |
smallint or int → bigint |
| bigint or numeric → numeric | |
| float → corresponding float | |
count(*) |
bigint |
count(col) |
bigint |
max(col) |
same as input |
min(col) |
same as input |
The median is commonly desired as it is a measure of the distribution center less affected by outliers
Postgres (and most other SQL variants) do not
have a median() function
Instead they offer more general percentile functions
Ordering matters here, so the syntax needs to be a bit different:
SELECT
|||percentile_func|||(|||percent|||) WITHIN GROUP (ORDER BY |||column|||)
FROM |||tablename|||;WITHIN GROUP functions entirely
ignore NULL valuespercentile_cont is best used for
continuous values
percentile_disc is best used for discrete
values
SELECT
percentile_cont(0.5) WITHIN GROUP (ORDER BY |||column|||)
FROM |||tablename|||;mode()SELECT
mode() WITHIN GROUP (ORDER BY |||column|||)
FROM |||tablename|||;| Function | Output Type |
|---|---|
percentile_cont(f) |
double precision |
percentile_disc(f) |
Same as ordered column |
mode() |
Same as ordered column |