Skip to content

[Edit] SQL: ROUND() #6986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 65 additions & 10 deletions content/sql/concepts/commands/terms/round/round.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,90 @@
---
Title: 'ROUND'
Description: 'Rounds a value to the nearest integer or to a specific number of decimals if an optional value is provided.'
Title: 'ROUND()'
Description: 'Rounds a value to the nearest integer or to a specific number of decimals.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Data Types'
- 'Integers'
- 'Numbers'
- 'Values'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
---

The **`ROUND`** command rounds a value to the nearest integer or to a specific number of decimals if an optional value is provided.
In SQL, the **`ROUND()`** function rounds a value to the nearest integer or to a specific number of decimals. It's a powerful function that helps ensure consistency and accuracy in the output data.

## Syntax

```pseudo
ROUND(value, decimal_places)
```

If the optional `decimal_places` parameter is omitted, then the `value` is rounded with zero decimals.
**Parameters:**

## Example
- `value`: The value to be rounded.
- `decimal_places` (Optional): The total number of decimal places to round to. If omitted, the default is `0` (round to the nearest whole number).

The example below assumes there is a `grades` table with a column `test_scores` with values for all the exams administered in a semester, rounded to two decimal places:
## Example 1: Basic Rounding Using `ROUND()`

This example utilizes the `ROUND()` function to round a floating-point number to two decimal places:

```sql
SELECT ROUND(123.4567, 2) AS RoundedValue;
```

Here is the output:

```shell
RoundedValue
-------------
123.46
```

## Example 2: Rounding to Whole Numbers Using `ROUND()`

This example utilizes the `ROUND()` function without specifying decimal places to round a floating-point number to the nearest whole number:

```sql
SELECT names,
ROUND(AVG(test_scores), 2) AS semester_avg
FROM grades
GROUP BY names;
SELECT ROUND(98.7) AS RoundedValue;
```

Here is the output:

```shell
RoundedValue
-------------
99
```

## Example 3: Rounding to Negative Decimal Places Using `ROUND()`

This example utilizes the `ROUND()` function to round a floating-point number to the left of the decimal point:

```sql
SELECT ROUND(12345.67, -2) AS RoundedValue;
```

Here is the output:

```shell
RoundedValue
-------------
12300
```

## Frequently Asked Questions

### 1. Does `ROUND()` always round up?

No. `ROUND()` follows the standard rounding rule: it rounds up if the next digit is 5 or more and down otherwise.

### 2. Is `ROUND()` supported in all SQL databases?

Yes, `ROUND()` is supported in all SQL databases. However, the syntax and behavior may differ slightly.

### 3. What’s the difference between `ROUND()` and `TRUNC()`?

`ROUND()` adjusts values based on rounding rules, while `TRUNC()` simply cuts off digits without rounding.