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 →Example
Tidy decimals
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
- Rounds the number.
SELECT order_id, category, ROUND(revenue, 2) AS rounded_revenue, ROUND(discount, 2) AS rounded_discount FROM orders - 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 - 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
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 orders2. 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_sales3. 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 products4. 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 reviews5. 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 shipmentsLearn 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 →