Adventures in Machine Learning

Efficiently Multiply Values in SQL Databases with JOIN Statements

Multiplying Values in SQL: How to Combine Data Tables for Efficient Calculations

Have you ever found yourself in need of multiplying two columns of data in a database? This is a common task in businesses that require calculating various financial transactions or production metrics.

In this article, we will explore ways of multiplying values in an SQL database, whether from a single or multiple tables.

Using the Multiplication Operator in SQL

SQL, or Structured Query Language, is a standard language used in databases to edit and retrieve information. The multiplication operator in SQL is represented by the asterisk sign (*), just like you would in a calculator.

To multiply values in a single table, you will need to use this operator within a SELECT statement along with the desired columns. For example, imagine you have a table called ‘prices’ with the following columns: ‘Item’, ‘Price’, and ‘Quantity’.

We can create a new column called ‘Total’ by multiplying the Price and Quantity columns using the following SQL query:

SELECT Item, Price, Quantity, (Price * Quantity) AS Total

FROM prices;

The AS keyword is used in the query to rename the output column from the multiplication operation. The resulting table will have an additional column ‘Total,’ which displays the product of the Price and Quantity columns.

This method can be applied to any table with columns that require multiplication.

Multiplying Values from Different Joined Tables

Sometimes, you may need to combine information from different tables to perform calculations. For instance, let’s consider two tables: ‘Purchases’ and ‘Discounts.’ Suppose your business provides discounts to customers based on their purchase history.

Your goal is to calculate the total price of a purchase with discounts factored in. The Purchases table contains the following columns: ‘ID’, ‘CustomerName’, ‘Item’, ‘Quantity,’ and ‘Price.’ Meanwhile, the Discounts table includes: ‘ID’, ‘CustomerName,’ and ‘PercentDiscount.’

To get the total price with discounts, we will need to combine these tables using a JOIN statement, then multiply the discounted price by the quantity of items purchased.

Here is the SQL query that achieves this:

SELECT purchases.ID, purchases.CustomerName, purchases.Item,


(purchases.Price – purchases.Price * (discounts.PercentDiscount/100))

* purchases.Quantity AS TotalPrice

FROM purchases

LEFT JOIN discounts ON purchases.CustomerName = discounts.CustomerName;

In this query, we used a LEFT JOIN clause between the Purchases and Discounts tables based on the matching ‘CustomerName’ column. We calculated the discounted price by subtracting the percentage discount from the original price, then multiplying this value by the quantity purchased.

Finally, we used the asterisk (*) operator to achieve the multiplication with the Quantity column.

Example Database with Purchase and Discount Tables

Suppose you want to create a database to store information about purchases and discounts in your business. We can create two tables: ‘Purchases’ and ‘Discounts,’ as follows:

Purchases Table:

– ID (int, primary key)

– CustomerName (varchar(255))

– Item (varchar(255))

– Quantity (int)

– Price (float)

Discounts Table:

– ID (int, primary key)

– CustomerName (varchar(255))

– PercentDiscount (float)

In the Purchases table, we have columns for an ID number, customer name, the item purchased, the quantity, and the price per unit.

The ID column represents a unique primary key for each purchase, while the CustomerName column links this table to information in the Discounts table. The Discounts table includes an ID and CustomerName column for identification and a PercentDiscount column for storing the percentage discount that customers receive.

Description of Purchase Table Columns

The Purchases table’s ID column contains an integer value that uniquely identifies each purchase. This value auto-increments with each new purchase, meaning that it increases by 1 for every record added.

The CustomerName column stores a string value representing the customer’s name. The Item column stores a string value representing the purchased item’s name.

The Quantity column stores an integer value representing the quantity of items purchased. Finally, the Price column stores a decimal value representing the price per unit of an item.

Description of Discount Table Columns

The Discounts table’s ID column serves the same purpose as the Purchases table’s ID column. The CustomerName column stores a string value representing the customer’s name, which links to the Purchases table for applying the correct discounts.

The PercentDiscount column stores a decimal value representing the percentage discount offered to each customer based on their purchase history.


Multiplying values in an SQL database may seem daunting at first, but it is a fundamental operation that can streamline data processing in many industries. Whether you need to perform calculations within one table or combine data from multiple tables, SQL provides effective tools to achieve your goals.

By following the examples and explanations provided above, you should have a good foundation for multiplying values in an SQL database.

3) Multiplying Values to Determine Total Price

When working with databases, multiplications are often necessary to calculate the total values of transactions, orders, and more. The most common multiplication involves calculating the total price based on a product’s quantity and price.

In this section, we will explore how to calculate the total price in a purchase table.

