Q1Which of the following is the correct result of SELECT ROUND(3.84);?
Functions ③ — Math Functions (ROUND / FLOOR / CEILING) and COALESCE
The third of three SQL function articles. Cover the math functions ROUND / FLOOR / CEILING / POWER and COALESCE — which returns the first non-NULL value — using CSV-loaded staff and test-score datasets.
Data used in this article — staff and test_score
The last function article covers math functions and COALESCE. With the math functions, we'll handle rounding, flooring, ceiling, and exponentiation on numeric values like the salary column. With COALESCE, we'll cover replacing NULL with another value.
This article uses two CSVs. The first half (math functions) uses the same staff table as before (10 employees). The second half (COALESCE) brings in a new test_score table (8 students × 3 test attempts, with NULLs). In test_score, the score_1 / score_2 / score_3 columns have NULLs where a student was absent and didn't take that test — perfect for a realistic scenario like "use the score from the first test the student was able to take" to learn how COALESCE works.
Math functions — ROUND / FLOOR / CEILING / POWER
Here are the 4 main functions for shaping numeric values:
- ROUND(x, n): rounds to n decimal places. Omit n and it rounds to an integer
- FLOOR(x): floor (rounds toward negative infinity)
- CEILING(x) or CEIL(x): ceiling (rounds toward positive infinity)
- POWER(x, y): exponentiation (x to the power of y)
-- 1) ROUND: rounding
SELECT ROUND(3.14); -- 3 (integer)
SELECT ROUND(3.14, 1); -- 3.1 (1 decimal place)
-- 2) FLOOR: floor
SELECT FLOOR(3.84); -- 3
-- Note: CEILING runs on MySQL / PostgreSQL / Oracle / SQL Server — SELECT CEILING(3.14); → 4
-- 3) POWER: exponentiation
SELECT POWER(3, 4); -- 81 (3 to the 4th)
SELECT POWER(2, 10); -- 1024
-- 4) On a column — round monthly salary
SELECT name, salary, ROUND(salary / 12.0) AS monthly
FROM staff;
| Input | ROUND (rounding) | FLOOR (floor) | CEILING (ceiling / not available in this course's console) |
|---|---|---|---|
| 3.14 | 3 | 3 | 4 |
| 3.84 | 4 | 3 | 4 |
| 3.50 | 4 | 3 | 4 |
| -3.14 | -3 | -4 (toward negative) | -3 |
COALESCE — return the first non-NULL value
`COALESCE(value1, value2, value3, ...)` evaluates its arguments left to right and returns the first one that isn't NULL. If they're all NULL, it returns NULL. Use it whenever you want a NULL fallback: "use the main column if it has a value, otherwise the backup column, otherwise a default."
This article uses the test_score table. For each student_id, score_1 / score_2 / score_3 hold the 1st, 2nd, and 3rd test scores, with NULL where the student was absent. Passing (score_1, score_2, score_3, 0) to COALESCE gives you the score from the first test the student was able to take, treated as the "final score." Adding 0 at the end means "if they missed all three, treat it as 0."
-- 1) Check with literal values
SELECT COALESCE(NULL, NULL, 'C'); -- 'C'
SELECT COALESCE(NULL, NULL, NULL); -- NULL
-- 2) Pull the final score from the test_score table
SELECT student_id, name, score_1, score_2, score_3,
COALESCE(score_1, score_2, score_3) AS first_score
FROM test_score;
-- 3) Use a default — "absent across the board = 0 points"
SELECT student_id, name,
COALESCE(score_1, score_2, score_3, 0) AS final_score
FROM test_score;
The 2-argument version can be written as IFNULL
For the simpler 2-argument case — "if NULL, use a default; otherwise, the original value" — you can also write IFNULL(column, default) (supported in both this course's console and MySQL). COALESCE(email, 'not registered') and IFNULL(email, 'not registered') are exactly the same.
When you need 3 or more arguments, use COALESCE. Since COALESCE is the SQL standard, the same form works on PostgreSQL, Oracle, SQL Server, etc. — it's the more portable choice.
Knowledge Check
Answer each question one by one.
Q2What does SELECT FLOOR(3.84), CEILING(3.14); return?
Q3What does SELECT COALESCE(NULL, NULL, 'C', 'D'); return?