How To Use Mathematical Expressions and Aggregate Functions in SQL

Published on January 28, 2022

Technical Writer

How To Use Mathematical Expressions and Aggregate Functions in SQL


Structured Query Language (SQL) is used to store, manage, and organize information in a relational database management system (RDBMS). SQL can also perform calculations and manipulate data through expressions. Expressions combine various SQL operators, functions, and values, to calculate a value. Mathematical expressions are commonly used to add, subtract, divide, and multiply numerical values. Additionally, aggregate functions are used to evaluate and group values to generate a summary, such as the average or sum of values in a given column. Mathematical and aggregate expressions can provide valuable insights through data analysis that can inform future decision-making.

In this tutorial, you’ll practice using mathematical expressions. First, you’ll use numeric operations on a calculator, then use those operators on sample data to perform queries with aggregate functions, and finish with a business scenario to query sample data for more complex information and analysis.


To complete this tutorial, you will need:

  • A server running Ubuntu 20.04, with a non-root user with sudo administrative privileges and firewall enabled. Follow our Initial Server Setup with Ubuntu 20.04 to get started.
  • MySQL installed and secured on the server. Follow our How To Install MySQL on Ubuntu 20.04 guide to set this up. This guide assumes you’ve also set up a non-root MySQL user, as outlined in Step 3 of this guide.

Note: Please note that many RDBMSs use their own unique implementations of SQL. Although the commands outlined in this tutorial will work on most RDBMSs, the exact syntax or output may differ if you test them on a system other than MySQL.

To practice many of the mathematical expression examples in this tutorial, you’ll need a database and table loaded with sample data. If you do not have one ready to insert, you can read the following Connecting to MySQL and Setting up a Sample Database section to learn how to create a database and table. This tutorial will refer to this sample database and table throughout.

Connecting to MySQL and Setting up a Sample Database

If your SQL database runs on a remote server, SSH into your server from your local machine:

  1. ssh sammy@your_server_ip

Next, open the MySQL prompt, replacing sammy with your MySQL user account information:

  1. mysql -u sammy -p

Create a database named mathDB:


If the database was created successfully, you’ll receive the following output:

Query OK, 1 row affected (0.01 sec)

To select the mathDB database run the following USE statement:

  1. USE mathDB;
Database changed

After selecting the database, create a table within it using the CREATE TABLE command. For this tutorial’s example, we’ll create a table named product_information to store inventory and sales information for a small tea shop. This table will hold the following eight columns:

  • product_id: represents the values of the int data type and will serve as the table’s primary key. This means each value in this column will function as a unique identifier for its respective row.
  • product_name: details the names of the products using the varchar data type with a maximum of 30 characters.
  • product_type: stores the types of products, as demonstrated by the varchar data type with a maximum of 30 characters.
  • total_inventory: represents how many units of each product are left in storage, using the int data type with a maximum of 200.
  • product_cost: displays the price of each product purchased at cost using the decimal data type with a maximum of 3 values to the left, and 2 values after the decimal point.
  • product_retail: stores prices for each product sold at retail, as shown by the decimal data type with a maximum of 3 values to the left, and 2 values after the decimal point.
  • store_units: using values of the int data type, displays how many units of the specific product are available for in-store sales inventory.
  • online_units: represents how many units of the specific product are available for online sales inventory using values of the int data type

Create this sample table by running the following command:

  1. CREATE TABLE product_information (
  2. product_id int,
  3. product_name varchar(30),
  4. product_type varchar(30),
  5. total_inventory int(200),
  6. product_cost decimal(3, 2),
  7. product_retail decimal(3, 2),
  8. store_units int(100),
  9. online_units int(100),
  10. PRIMARY KEY (product_id)
  11. );
Query OK, 0 rows affected, 0 warnings (0.01 sec)

