MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL arithmetic operators

MySQL supports a number of arithmetic operators that you can use in your SQL statements. Here are the basic ones:

  1. Addition (+): Adds two numbers.

    Example: SELECT 5 + 3; //Returns 8

  2. Subtraction (-): Subtracts the second number from the first.

    Example: SELECT 5 - 3; //Returns 2

  3. Multiplication (*): Multiplies two numbers.

    Example: SELECT 5 * 3; //Returns 15

  4. Division (/): Divides the first number by the second. If the division is not exact, the result is a decimal.

    Example: SELECT 10 / 3; //Returns 3.3333

  5. Modulo (% or MOD): Returns the remainder of a division operation.

    Example: SELECT 10 % 3; //Returns 1

    Or: SELECT MOD(10,3); //Returns 1

Arithmetic Operators in a Table:

You can also use these operators in your queries on a table. For example, consider the following products table:

ProductIDPriceQuantity
1105
2203
3157

If you want to calculate the total cost for each product, you can multiply the Price and Quantity columns:

SELECT ProductID, Price, Quantity, Price * Quantity AS TotalCost
FROM products;

This will return:

ProductIDPriceQuantityTotalCost
110550
220360
3157105

The AS keyword is used to rename the calculated column to TotalCost.

NOTE: Be careful when performing division operations, as MySQL performs integer division when both operands are integers. For example, SELECT 5 / 2; will return 2 instead of 2.5. To get a decimal result, make sure at least one of the operands is a decimal, like SELECT 5 / 2.0;.

  1. MySQL arithmetic operators examples:

    • MySQL supports various arithmetic operators for performing mathematical operations in queries.
    SELECT 5 + 3 AS addition, 5 - 3 AS subtraction, 5 * 3 AS multiplication, 5 / 3 AS division, 5 % 3 AS modulus;
    
  2. How to use addition operator in MySQL:

    • The addition operator (+) is used to add two numeric values.
    SELECT column1 + column2 AS sum FROM table_name;
    
  3. Subtraction operator in MySQL queries:

    • The subtraction operator (-) is used to subtract one numeric value from another.
    SELECT column1 - column2 AS difference FROM table_name;
    
  4. Multiplication operator usage in MySQL:

    • The multiplication operator (*) is used to multiply numeric values.
    SELECT column1 * column2 AS product FROM table_name;
    
  5. Division operator in MySQL arithmetic:

    • The division operator (/) is used to divide one numeric value by another.
    SELECT column1 / column2 AS quotient FROM table_name;
    
  6. Modulus operator in MySQL calculations:

    • The modulus operator (%) returns the remainder of the division of one number by another.
    SELECT column1 % column2 AS remainder FROM table_name;
    
  7. Order of operations in MySQL arithmetic expressions:

    • MySQL follows the standard order of operations (PEMDAS/BODMAS: Parentheses/Brackets, Exponents/Orders, Multiplication and Division, Addition and Subtraction).
    SELECT (column1 + column2) * column3 AS result FROM table_name;
    
  8. Performing calculations with MySQL arithmetic operators:

    • You can perform complex calculations using multiple arithmetic operators.
    SELECT (column1 * 2) + (column2 / 3) AS result FROM table_name;
    
  9. Using parentheses in MySQL arithmetic:

    • Parentheses are used to control the order of operations and prioritize calculations.
    SELECT (column1 + column2) * column3 AS result FROM table_name;