Multiplying Price and Quantity Columns in Purchase Table

Consider a purchase table that contains various columns such as ‘Product,’ ‘Price,’ ‘Quantity,’ and ‘Total Price.’ We can populate the ‘Total Price’ column by multiplying the corresponding ‘Price’ and ‘Quantity’ columns. We can use the following SQL query to accomplish this:

SELECT Product, Price, Quantity, (Price * Quantity) AS ‘Total Price’

FROM PurchaseTable;

In this query, we selected the columns ‘Product,’ ‘Price,’ and ‘Quantity’ from the PurchaseTable and created a new column that multiplies the ‘Price’ and ‘Quantity’ columns.

We also used the ‘AS’ keyword to give the calculated column an alias of ‘Total Price.’ By doing so, we can easily identify the calculated column in the resulting output.

Giving the Result an Alias

The AS keyword is used to give an alias or renaming a column in the SQL query. Giving an alias can make the resulting table easier to read and interpret.

In the case of the ‘Total Price’ column calculated in the previous example, we can rename it to something more concise by using the AS keyword. Consider the following SQL query:

SELECT Product, Price, Quantity, (Price * Quantity) AS ‘TotalPrice’

FROM PurchaseTable;

In this query, we have used the AS keyword to rename the ‘Total Price’ column to ‘TotalPrice,’ making the name more readable.

The alias can be something as simple as no spaces in the column name.

4) Using JOIN to Multiply Values from Different Tables

Often in business, it is necessary to pull data from different tables and then join the data to get a complete picture. The JOIN keyword is utilized in SQL to join two or more tables, and we can use it to multiply values to determine a total price.

Joining Tables in SQL

Let’s consider two tables: ‘Orders’ and ‘Products.’ The Orders table contains columns for order ID, product ID, and quantity ordered. The Products table contains columns for product ID, product name, and price.

We want to join these two tables and obtain a table with the product information along with the total price. We can use the JOIN keyword to link these two tables together on the common ‘product ID.’ In this example, we will use INNER JOIN, which includes only the rows in both tables that satisfy the specified conditions.

Consider the following SQL query:

SELECT Orders.Order_ID, Products.Product_Name, Orders.Quantity, Products.Price

FROM Orders


ON Orders.Product_ID=Products.Product_ID;

In this query command, we select the columns ‘Order_ID,’ ‘Product_Name,’ ‘Quantity,’ and ‘Price’ from the Orders and Products tables while also joining them using the INNER JOIN keyword and matching the ‘Product_ID’ between the tables.

Selecting Columns from Multiple Joined Tables

Once we have joined multiple tables, we may want to select specific columns for more efficient querying. This can be done by explicitly specifying which table a column belongs to.

Consider the example above with the inner join between ‘Orders’ and ‘Products’ tables. Suppose we want to display the total price for each order, which involves multiplying quantity and price.

We can achieve this by referencing the columns from both tables in a new SELECT statement as follows:

SELECT Orders.Order_ID, Products.Product_Name, Orders.Quantity, Products.Price, (Orders.Quantity * Products.Price) AS ‘Total_Price’

FROM Orders


ON Orders.Product_ID = Products.Product_ID;

In this example, we have added a column calculated by multiplying the product’s quantity and price and renaming the resulting column to ‘Total_Price.’ This calculated column will appear in the resulting output next to the other selected columns.

Multiplying Values from Joined Tables

Following the previous example, we can see that finding the total price involves calculating the total for each order. To carry out this calculation, we use the multiplication operation between the ‘Quantity’ and ‘Price’ columns of the ‘Orders’ and ‘Products’ tables, respectively.

By simply multiplying the two columns, we obtain the total price for each unit of product ordered. We can join these two columns to get the entire table’s total price by using the AS keyword to rename the new column created by the multiplication operation.


Calculating the total price in a purchase table and multiplying values in joined tables are two fundamental operations in SQL databases. By following the examples and explanations provided, you should have a solid foundation for multiplying values and joining tables in SQL.

By employing the right techniques, you can efficiently perform complex calculations that make data interpretation more manageable. Multiplying values in SQL databases is an essential operation in various industries to calculate financial transactions and production metrics.

In this article, we explored how to perform this operation within a single table, giving an alias to the result and using the JOIN keyword to multiply values from different joined tables. By utilizing these techniques, we can streamline complex calculations to make data interpretations more manageable.

Takeaways from this article include practical examples, such as calculating total price, using aliases, and selecting columns from different tables. Overall, this article emphasizes the importance of understanding how to multiply values in SQL databases to carry out efficient data processing.