Lessons › SQL › Chapter 4: Shaping data
SQL lesson
ROUND()
ROUND() trims a number to however many decimal places you specify, so a messy result like 149.9875 becomes a clean 150.00 or 149.99.
Try this lesson free →Example
Rounding Retail Numbers with ROUND()
SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount
FROM orders| order_id | category | rounded_revenue | rounded_discount |
|---|---|---|---|
| O2001 | Electronics | 149.99 | 0.13 |
| O2002 | Clothing | 54.46 | 0.08 |
| O2003 | Home | 239.12 | 0.2 |
| O2004 | Beauty | 32.68 | 0.05 |
| O2005 | Grocery | 19.0 | 0.13 |
Step by step
- Run the query without ROUND so you can see what the unfiltered values look like.
SELECT order_id, category, revenue, ROUND(discount, 2) AS rounded_discount FROM orders; - ROUND rounds the value to the nearest integer (or to N decimal places with a second argument).
SELECT order_id, category, ROUND(revenue, 2), ROUND(discount, 2) AS rounded_discount FROM orders; - AS gives the ROUND result a friendly column name so the output reads cleanly.
SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount FROM orders;
Practice
1. Using orders, write a query to show revenue rounded to 0 decimal places.
SELECT ROUND(revenue, 0) FROM orders2. Using store_sales, write a query to show revenue rounded to 0 decimal places.
SELECT ROUND(revenue, 0) FROM store_sales3. Using products, write a query to show price rounded to 2 decimal places.
SELECT ROUND(price, 2) FROM products4. Using reviews, write a query to show rating rounded to 0 decimal places.
SELECT ROUND(rating, 0) FROM reviews5. Using employees, write a query to show salary rounded to 2 decimal places.
SELECT ROUND(salary, 2) FROM employeesLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →