In this section, we will explore various numeric functions available in SQL. Numeric functions are used to perform operations on numeric data types and return numeric values. These functions are essential for data analysis, reporting, and various other database operations.
Key Concepts
- Arithmetic Functions: Perform basic arithmetic operations.
- Rounding Functions: Round numeric values to a specified precision.
- Trigonometric Functions: Perform trigonometric calculations.
- Other Numeric Functions: Include functions for absolute values, square roots, and more.
Common Numeric Functions
- Arithmetic Functions
ABS(number)
: Returns the absolute value of a number.CEIL(number)
: Returns the smallest integer greater than or equal to a number.FLOOR(number)
: Returns the largest integer less than or equal to a number.MOD(number, divisor)
: Returns the remainder of a number divided by a divisor.POWER(number, exponent)
: Returns the value of a number raised to the power of an exponent.
- Rounding Functions
ROUND(number, decimals)
: Rounds a number to a specified number of decimal places.TRUNC(number, decimals)
: Truncates a number to a specified number of decimal places.
- Trigonometric Functions
SIN(number)
: Returns the sine of a number.COS(number)
: Returns the cosine of a number.TAN(number)
: Returns the tangent of a number.
- Other Numeric Functions
SQRT(number)
: Returns the square root of a number.EXP(number)
: Returns e raised to the power of a number.LOG(number)
: Returns the natural logarithm of a number.LOG10(number)
: Returns the base-10 logarithm of a number.
Practical Examples
Example 1: Using ABS
and MOD
Explanation:
ABS(-15)
returns15
.MOD(10, 3)
returns1
.
Example 2: Using CEIL
and FLOOR
Explanation:
CEIL(4.2)
returns5
.FLOOR(4.8)
returns4
.
Example 3: Using ROUND
and TRUNC
Explanation:
ROUND(123.456, 2)
returns123.46
.TRUNC(123.456, 2)
returns123.45
.
Example 4: Using Trigonometric Functions
Explanation:
SIN(PI()/2)
returns1
.COS(PI())
returns-1
.TAN(PI()/4)
returns1
.
Example 5: Using SQRT
and LOG
Explanation:
SQRT(16)
returns4
.LOG(2.71828)
returns approximately1
.LOG10(100)
returns2
.
Exercises
Exercise 1: Basic Arithmetic Functions
Task: Write a query to calculate the absolute value of -20
, the ceiling of 7.3
, and the floor of 7.8
.
Exercise 2: Rounding and Truncating
Task: Write a query to round the number 45.6789
to 2 decimal places and truncate it to 1 decimal place.
Exercise 3: Trigonometric Functions
Task: Write a query to find the sine, cosine, and tangent of PI()/3
.
Exercise 4: Square Root and Logarithms
Task: Write a query to find the square root of 25
, the natural logarithm of 7.389
, and the base-10 logarithm of 1000
.
Summary
In this section, we covered various numeric functions in SQL, including arithmetic, rounding, trigonometric, and other numeric functions. These functions are essential for performing calculations and data analysis in SQL. By practicing the provided examples and exercises, you should now have a solid understanding of how to use these functions effectively.
SQL Course
Module 1: Introduction to SQL
Module 2: Basic SQL Queries
Module 3: Working with Multiple Tables
Module 4: Advanced Data Filtering
- Using LIKE for Pattern Matching
- IN and BETWEEN Operators
- NULL Values and IS NULL
- Aggregating Data with GROUP BY
- HAVING Clause
Module 5: Data Manipulation
Module 6: Advanced SQL Functions
Module 7: Subqueries and Nested Queries
- Introduction to Subqueries
- Correlated Subqueries
- EXISTS and NOT EXISTS
- Using Subqueries in SELECT, FROM, and WHERE Clauses
Module 8: Indexes and Performance Optimization
- Understanding Indexes
- Creating and Managing Indexes
- Query Optimization Techniques
- Analyzing Query Performance