ROUND() Function in SQL

ROUND() Function in SQL

2 mins readComment
Updated on Nov 23, 2023 16:45 IST

The ROUND() function in SQL is used to round a numeric field to the nearest value with a specified number of decimal places. It takes two arguments: the number to be rounded and the number of decimal places to round to. If the second argument is omitted, the function rounds to the nearest whole number. Let's read more about Round() Function in SQL.

2022_07_MicrosoftTeams-image-19.jpg

Table of contents

As the name suggests, the ROUND() function in SQL is used to round off a decimal value to a specific decimal place. The ROUND() function in SQL has the following features:

• It accepts all positive and negative numbers as a parameter.

• It also accepts zero as a parameter.

• Fractions can also be fed to the ROUND() function as a parameter.

Syntax:

ROUND(number, decimals)

Parameters:

• number: This parameter corresponds to the number to be rounded.

• decimals: This argument corresponds to the number of decimal places to which the number is to be rounded.

Returns:

The ROUND() function in SQL returns the rounded number up to a specific decimal place.

Note: We will work on MySQL to demonstrate the ROUND() function. The parameters may vary depending upon your relation database provider.

Now, we have a basic understanding of what the ROUND() function in SQL does. Let’s take a look at a few example use cases.

Encapsulation in C++ with a Real-Life Example
All About OOPs Concepts in C++
Function Overloading in C++
Recommended online courses

Best-suited PL/SQL courses for you

Learn PL/SQL with these high-rated online courses

5 K
80 hours
10 K
6 weeks
– / –
2 months
– / –
1 month
16.55 K
8 weeks
– / –
1 month

Example 1: Round a number to 3 decimal places


 
SELECT ROUND(99.852413, 3);
Copy code

Example 2: Round positive numbers, negative numbers, and zero up to 2 decimal places

— round positive number


 
SELECT ROUND(99.852413, 2);
Copy code

— round negative number


 
SELECT ROUND(-99.852413, 2);
Copy code

— round zero


 
SELECT ROUND(0, 2);
Copy code

Output:

2022_07_image-230.jpg

Example 3: Using the ROUND() function with Variables

Here we will declare two variables, namely my_number and my_decimal, and set values for them, respectively. Finally, we will use the ROUND() function with my_number and my_decimal as a parameter for rounding the number.


 
SET @my_number :=123.321;</p>
SET @my_decimal := 2;</p>
SELECT ROUND(@my_number, @my_decimal);</p>
Copy code

Output:

2022_07_image-231.jpg

Example 4: Round numeric data from a column

The ROUND() function can also be used to get the rounded values from a column. In the below example, we first create a table of grocery items and their respective prices using the below SQL query:


 
CREATE TABLE Grocery (</p>
item_name TEXT NOT NULL,</p>
selling_price DECIMAL(13,6) NOT NULL</p>
Copy code

Now that we have created the table, let’s insert some data into it using the below commands:


 
INSERT INTO Grocery VALUES ('chips',15.333);</p>
INSERT INTO Grocery VALUES ('Cheese',39.3347);</p>
INSERT INTO Grocery VALUES ('Popcorn',77.3654);</p>
INSERT INTO Grocery VALUES ('Mango',150.1548);</p>
INSERT INTO Grocery VALUES ('Apples',333.378845);</p>
INSERT INTO Grocery VALUES ('Brush',25.8477);</p>
Copy code
2022_07_image-233.jpg

Now let’s round the prices of each item to 2 decimal places using the below query:


 
SELECT item_name, selling_price, ROUND(selling_price, 2) AS Rounded_price FROM Grocery;
Copy code

Conclusion

In this article, we have explored the ROUND() function in SQL. We have managed to look into parameters that go into the function.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio