NumbersSQL standard

FLOOR

Rounds a number down — to the nearest integer that isn't larger than it.

Syntax

FLOOR(<number>)

Drops the fractional part towards the smaller number: 4.9 becomes 4. An integer stays as it is. Handy for sorting values into ranges: amounts by hundreds, ages by decades.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

Down to an integer
SELECT FLOOR(4.9) AS down,
FLOOR(4.0) AS whole,
FLOOR(-4.1) AS negative;
downwholenegative
44-5
Each order in its own amount range
SELECT id, amount,
FLOOR(amount / 100) * 100 AS price_range
FROM orders;
idamountprice_range
1120.50100
280.250
3199.99100
445.000
564.750
How many orders in each range
SELECT FLOOR(amount / 100) * 100 AS price_range,
COUNT(*) AS order_count
FROM orders
GROUP BY price_range
ORDER BY price_range;
price_rangeorder_count
03
1002

0 means amounts from 0 to 99.99, 100 — from 100 to 199.99.

Watch out

For negative numbers, “down” means further from zero: FLOOR(-4.1) gives -5. Simply dropping the fractional part is TRUNC: TRUNC(-4.1) gives -4.

Integer division drops the fractional part towards zero, not down: -7 / 2 gives -3, while FLOOR(-7 / 2.0) gives -4.

Related functions

Learn more in the course