The ROUND() function in SQL is a mathematical function used to round a numeric value to a specified number of decimal places. If the number of decimal places is not specified, it rounds the value to the nearest whole number.
- Useful for financial calculations, reports and data analysis.
- Round numbers to a specified number of decimal places.
Syntax
ROUND(number, decimal_places)- number: The numeric value that you want to round.
- decimal_places: The number of decimal places to which the value should be rounded. This argument is optional in many SQL databases.
Note: The ROUND() function is supported by MySQL, SQL Server, SQLite, PostgreSQL and Oracle. The exact behavior for negative precision can vary between database systems.
Working
Below are some examples of the ROUND() function to understand how it is used with numeric values.
Example 1: Round to the Nearest Integer
In this example, we will round a decimal number to the nearest whole number.
Query:
SELECT ROUND(25.6) AS rounded_value;Output:
Example 2: Round to Two Decimal Places
In this example, we will round a number to two decimal places.
Query:
SELECT ROUND(45.6789, 2) AS rounded_value;Output:
Example 3: Use ROUND() on a Table Column
Let us create a products table containing product prices.
Now, we can use the ROUND() function to round the product prices to two decimal places.
Query:
SELECT product_name,price,ROUND(price, 2) AS rounded_priceFROM products;
Output:
- Rounds each product price to two decimal places.
- The original price values remain unchanged.
- The rounded values are displayed as rounded_price.