BBampaa

Lessons › SQL › Chapter 4: Shaping data

SQL lesson

ROUND()

ROUND cuts a number down to the decimal places you want. ROUND(149.9875, 2) gives 149.99. The second number is how many places to keep. 0 gives a whole number.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Tidy decimals

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. Rounds the number.
    SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount FROM orders
  2. The second number is how many decimal places to keep.
    SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount FROM orders
  3. 0 places gives a whole number.
    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, show revenue, and next to it revenue rounded to 0 decimal places (a whole number). Name the second column revenue_rounded.
SELECT revenue, ROUND(revenue, 0) AS revenue_rounded FROM orders
2. Using store_sales, show revenue, and next to it revenue rounded to 1 decimal place. Name the second column revenue_rounded.
SELECT revenue, ROUND(revenue, 1) AS revenue_rounded FROM store_sales
3. Using products, show price, and next to it price rounded to 0 decimal places (a whole number). Name the second column price_rounded.
SELECT price, ROUND(price, 0) AS price_rounded FROM products
4. Using reviews, show rating, and next to it rating rounded to 0 decimal places (a whole number), so each review shows whole stars. Name the second column rating_rounded.
SELECT rating, ROUND(rating, 0) AS rating_rounded FROM reviews
5. Using shipments, show weight_kg, and next to it weight_kg rounded to 1 decimal place. Name the second column weight_kg_rounded.
SELECT weight_kg, ROUND(weight_kg, 1) AS weight_kg_rounded FROM shipments

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