BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

Rounding Retail Numbers with ROUND()

SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount
FROM orders

Result:

order_idcategoryrounded_revenuerounded_discount
O2001Electronics149.990.13
O2002Clothing54.460.08
O2003Home239.120.2
O2004Beauty32.680.05
O2005Grocery19.00.13

Step by step

  1. 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;
  2. 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;
  3. 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

Try each one in your head first, then open the answer.

1. Using orders, write a query to show revenue rounded to 0 decimal places.
SELECT ROUND(revenue, 0) FROM orders
2. Using store_sales, write a query to show revenue rounded to 0 decimal places.
SELECT ROUND(revenue, 0) FROM store_sales
3. Using products, write a query to show price rounded to 2 decimal places.
SELECT ROUND(price, 2) FROM products
4. Using reviews, write a query to show rating rounded to 0 decimal places.
SELECT ROUND(rating, 0) FROM reviews
5. Using employees, write a query to show salary rounded to 2 decimal places.
SELECT ROUND(salary, 2) FROM employees

Learn 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 →
About 5–10 minutes · no sign-up needed