Now insert some sample data into the empty table:

  1. INSERT INTO product_information
  2. (product_id, product_name, product_type, total_inventory, product_cost, product_retail, store_units, online_units)
  4. (1, 'chamomile', 'tea', 200, 5.12, 7.50, 38, 52),
  5. (2, 'chai', 'tea', 100, 7.40, 9.00, 17, 27),
  6. (3, 'lavender', 'tea', 200, 5.12, 7.50, 50, 112),
  7. (4, 'english_breakfast', 'tea', 150, 5.12, 7.50, 22, 74),
  8. (5, 'jasmine', 'tea', 150, 6.17, 7.50, 33, 92),
  9. (6, 'matcha', 'tea', 100, 6.17, 7.50, 12, 41),
  10. (7, 'oolong', 'tea', 75, 7.40, 9.00, 10, 29),
  11. (8, 'tea sampler', 'tea', 50, 6.00, 8.50, 18, 25),
  12. (9, 'ceramic teapot', 'tea item', 30, 7.00, 9.75, 8, 15),
  13. (10, 'golden teaspoon', 'tea item', 100, 2.00, 5.00, 18, 67);
Query OK, 10 rows affected (0.01 sec) Records: 10 Duplicates: 0 Warnings: 0

Once you’ve inserted the data, you’re ready to begin using mathematical expressions.

Calculating with Mathematical Expressions

In SQL, you typically use SELECT to query your database and retrieve the desired result set. However, you can also use the SELECT keyword to perform a variety of mathematical operations.

Keep in mind that in a real-life scenario, SQL is primarily used to query and make calculations from values in your actual database. But for this section, you’ll use SELECT solely for numerical values to get familiar with the syntax of mathematical expressions and operators.

Before you begin, here’s an overview of the operators you can use to perform six arithmetic operations in SQL. Please note this list is not comprehensive and that many RDBMSs have a unique set of mathematical operators:

  • Addition uses the + symbol
  • Subtraction uses the - symbol
  • Multiplication uses the * symbol
  • Division uses the / symbol
  • Modulo operations use the % symbol
  • Exponentiation uses POW(x,y)

You can practice running different types of calculations with your own value combinations. We’ll demonstrate using the following examples, starting with an addition equation:

  1. SELECT 893 + 579;
+-----------+ | 893 + 579 | +-----------+ | 1472 | +-----------+ 1 row in set (0.00 sec)

Note that because you’re not retrieving any data from the database and you’re only calculating raw numbers, you don’t need to include a FROM clause in this or other example queries in this section.

Now perform a calculation with the subtraction operator. Also, be aware that you can calculate values with decimals like the following:

  1. SELECT 437.82 - 66.34;
+----------------+ | 437.82 - 66.34 | +----------------+ | 371.48 | +----------------+ 1 row in set (0.00 sec)

You can include multiple values and operators in a single calculation in SQL. The following example calculation uses three multiplication operators to find the product of four numbers:

  1. SELECT 60 * 1234 * 2 * 117;
+---------------------+ | 60 * 1234 * 2 * 117 | +---------------------+ | 17325360 | +---------------------+ 1 row in set (0.00 sec)

Next, calculate a division problem that combines a decimal value and a whole number value, such as the following:

  1. SELECT 2604.56 / 41;
+--------------+ | 2604.56 / 41 | +--------------+ | 63.525854 | +--------------+ 1 row in set (0.00 sec)

Another division operator is %, or a modulo operator, which calculates the remaining value after dividing the dividend by the divisor:

  1. SELECT 38 % 5;
+--------+ | 38 % 5 | +--------+ | 3 | +--------+ 1 row in set (0.00 sec)

Another operator that may be useful is POW(x,y), which calculates the power of the exponent (y) for the specified base-value (x):

  1. SELECT POW(99,9);
+---------------------+ | POW(99,9) | +---------------------+ | 9.13517247483641e17 | +---------------------+ 1 row in set (0.01 sec)

Now that you’ve practiced calculating with each operation on its own, you can try combining different mathematical operators to practice more complex mathematical equations.

Understanding Order of Operations in SQL

You may be familiar with the term PEMDAS, which stands for parentheses, exponents, multiplication, division, addition, and subtraction. This term serves as a guideline for the order of operations necessary to solve more complex equations. PEMDAS is the term used in the U.S., while other countries may use different acronyms to represent their order of operations rule.

When it comes to combining different mathematical operations nested within parentheses, SQL reads them left to right, and then values beginning from the inside to the outside. For this reason, ensure your values within parentheses accurately capture the problem you’re trying to solve.

Try a calculation using parentheses and a couple of different operators:

  1. SELECT (2 + 4 ) * 8;
+-----------+ | (2+4) * 8 | +-----------+ | 48 | +-----------+ 1 row in set (0.00 sec)

Remember, parentheses placement matters and if you’re not careful, the entire result can change. For example, the following uses the same three values and operators, but with a different parentheses placement, this produces a different result:

  1. SELECT 2 + (4 * 8);
+-------------+ | 2 + (4 * 8) | +-------------+ | 34 | +-------------+ 1 row in set (0.00 sec)

If you prefer to perform calculations without parentheses, you can do that as well. Remember that there is still the order of operations rule; therefore, similar to the parentheses placement, verify that this is the equation you want based on the operation order it will be evaluated upon. In the following example, you’ll notice that the division operation takes precedence over the subtraction operator and results in a negative value:

  1. SELECT 100 / 5 - 300;
+---------------+ | 100 / 5 - 300 | +---------------+ | -280.0000 | +---------------+ 1 row in set (0.00 sec)

You’ve successfully used mathematical expressions for numeric calculations and for complex calculations by combining various operators. Next, you’ll use the sample data to make calculations with aggregate functions and generate new information about your data.

Analyzing Data with Aggregate Functions

Imagine that you’re the owner of a small tea shop, and you want to perform calculations pertinent to the information you’ve stored in your database. SQL can use mathematical expressions to query and manipulate data by retrieving it from your database table and different columns. This helps with generating new information about the data you’re interested in analyzing. In this section, you’ll practice querying and manipulating sample data with aggregate functions to find information about the tea shop’s business.

The primary aggregate functions in SQL include SUM, MAX, MIN, AVG, and COUNT. The SUM function adds all of the values in a column. For example, use SUM to add up the amount for the total_inventory column in your sample data set:

  1. SELECT SUM(total_inventory) FROM product_information;
+----------------------+ | SUM(total_inventory) | +----------------------+ | 1155 | +----------------------+ 1 row in set (0.00 sec)

The MAX function finds the maximum value held in the selected column. For this example, use MAX to query the maximum amount spent for products listed in the product_cost column, and use the AS statement to re-label the header so it reads more clearly:

  1. SELECT MAX(product_cost) AS cost_max
  2. FROM product_information;
+----------+ | cost_max | +----------+ | 7.40 | +----------+ 1 row in set (0.00 sec)

The MIN function is the opposite of the MAX function because it calculates the minimum value. Use MIN to query for the minimum amount spent for product_retail:

  1. SELECT MIN(product_retail) AS retail_min
  2. FROM product_information;
+------------+ | retail_min | +------------+ | 5.00 | +------------+ 1 row in set (0.00 sec)

The AVG function calculates the average of all the values from the specified column in your table. Also, note that you can run more than one aggregate function in the same query. Try combining a query to find the average cost of products sold at retail and products purchased at cost:

  1. SELECT AVG(product_retail) AS retail_average,
  2. AVG(product_cost) AS cost_average
  3. FROM product_information;
+----------------+--------------+ | retail_average | cost_average | +----------------+--------------+ | 7.875000 | 5.750000 | +----------------+--------------+ 1 row in set (0.00 sec)

