SQL round

Created By: chatGPT

SQL provides the ROUND() function to round numeric values to a specified number of decimal places. This function is particularly useful when you want to simplify the presentation of data or perform calculations without excessive decimal points.
SELECT ROUND(column_name, decimal_places)
FROM table_name;

In the ROUND() function, you will pass two arguments:

  • The first argument is the column or the numeric value you wish to round.
  • The second argument is the number of decimal places to which you want the value rounded. If you omit the second argument, the value will be rounded to the nearest whole number.
SELECT ROUND(123.4567, 2);  -- Returns 123.46
SELECT ROUND(123.4567);    -- Returns 123
You can also use the ROUND() function within aggregated functions, such as SUM() or AVG(), to maintain consistency in the number of decimal places for aggregated results.
SELECT ROUND(AVG(price), 2) AS average_price
FROM products;
It's important to note how the ROUND() function behaves when rounding. If the digit to the right of your specified decimal place is 5 or greater, the last digit will increase by one. Otherwise, it remains the same.
SELECT ROUND(2.675, 2);   -- May return 2.67 or 2.68
SELECT ROUND(2.685, 2);   -- Returns 2.69
Using negative numbers in the second argument allows you to round to the left of the decimal point. For example, rounding to the nearest ten or hundred.
SELECT ROUND(1234.56, -1);  -- Returns 1230
SELECT ROUND(1234.56, -2);  -- Returns 1200
Introduction And SetupSelectFromWhereJoinInner JoinLeft JoinRight JoinFull JoinOnGroup ByHavingOrder ByLimitInsert IntoValuesUpdateSetDeleteCreate TableAlter TableDrop TableCreate IndexDrop IndexUnionUnion AllIntersectExceptCaseWhenThenElseEndInBetweenLikeIs NullIs Not NullExistsNot ExistsAndOrNotAsWithDistinctCountSumAvgMinMaxCastCoalesceNullifSubstringConcatLowerUpperTrimReplaceRoundFloorCeilAbsModCurrent_dateCurrent_timeCurrent_timestamp