✏️ Explanatory Question

What are the different types of operators in MySQL?

👁 10 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

23

What are the different types of operators in MySQL?

Level: Basic to Intermediate — Operators are the building blocks of every WHERE clause and expression.

MySQL operators are symbols or keywords used to perform operations on values in queries. They fall into five main categories.

The Five Operator Categories

  • Arithmetic: +, -, *, /, % (modulo) — for math operations.
  • Comparison: =, != / <>, >, <, >=, <=, <=> — compare two values.
  • Logical: AND, OR, NOT, XOR — combine multiple conditions.
  • Special / Set: BETWEEN, IN, LIKE, IS NULL, EXISTS — pattern & range matching.
  • Bitwise: &, |, ^, ~, <<, >> — operate on bits.
Special operator to remember: <=> is the NULL-safe equality operator. Unlike =, it returns 1 (true) when comparing two NULLs, instead of NULL.

Operator Quick Reference

Category Examples Use Case
Arithmetic + - * / % Calculations
Comparison = != > < >= <= Filtering rows
Logical AND, OR, NOT Combine conditions
Range / Set BETWEEN, IN, LIKE Pattern & range match
Bitwise & | ^ ~ Bit-level operations

Quick Example — Operators in Action

-- Arithmetic
SELECT salary * 12 AS annual_salary FROM employees;

-- Comparison + Logical
SELECT * FROM employees
WHERE salary >= 30000 AND dept_id = 10;

-- Range / Set operators
SELECT * FROM employees WHERE salary BETWEEN 20000 AND 50000;
SELECT * FROM employees WHERE dept_id IN (10, 20, 30);
SELECT * FROM employees WHERE name LIKE 'R%';   -- starts with R

-- NULL-safe comparison
SELECT NULL <=> NULL;   -- returns 1 (true)
SELECT NULL = NULL;     -- returns NULL
Interviewer tip: List the five categories, then highlight the NULL-safe operator <=> and the difference between BETWEEN (inclusive) and range comparisons — these details impress interviewers.