The COUNT function operates differently from the others because it calculates a value from the table itself by counting the number of rows returned by the query. Use the COUNT function with the WHERE statement to query the number of products whose retail value is more than $8.00:

  1. SELECT COUNT(product_retail)
  2. FROM product_information
  3. WHERE product_retail > 8.00;
+-----------------------+ | COUNT(product_retail) | +-----------------------+ | 4 | +-----------------------+ 1 row in set (0.00 sec)

Now query the number of products from product_cost that were purchased by the store for more than $8.00:

  1. SELECT COUNT(product_cost)
  2. FROM product_information
  3. WHERE product_cost > 8.00;
+---------------------+ | COUNT(product_cost) | +---------------------+ | 0 | +---------------------+ 1 row in set (0.00 sec)

You’ve successfully used aggregate functions to provide a summary of values such as the maximum, minimum, average, and count. You retrieved this information from the sample data to simulate a real-world scenario. In the final section, you’ll apply everything you’ve learned about mathematical expressions and aggregate functions to perform more detailed queries and analyses on the sample data for the small tea shop.

Applying Mathematical Expressions in a Business Scenario

This section provides a few example scenarios that involve performing data analysis to help the tea shop owners with decision-making as it relates to their business.

As a first scenario, calculate the total units currently available in inventory to understand how many products are remaining for in-store and online sales. This query will also include the DESC statement to organize the data from the largest to smallest amount. Typically RDBMSs will default to ascending order, but this example includes the DESC option which allows you to view the data in descending order:

  1. SELECT product_name,
  2. total_inventory - (store_units + online_units)
  3. AS remaining_inventory
  4. FROM product_information
  5. ORDER BY(remaining_inventory) DESC;
+-------------------+---------------------+ | product_name | remaining_inventory | +-------------------+---------------------+ | chamomile | 110 | | chai | 56 | | english_breakfast | 54 | | matcha | 47 | | lavender | 38 | | oolong | 36 | | jasmine | 25 | | golden teaspoon | 15 | | tea sampler | 7 | | ceramic teapot | 7 | +-------------------+---------------------+ 10 rows in set (0.00 sec)

This query is useful because it calculates the remaining inventory, which can help the tea shop owners make plans to purchase more orders if they’re running low on a product.

For the next scenario, you’ll analyze and compare the amount of revenue from in-store and online sales:

  1. SELECT product_name,
  2. (online_units * product_retail) AS o,
  3. (store_units * product_retail) AS s
  4. FROM product_information;
+-------------------+--------+--------+ | product_name | o | s | +-------------------+--------+--------+ | chamomile | 390.00 | 285.00 | | chai | 243.00 | 153.00 | | lavender | 840.00 | 375.00 | | english_breakfast | 555.00 | 165.00 | | jasmine | 690.00 | 247.50 | | matcha | 307.50 | 90.00 | | oolong | 261.00 | 90.00 | | tea sampler | 212.50 | 153.00 | | ceramic teapot | 146.25 | 78.00 | | golden teaspoon | 335.00 | 90.00 | +-------------------+--------+--------+ 10 rows in set (0.00 sec)

Next, calculate the total revenue from in-store and online sales using the SUM function and several mathematical operators:

  1. SELECT SUM(online_units * product_retail) +
  2. SUM(store_units * product_retail)
  3. AS total_sales
  4. FROM product_information;
+-------------+ | total_sales | +-------------+ | 5706.75 | +-------------+ 1 row in set (0.00 sec)

Performing these queries is important for two reasons. The first reason is so that the tea shop owners can evaluate which items are best-sellers and prioritize those products when purchasing more in the future. Second, they can analyze how well the tea shop performed overall with products sales in-store and online.

Next, you’ll find the profit margin for each product. The profit margin for a given product is the amount of revenue that the business gains for each unit of that product that it sells. To understand how much revenue you earned, you can multiply the sales by the profit margin.

