Examples of SQL Server Arithmetic Operators
CREATE TABLE Sales (
OrderID INT,
Product VARCHAR(50),
Quantity INT,
UnitPrice DECIMAL(10, 2)
);
INSERT INTO Sales (OrderID,Product, Quantity, UnitPrice);
VALUES
(1, 'Laptop', 2, 1000.00),
(2, 'Smartphone', 3, 700.00),
(3, 'Headphones', 5, 50.00);
Output:
Example of Addition Operators
SELECT OrderID, Quantity + 1 AS Increased_Quantity
FROM Sales;
Output:
Explanation: This query retrieves the “OrderID” and increases the “Quantity” of each order by 1. The result set shows the order ID alongside the incremented quantity for each sale item.
Example of Subtraction Operators
SELECT OrderID, UnitPrice - 50 AS Reduced_Price
FROM Sales;
Output:
Explanation: This query will decrease the unit price of each product by 50.
Example of Multiplication Operators
SELECT OrderID, Quantity * UnitPrice AS Total_Price
FROM Sales;
Output:
Explanation: This query will calculate the total price for each order by multiplying the quantity with the unit price.
Example of Division Operators
SELECT OrderID, UnitPrice / Quantity AS Price_Per_Unit
FROM Sales;
Output:
Explanation: This query will calculate the price per unit for each product by dividing the unit price by the quantity.
Example of Modulus Operators
SELECT OrderID, Quantity % 2 AS Remainder,FROM Sales;
Output:
Explanation: This query will calculate the remainder when dividing the quantity of each product by 2.
SQL Server Arithmetic Operators
Microsoft’s SQL Server, a capable relational database management system, has a lot of features to deal with data and make manipulations purely. SQL Server, in addition to the many features available to users, has a set of operators for arithmetic which allows users of databases to perform mathematical operations on data stored within tables.
In this article, SQL Server arithmetic operators will be explored, including how to use them, their syntax, and the context of practical examples.
Contact Us