| Lesson 7 | Arithmetic functions |
| Objective | Understand the purpose of arithmetic functions. |
Arithmetic functions and arithmetic expressions give SQL the ability to do more than retrieve stored values. They allow a query to calculate, transform, summarize, and compare numeric data directly inside the database engine. This matters because most real database work involves numbers that must be interpreted rather than merely displayed. Totals, averages, discounts, ratios, taxes, commissions, margins, and grouped summaries all depend on arithmetic logic.
When learners first encounter SQL, they often think of it as a language for selecting rows and columns. That is only part of the story. SQL also allows developers to build derived values from the data already stored in the tables. A query can multiply a quantity by a unit price, subtract costs from revenue, divide one measurement by another, or compute subtotals for entire groups of rows. Arithmetic functions therefore play an important role in reporting, data analysis, business logic, and operational query design.
This lesson explains the purpose of arithmetic functions in SQL, introduces the most common arithmetic operators, distinguishes scalar mathematical functions from aggregate functions, and shows why these tools are essential when building useful result sets. It also explains why auxiliary structures such as calendar tables sometimes help when arithmetic logic extends into date-related calculations.
The purpose of arithmetic in SQL is to let the database calculate values directly from stored data. This is useful because many business questions are mathematical in nature. A manager may want total monthly sales, an analyst may want an average score, an accountant may want year-to-date totals, and a report writer may want grouped subtotals by region or department. These tasks require more than simple retrieval. They require calculation.
Arithmetic also helps transform raw data into meaningful output. A table might store quantity and price as separate columns, but a report often needs a total amount. A table might store temperature in one scale, but a query may need to convert it to another. A system might store raw scores, while the user wants percentages or rounded values. Arithmetic expressions make those transformations possible without requiring the values to be permanently stored in the table.
This is why arithmetic functions are so closely tied to analytics and reporting. They allow the query itself to become more expressive. Instead of pulling data into another tool for calculation, SQL can often perform the needed mathematical work where the data already resides.
SQL supports the same core arithmetic ideas that learners already know from basic mathematics. The most common arithmetic operators are:
+-*/% where supportedThese operators can be evaluated directly in a query, even without referencing a table. This is useful because it demonstrates that SQL can process expressions as well as retrieve stored values.
SELECT 10 + 20;
SELECT 10 * 20;
SELECT 10 / 5;
SELECT 12 % 5;
These examples show that SQL can add, multiply, divide, and compute a remainder directly in the result set. Although they are simple, they illustrate an important principle: SQL expressions can return calculated output, and that same idea extends naturally to table columns.
Arithmetic becomes most useful when it is applied to values stored in database tables. In those situations, the query returns a computed column based on one or more existing columns. This is one of the most practical uses of SQL arithmetic.
For example, a sales table might contain quantity and price_per_unit columns. A report could calculate a line total using an expression such as:
SELECT quantity,
price_per_unit,
quantity * price_per_unit AS total_amount
FROM sales;
The calculated value total_amount is not necessarily stored in the base table. It is derived at query time. This makes arithmetic expressions especially powerful because the database can return new meaning from existing data without requiring the schema to be changed.
Other common examples include:
These are all examples of computed columns. They are one of the clearest reasons arithmetic functions matter in everyday SQL.
It is useful to distinguish arithmetic operators from arithmetic functions. Operators such as +, -, *, and / combine values directly in an expression. Functions, by contrast, are named operations that either transform one value or summarize many values.
For example, ROUND() is a function that adjusts numeric precision, while SUM() is a function that totals values across multiple rows. Both are mathematical in nature, but they serve different purposes than a direct operator like multiplication.
Thinking in these categories helps learners build cleaner SQL. Some tasks require row-by-row expressions, while others require set-level summaries or numeric transformations. The more clearly those roles are understood, the easier it becomes to choose the right SQL tool.
One major purpose of arithmetic functions in SQL is summarization. Aggregate functions allow the database to calculate a single result from many rows. This is especially important in reporting and analysis.
The most commonly used aggregate functions include:
SUM() to total numeric valuesAVG() to compute an averageMIN() to find the smallest valueMAX() to find the largest valueThese functions are arithmetic in purpose because they extract a meaningful numerical summary from a collection of records. For example, AVG() depends on arithmetic even if the underlying calculation is hidden behind the function name. It conceptually adds values and divides by the number of rows being averaged.
This is why arithmetic is so important for reports. A report rarely needs every raw transaction shown individually without interpretation. Instead, users often want totals, averages, highest values, lowest values, and grouped summaries. Aggregate functions make that possible directly in SQL.
Arithmetic summaries become even more useful when combined with GROUP BY. Grouping allows SQL to calculate subtotals or category-based summaries rather than a single summary for the whole table.
For example, a sales query might total revenue by region, a payroll query might average salary by department, or an inventory query might show the maximum quantity by product category. In each case, the arithmetic function is made more powerful because it is applied within groups of related rows.
This is why the original lesson correctly points toward GROUP BY situations. Arithmetic in SQL is not only about one-off calculations. It is also about summarizing result sets in a structured and meaningful way. Once a learner understands grouping, aggregate functions become much more valuable because they can produce subtotals instead of only grand totals.
GROUP BY
That keyword signals an important step in SQL thinking: instead of asking for calculations across all rows, the developer asks for calculations within logical categories. This is central to business reporting.
In addition to aggregate functions, SQL also supports scalar mathematical functions that operate on individual numeric values. These functions do not summarize sets of rows. Instead, they transform one value at a time.
Examples mentioned in the original lesson include:
ROUND()CEILING()FLOOR()POWER()SQRT()These functions are useful when the query needs to shape or refine numeric output. A reporting query may round decimal results, a calculation may raise a value to a power, or a numeric analysis may require a square root. In each case, SQL is doing more than returning stored numbers. It is applying mathematical logic to produce a more useful result.
This distinction matters: aggregate functions operate across rows, while scalar mathematical functions operate on individual values. Both belong to the broader topic of arithmetic in SQL, but they support different kinds of tasks.
Arithmetic functions are especially important when building reports. Reports often need calculated information rather than raw stored values. A total sales report may require SUM(), a performance report may require AVG(), and a financial summary may require percentages, margins, or rounded values.
This is why numeric functions feel familiar to many learners. They resemble the kinds of calculations used in spreadsheet applications. The difference is that SQL performs these calculations directly against table data, often across large numbers of rows, and can combine them with joins, filtering, grouping, and ordering logic.
That makes arithmetic one of the core bridges between relational data storage and analytical meaning. The database is not just storing quantities, prices, or counts. It is also helping interpret them.
The lesson ends by mentioning calendar tables, and that topic is worth preserving because it broadens the learner’s understanding of arithmetic in SQL. A calendar table is an auxiliary table designed to hold date-related information in a structured way. One type contains a row for each date and includes columns that describe attributes of that date.
Such attributes may include:
Another type of calendar table stores the start and end dates of events, such as project windows, promotional periods, or fiscal ranges. This is useful because date arithmetic can become complicated, and not every DBMS provides the same breadth of built-in date functions.
In some situations, it may be faster or simpler to look up precomputed date information in a calendar table than to recalculate it repeatedly in a query. That is why calendar tables belong in this lesson: they show that arithmetic thinking in SQL extends beyond simple numeric operators and into the design of supporting structures for complex date-oriented logic.
The purpose of arithmetic functions in SQL is to make the query language more useful for real business and analytical work. Arithmetic turns SQL from a passive retrieval language into an active calculation environment. It allows the database to return totals, averages, adjusted values, computed columns, subtotals, and mathematically transformed output.
Once learners understand arithmetic operators, aggregate functions, scalar mathematical functions, and grouped summaries, they are better prepared to write richer queries. They can build reports that answer business questions, not just lists of stored values. They can also design result sets that communicate meaning more clearly through calculation and summarization.
That is the real purpose of arithmetic functions: they let SQL participate directly in analysis, not just storage.