To calculate the profit margin for your individual products, subtract product_cost from product_retail for each row. Then divide this value by the product retail to calculate the profit margin percentage:

  1. SELECT product_name,
  2. (product_retail - product_cost) / product_retail
  3. AS profit_margin
  4. FROM product_information;
+-------------------+-------------+ | product_name | profit_margin | +-------------------+-------------+ | chamomile | 0.317333 | | chai | 0.177778 | | lavender | 0.317333 | | english_breakfast | 0.317333 | | jasmine | 0.177333 | | matcha | 0.177333 | | oolong | 0.177778 | | tea sampler | 0.294118 | | ceramic teapot | 0.282051 | | golden teaspoon | 0.600000 | +-------------------+-------------+ 10 rows in set (0.00 sec)

Based on this output, you’ll learn that the product with the highest profit margin is the golden teaspoon at 60%, and the lowest is for the Chai, Jasmine, Matcha, and Oolong teas at 18%. For the golden teaspoon, this means at a retail value of $5.00 with a profit margin of 60%, you create $3.00 in revenue.

You could also use the aggregate function AVG to calculate the average profit margin for all of the tea shop’s products. This average serves as a benchmark for the tea shop owners to then identify what products fall below that number and strategize how to improve:

  1. SELECT AVG((product_retail - product_cost) / product_retail)
  2. AS avg_profit_margin
  3. FROM product_information;
+-------------------+ | avg_profit_margin | +-------------------+ | 0.2838391151 | +-------------------+ 1 row in set (0.00 sec)

From this calculation, you can conclude that the mean profit margin for products at this tea shop is 28%.

With this new information, imagine that the tea shop owners want to increase the profit margin to 31% in the next quarter for any products that currently have a profit margin less than 27%. In order to do this, you’ll subtract your target profit margin from 1 (1 - 0.31) and then divide each of the returned products’ costs by this value. The result will be the new price that the product must sell for at retail to achieve a 31% profit margin:

  1. SELECT product_name, product_cost / (1 - 0.31)
  2. AS new_retail
  3. FROM product_information
  4. WHERE (product_retail - product_cost) / product_retail < 0.27;
+--------------+------------+ | product_name | new_retail | +--------------+------------+ | chai | 10.724638 | | jasmine | 8.942029 | | matcha | 8.942029 | | oolong | 10.724638 | +--------------+------------+ 4 rows in set (0.00 sec)

These results display the new retail prices necessary for under-performing products to achieve a 31% profit margin. Data analysis such as this equips the tea shop owners with the ability to make decisive business decisions about how to improve their revenue for the next quarter and understand what to aim for.


Using mathematical expressions in SQL can range from solving arithmetic problems like you would on a calculator, to performing complex analyses on real-world data that may influence business decisions. If you can recognize the primary mathematical operators and rules for orders of operations, the calculation possibilities are endless. And when you want to analyze your data even further, using these operators with aggregate functions can calculate answers to your “what if” questions that may help with strategic planning. Learn more about what you can do with databases in SQL with our series on How To Use SQL.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

Tutorial Series: How To Use SQL

Series Description

Structured Query Language — commonly known as SQL — is a language used to define, control, manipulate, and query data held in a relational database. SQL has been widely adopted since it was first developed in the 1970s, and today it’s the predominant language used to manage relational database management systems.

Ideal for managing structured data (data that can fit neatly into an existing data model), SQL is an essential tool for developers and system administrators in a wide variety of contexts. Also, because of its maturity and prevalence, candidates with SQL experience are highly sought after for jobs across a number of industries.

This series is intended to help you get started with using SQL. It includes a mix of conceptual articles and tutorials which provide introductions to various SQL concepts and practices. You can also use the entries in this series for reference while you continue to hone your skills with SQL.

Note: Please be aware that the tutorials in this series use MySQL in examples, but many RDBMSs use their own unique implementations of SQL. Although the commands outlined in this tutorial will work on most RDBMSs, the exact syntax or output may differ if you test them on a system other than MySQL.

About the authors
Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel