SQL ROUND()
In SQL, the ROUND() function rounds a value to the nearest integer or to a specific number of decimals. It’s a powerful function that helps ensure consistency and accuracy in the output data.
Syntax
ROUND(value, decimal_places)
Parameters:
value: The value to be rounded.decimal_places(Optional): The total number of decimal places to round to. If omitted, the default is0(round to the nearest whole number).
Example 1: Basic Rounding Using ROUND()
This example utilizes the ROUND() function to round a floating-point number to two decimal places:
SELECT ROUND(123.4567, 2) AS RoundedValue;
Here is the output:
RoundedValue-------------123.46
Example 2: Rounding to Whole Numbers Using ROUND()
This example utilizes the ROUND() function without specifying decimal places to round a floating-point number to the nearest whole number:
SELECT ROUND(98.7) AS RoundedValue;
Here is the output:
RoundedValue-------------99
Example 3: Rounding to Negative Decimal Places Using ROUND()
This example utilizes the ROUND() function to round a floating-point number to the left of the decimal point:
SELECT ROUND(12345.67, -2) AS RoundedValue;
Here is the output:
RoundedValue-------------12300
Frequently Asked Questions
1. Does ROUND() always round up?
No. ROUND() follows the standard rounding rule: it rounds up if the next digit is 5 or more and down otherwise.
2. Is ROUND() supported in all SQL databases?
Yes, ROUND() is supported in all SQL databases. However, the syntax and behavior may differ slightly.
3. What’s the difference between ROUND() and TRUNC()?
ROUND() adjusts values based on rounding rules, while TRUNC() simply cuts off digits without rounding.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.17 hours
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.5 hours