The ROUND() Function
Part of the Fundamentals section of Coddy's SQL journey — lesson 26 of 72.
The
Note: The
Note: When the digit after the last kept decimal place is 5 or greater, the last kept decimal rounds up.Practical example with a table:
ROUND() function is used to round a numeric value to a specified number of decimal places. To use the function follow this syntax:ROUND(number, decimal_places)number: The number you want to round
decimal_places: (Optional) The number of decimal places to round to- If omitted, rounds to the nearest whole number
- If positive, rounds to that many decimal places
- If negative, rounds to the left of the decimal point
SELECT ROUND(3.7);
-- Returns 4.0
SELECT ROUND(3.3);
-- Returns 3.0
SELECT ROUND(3.5);
-- Returns 4.0 (rounds up from .5)ROUND() function in SQLite always returns a floating-point (REAL) value, which is why the results above include a decimal point (e.g., 4.0 instead of 4).Rounding to specific decimal places:SELECT ROUND(3.14159, 2);
-- Returns 3.14
SELECT ROUND(3.14159, 1);
-- Returns 3.1
SELECT ROUND(3.14159, 0);
-- Returns 3.0
SELECT ROUND(3.145, 2);
-- Returns 3.15 (rounds up because the next digit is 5)SELECT
product_name,
ROUND(price, 2) as rounded_price
FROM products;Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns