-
Hajipur, Bihar, 844101
In MySQL, operators are symbols or keywords that perform operations on one or more values. They are essential for filtering, calculating, comparing, and manipulating data in queries. Operators allow you to build complex conditions, perform arithmetic calculations, and control logic in SQL statements.
Understanding MySQL operators is fundamental for writing accurate and efficient queries.
MySQL operators can be broadly categorized into the following types:
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators (like LIKE, IN, BETWEEN, IS NULL)
Arithmetic operators are used to perform mathematical calculations on numeric values.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | SELECT 10 + 5; |
- |
Subtraction | SELECT 10 - 5; |
* |
Multiplication | SELECT 10 * 5; |
/ |
Division | SELECT 10 / 5; |
% |
Modulus | SELECT 10 % 3; |
SELECT name, score, score + 10 AS NewScore
FROM students;
Adds 10 points to each student’s score.
Arithmetic operators can be used in SELECT, UPDATE, and WHERE clauses.
Comparison operators are used to compare two values. They return TRUE or FALSE.
| Operator | Description | Example |
|---|---|---|
= |
Equal | score = 90 |
!= or <> |
Not equal | score != 50 |
> |
Greater than | age > 18 |
< |
Less than | age < 25 |
>= |
Greater than or equal | score >= 60 |
<= |
Less than or equal | score <= 100 |
SELECT name
FROM students
WHERE score >= 90;
Returns students who scored 90 or more.
Comparison operators are often used with WHERE, HAVING, and JOIN conditions.
Logical operators combine multiple conditions to return TRUE or FALSE.
| Operator | Description | Example |
|---|---|---|
AND |
Both conditions must be true | age > 18 AND score >= 50 |
OR |
Either condition can be true | age < 18 OR score < 50 |
NOT |
Negates a condition | NOT(score >= 50) |
SELECT name
FROM students
WHERE age > 18 AND score >= 50;
Returns students who are older than 18 and have score 50 or higher.
Bitwise operators perform operations at the binary level. They are less commonly used but useful in flag fields or settings stored as integers.
| Operator | Description | Example |
|---|---|---|
& |
Bitwise AND | 5 & 3 |
| ` | ` | Bitwise OR |
^ |
Bitwise XOR | 5 ^ 3 |
~ |
Bitwise NOT | ~5 |
<< |
Left shift | 5 << 1 |
>> |
Right shift | 5 >> 1 |
SELECT 5 & 3 AS BitwiseAND;
Returns 1, which is the result of bitwise AND.
Assignment operators are used to assign values to variables or columns.
| Operator | Description | Example |
|---|---|---|
= |
Assign value | SET @score = 90; |
:= |
Assign value | SET @score := 90; |
SET @total = 100;
Assigns the value 100 to the variable @total.
Used for pattern matching in strings.
Wildcards: % for multiple characters, _ for a single character.
SELECT name
FROM students
WHERE name LIKE 'A%';
Returns students whose name starts with 'A'.
Checks if a value exists in a list of values.
SELECT name
FROM students
WHERE id IN (1, 2, 3);
Checks if a value lies within a range.
SELECT name
FROM students
WHERE score BETWEEN 50 AND 90;
Checks for NULL values.
SELECT name
FROM students
WHERE age IS NULL;
MySQL evaluates expressions based on operator precedence:
Arithmetic (* / % + -)
Comparison (=, <>, !=, >, <, >=, <=)
Logical (NOT, AND, OR)
SELECT *
FROM students
WHERE NOT age > 18 AND score >= 50;
MySQL evaluates NOT age > 18 first, then AND score >= 50.
Calculate total marks with bonus:
SELECT name, score + 5 AS TotalScore
FROM students;
Filter students by multiple conditions:
SELECT name
FROM students
WHERE age > 18 AND score >= 60;
Find students whose names start with A or B:
SELECT name
FROM students
WHERE name LIKE 'A%' OR name LIKE 'B%';
Check enrollment in a list of courses:
SELECT name
FROM students
WHERE course_id IN (101, 102, 103);
Find students with scores within a range:
SELECT name
FROM students
WHERE score BETWEEN 50 AND 90;
MySQL operators perform arithmetic, comparison, logical, and bitwise operations.
Arithmetic operators handle mathematical calculations.
Comparison operators compare values and return TRUE/FALSE.
Logical operators combine multiple conditions.
Bitwise operators manipulate binary data.
Assignment operators store values in variables.
Special operators like LIKE, IN, BETWEEN, and IS NULL allow flexible filtering.
Understanding operator precedence ensures accurate query results.
Mastering MySQL operators is essential for writing precise, efficient, and flexible queries in everyday database operations.
Select all students whose score is greater than 80 using a comparison operator.
Select students whose age is between 18 and 25 using the BETWEEN operator.
Select students whose name starts with ‘A’ using the LIKE operator.
Select students whose id is in the list 1, 2, 3, 4 using the IN operator.
Select students whose score is not equal to 50 using the != operator.
Select students who are older than 18 AND have a score above 60 using a logical operator.
Select students who are younger than 18 OR have a score below 50 using a logical operator.
Calculate a new column TotalScore by adding 10 to each student’s score using an arithmetic operator.
Select students where age is NULL using IS NULL.
Use a bitwise AND operator to find students whose flags column matches a specific binary pattern.