SQL Functions   «Prev  Next»
Lesson 7 Arithmetic functions
Objective Understand the purpose of arithmetic functions.

SQL Arithmetic Functions

Arithmetic functions in SQL (Structured Query Language) serve an integral role in data manipulation and analysis within databases. These functions enable mathematical operations directly on the data residing in a SQL database, expanding the scope and power of SQL beyond its basic data retrieval and manipulation capabilities. The primary purpose of arithmetic functions in SQL is to perform mathematical calculations on numerical data stored in database tables. They perform basic arithmetic operations such as addition (+), subtraction (-), multiplication (*), and division (/), as well as a modulus operation (%) which returns the remainder of a division operation.
Here are some key applications of arithmetic functions:
  1. Data Aggregation: SQL arithmetic functions are extensively used in aggregate functions such as SUM(), AVG(), MIN(), MAX() to perform mathematical operations on multiple records in a database. For instance, AVG() computes the average of a set of numbers by adding them up (using the "+" operator) and then dividing by the count of numbers (using the "/" operator).
  2. Data Transformation: SQL arithmetic functions can be used to transform data in a column to meet specific requirements. For example, if you have data stored in Fahrenheit and want to convert it to Celsius, you can use the subtraction and division operators.
  3. Creating Computed Columns: Arithmetic functions can be used to create computed columns – new columns based on calculations performed on existing columns. This can be useful in numerous contexts, such as creating a 'total sales' column from 'quantity' and 'price per unit' columns in a sales table.
  4. Complex Calculations: Advanced arithmetic functions like POWER(), SQRT(), and others allow for more complex mathematical calculations, such as calculating the square root of a value or raising a value to the power of another.
  5. Rounding and Precision: Functions like ROUND(), CEILING(), and FLOOR() are used to adjust the precision of decimal numbers, rounding them up, down, or to the nearest whole number.

The use of arithmetic functions in SQL allows database administrators and data analysts to conduct sophisticated data analyses and manipulations directly within the database environment. This can contribute to increased efficiency and accuracy in data processing and decision making.
Numeric, or arithmetic/mathematical, functions are really helpful as you begin summarizing information in your tables. This will often be the case when you are building queries to support reports, you will be totalling groups of rows, categorizing information, and so on.
The numeric functions let you do more than just add and subtract values. They also let you do things like averaging all values for a given column, totaling all values, getting the maximum or minimum values for a given column, and more. You may recognize these functions as the same as those you use with many worksheet applications. They are nearly identical and operate in the same manner as you would expect. Just remember that you can assign column names to these results and that you can apply them in

GROUP BY

situations, giving you subtotals for results sets. The next several lessons cover the most commonly used functions.
Here are some examples showing usage of SQL Arithmetic Operators:
SQL> select 10+ 20;
+--------+
| 10+ 20 |
+--------+
|     30 |
+--------+
1 row in set (0.00 sec)

SQL> select 10 * 20;
+---------+
| 10 * 20 |
+---------+
|     200 |
+---------+
1 row in set (0.00 sec)

SQL> select 10 / 5;
+--------+
| 10 / 5 |
+--------+
| 2.0000 |
+--------+
1 row in set (0.03 sec)

SQL> select 12 %  5;
+---------+
| 12 %  5 |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

Calendar Tables

Another useful auxiliary table is a calendar table. One type of calendar table has a primary-key column that contains a row for each calendar date (past and future) and other columns that indicate the date's attributes:
  1. business day,
  2. holiday,
  3. international holiday,
  4. fiscal-month end,
  5. fiscal-year end,
  6. Julian date, and
  7. businessday offsets.

Another type of calendar table stores the starting and ending dates of events (in the columns event_id, start_date, and end_date, for example). Spreadsheets have more datearithmetic functions than DBMSs, so it might be easier to build a calendar table in a spreadsheet and then import it as a database table. Even if your DBMS has plenty of datearithmetic functions, it might be faster to look up data in a calendar table than to call these functions in a query.