MySQL Operators

MySQL Tutorial

MySQL Operators

In MySQL, operators are symbols or keywords used to perform operations on values or expressions in queries. Operators are essential for calculations, comparisons, logical operations, and string manipulations.

Operators in MySQL can be categorized into five main types:

Arithmetic Operators

Used for mathematical calculations.

OperatorDescriptionExample
+Addition SELECT 5 + 3; → 8
-Subtraction SELECT 5 - 3; → 2
*Multiplication SELECT 5 * 3; → 15
/Division SELECT 10 / 2; → 5
%Modulus (remainder) SELECT 10 % 3; → 1

Example:

Comparison Operators

Used to compare values. Returns TRUE or FALSE.

OperatorDescriptionExample
=Equal tomarks = 90
!= or <>Not equal tomarks != 90
>Greater thanmarks > 80
<Less thanmarks < 80
>=Greater than or equal tomarks >= 80
<=Less than or equal tomarks <= 80
BETWEENBetween a rangemarks BETWEEN 70 AND 90
INValue in a listdept IN ('IT','HR')
LIKEPattern matchingname LIKE 'J%'
IS NULLCheck if value is NULLmarks IS NULL

Example:

Logical Operators

Used to combine multiple conditions.

OperatorDescriptionExample
ANDAll conditions must be TRUEmarks > 80 AND dept='IT'
ORAny condition can be TRUEmarks > 90 OR dept='HR'
NOTNegates the conditionNOT dept='Finance'

Example:

Bitwise Operators

Operate on binary values.

OperatorDescriptionExample
&Bitwise AND 5 & 3 → 1
``Bitwise OR
^Bitwise XOR 5 ^ 3 → 6
~Bitwise NOT ~5 → -6
<<Left shift 5 << 1 → 10
>>Right shift 5 >> 1 → 2

Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assign valueSET @x = 5;
:=Assign value (alternative)SET @y := 10;

 Key Points

  1. Arithmetic → for calculations (+, -, *, /, %)

  2. Comparison → for comparing values (=, !=, <, >, BETWEEN, IN, LIKE)

  3. Logical → combine conditions (AND, OR, NOT)

  4. Bitwise → operate on bits (&, |, ^, ~, <<, >>)

  5. Assignment → assign values (=, :=)

You may also like...