ROUND() Function in MySQL
Last Updated :
24 Apr, 2023
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round-off, it rounds off the number to the nearest integer.
Syntax
ROUND(X, D)
Parameter Explanation
This method accepts two parameters in the syntax, as mentioned above and described below -
- X: The number which to is rounded.
- D: Number of decimal places up to which the given number is to be rounded. It is optional. If not given it round off the number to the closest integer. If it is negative, then the number is rounded to the left side of the decimal point.
- Returns: It returns the number after rounding to the specified places.
Example-1
Rounding off a number when D is not specified.
Rounding a Negative number.
Query:
SELECT ROUND(-10.11) AS Rounded_Number;
Output:
Rounding a Positive number.
Query:
SELECT ROUND(100.61) AS Rounded_Number;
Output:
Example-2
Rounding off a number when D is negative(-ve).
Rounding a Negative number.
Query:
SELECT ROUND(-1567.1100, -3) AS Rounded_Number;
Output:
Rounding a Positive number.
Query:
SELECT ROUND(1016.6089, -1) AS Rounded_Number;
Output:
Example-3
Rounding off a number when D is positive(+ve).
Rounding a Negative number up to 2 decimal places.
Query:
SELECT ROUND(-1567.1160, 2) AS Rounded_Number;
Output:
Rounding a Positive number up to three decimal places.
Query:
SELECT ROUND(1016.6019, 3) AS Rounded_Number;
Output:
Example-4
The ROUND Function can also be used to find the rounded values for the column data. In this example, we are going to find rounded values for the Price column. To demonstrate create a table named Product.
Query:
CREATE TABLE Product(
Product_id INT AUTO_INCREMENT,
Product_name VARCHAR(100) NOT NULL,
Buying_price DECIMAL(13, 6) NOT NULL,
Selling_price DECIMAL(13, 6) NOT NULL,
Selling_Date Date NOT NULL,
PRIMARY KEY(Product_id)
);
Now insert some data into the Product table -
Query:
INSERT INTO
Product(Product_name, Buying_price, Selling_price, Selling_Date)
VALUES
('P6', 1060.865460, 1700.675400, '2020-08-26'),
('P2', 2000.154300, 3050.986700, '2020-08-27'),
('P1', 4000.874300, 5070.786500, '2020-08-28'),
('P2', 2090.654300, 3050.896500, '2020-09-01'),
('P3', 5900.543280, 7010.654700, '2020-09-04'),
('P4', 4000.353200, 4500.125400, '2020-09-05'),
('P5', 5010.768900, 6000.873200, '2020-09-08');
So, the Product Table is as -
Now, we are going to round off both Buying_price and Selling_price columns up to 2 decimal places.
Query:
SELECT Product_name, Buying_price,
ROUND(Buying_price, 2) Rounded_Bprice,
Selling_price, ROUND(Selling_price, 2)
Rounded_Sprice
FROM Product;
Output:
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
SQL | WITH Clause SQL queries can sometimes be complex, especially when you need to deal with multiple nested subqueries, aggregations, and joins. This is where the SQL WITH clause also known as Common Table Expressions (CTEs) comes in to make life easier. The WITH Clause is a powerful tool that simplifies complex SQ
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read