0% completed
In MySQL, operators are symbols used to perform operations on one or more operands.
Following are the types of operators in SQL.
- Arithmetic Operators
- Logical Operators
- Compound Operators
- Comparison Operators
Arithmetic Operators
Arithmetic Operators perform mathematical calculations such as Addition, Subtraction, Multiplication, Module, and Division. The arithmetic operators can be directly used in the SQL statement or combination with column values.
Here are some common arithmetic operators in MySQL:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | SELECT 5 + 3; | 8 |
- | Subtraction | SELECT 10 - 4; | 6 |
* | Multiplication | SELECT 3 * 7; | 21 |
/ | Division | SELECT 20 / 5; | 4 |
% | Modulus (Remainder) | SELECT 17 % 3; | 2 |
Logical Operators
In MySQL, logical operators combine or compare conditions in a WHERE clause of a SQL query. These operators allow you to create more complex conditions by combining multiple conditions.
Here are some logical operators in MySQL:
Operator | Description | Example |
---|---|---|
AND | Logical AND | SELECT * FROM employees WHERE age > 25 AND department = 'Sales'; |
OR | Logical OR | SELECT * FROM employees WHERE age < 25 OR department = 'Marketing'; |
NOT | Logical NOT | SELECT * FROM employees WHERE NOT age = 22; |
Compound Operators
In MySQL, compound operators are shorthand notations that combine an arithmetic or bitwise operation with an assignment. These operators operate and assign the result to a variable in a single step.
Here are some common compound operators in MySQL:
Operator | Description | Example |
---|---|---|
+= | Add and assign | SET x = 5; SET x += 3; |
-= | Subtract and assign | SET y = 10; SET y -= 2; |
*= | Multiply and assign | SET z = 3; SET z *= 4; |
/= | Divide and assign | SET w = 20; SET w /= 2; |
Comparison Operators
Comparison operators in MySQL are used to compare values in various conditions within SQL queries. They allow you to create conditions determining which rows should be included in the result set.
Here are some common comparison operators in MySQL:
Operator | Description | Example |
---|---|---|
= | Equal to | SELECT * FROM products WHERE Price = 500; |
<> | Not equal to | SELECT * FROM products WHERE Price <> 500; |
< | Less than | SELECT * FROM products WHERE Price < 1000; |
> | Greater than | SELECT * FROM products WHERE Price > 1000; |
<= | Less than or equal to | SELECT * FROM products WHERE Price <= 500; |
>= | Greater than or equal to | SELECT * FROM products WHERE Price >= 500; |
Table of Contents
Arithmetic Operators
Logical Operators
Compound Operators
Comparison Operators