The ROUND() Function
Part of the Fundamentals section of Coddy's SQL journey — lesson 26 of 72.
The 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
For example,
Basic rounding to whole numbers:
SELECT ROUND(3.7);
-- Returns 4
SELECT ROUND(3.3);
-- Returns 3
SELECT ROUND(3.5);
-- Returns 4.0 (rounds up from .5)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)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:
SELECT
product_name,
ROUND(price, 2) as rounded_price
FROM products;Cheat sheet
The ROUND() function rounds numeric values to a specified number of decimal places:
ROUND(number, decimal_places)number: The number to rounddecimal_places: (Optional) Number of decimal places- If omitted, rounds to whole number
- If positive, rounds to that many decimal places
- If negative, rounds to the left of decimal point
Examples:
SELECT ROUND(3.7); -- Returns 4
SELECT ROUND(3.14159, 2); -- Returns 3.14
SELECT ROUND(3.14159, 0); -- Returns 3In queries:
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