SQL numeric functions are built-in operations that perform calculations on numeric data types. These functions are essential for data manipulation and analysis in relational databases.
SQL provides various numeric functions to handle different mathematical operations:
Returns the absolute value of a number.
SELECT ABS(-15.7) AS absolute_value;
Result: 15.7
Rounds a number to a specified number of decimal places.
SELECT ROUND(15.79, 1) AS rounded_value;
Result: 15.8
CEILING returns the smallest integer greater than or equal to the given number, while FLOOR returns the largest integer less than or equal to the given number.
SELECT CEILING(15.1) AS ceiling_value, FLOOR(15.9) AS floor_value;
Result: ceiling_value = 16, floor_value = 15
Numeric functions can be combined with other SQL SELECT statements and aggregate functions to perform complex calculations:
SELECT
product_name,
price,
ROUND(price * 0.9, 2) AS discounted_price
FROM
products
WHERE
ABS(price - 100) < 20;
This query selects products with prices within 20 units of 100, calculates a 10% discount, and rounds the result to two decimal places.
SQL numeric functions are powerful tools for data manipulation and analysis. By mastering these functions, you can perform complex calculations efficiently within your database queries, enhancing your data processing capabilities.