MySQL - Division Operator (/)



The division operator (/) in MySQL performs a division operation, where the value on the left side is divided by the value on the right side. The result is returned as a numeric quotient, which could be an integer or a decimal, depending on the data types of the values being divided.

Like addition operator, the division operator can also be used with the SELECT, UPDATE, and DELETE statements in MySQL, along with clauses like WHERE, ORDER BY etc.

Syntax

Following is the syntax of MySQL Multiplication operator −

[SELECT|DELETE|UPDATE] x / y;

Where, "x" and "y" are placeholders for the numeric values you want to divide.

Example

Following is an example of the "/" operator. Here, we are dividing two numbers and displaying the result in the form of a result-set. Look at the following query −

SELECT 54545/55 as Result;

Output

This will produce the following result −

Result
991.7273

Example

We can also have negative values as operands for this operator −

SELECT 62555/-455 as Result;

Output

This will produce the following result −

Result
-137.4835

Example

Let us create a MySQL table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

The following query inserts 7 records into the above created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );

Execute the below query to fetch all the records present in the CUSTOMERS table −

Select * From CUSTOMERS;

Following is the CUSTOMERS table −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Hyderabad 4500.00
7 Muffy 24 Indore 10000.00

Here, we are using the MySQL division operator (/) to calculate the ratio of SALARY to AGE for each person in the CUSTOMERS table −

SELECT NAME, SALARY / AGE AS SALARY_TO_AGE_RATIO
FROM CUSTOMERS;

This will produce the following result −

NAME SALARY_TO_AGE_RATIO
Ramesh 62.500000
Khilan 60.000000
Kaushik 86.956522
Chaitali 260.000000
Hardik 314.814815
Komal 204.545455
Muffy 416.666667

Example

You can also use the division operator with DELETE statement along with WHERE clause and assignment operators.

In this example, let us remove the records of CUSTOMERS whose ratio of SALARY to AGE is less than 250.

DELETE FROM CUSTOMERS
WHERE SALARY / AGE < 250.00000;

Output

Four rows has been deleted from the CUSTOMERS table.

Query OK, 4 rows affected (0.01 sec)

Verification

Execute the below query to verify whether the above records has been deleted or not −

Select * From CUSTOMERS;

As we can see the output, the customers whose ratio of SALARY to AGE is less than 250 has been deleted.

ID NAME AGE ADDRESS SALARY
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
7 Muffy 24 Indore 10000.00

Example

Along with SELECT and DELETE statements, you can also use the division operator with the UPDATE statement.

Let us update the "SALARY" values in the "CUSTOMERS" table by dividing each person's salary by their age −

UPDATE CUSTOMERS
SET SALARY = SALARY / AGE;

Output

This will produce the following result −

Query OK, 3 rows affected, 2 warnings (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 2

Verification

Execute the below query to verify whether the values in SALARY column has updated or not −

Select * From CUSTOMERS;

As we can see the output, the salaries of the customers has been updated.

ID NAME AGE ADDRESS SALARY
4 Chaitali 25 Mumbai 260.00
5 Hardik 27 Bhopal 314.81
7 Muffy 24 Indore 416.67
Advertisements