Operators in SQL

In SQL, operators are symbols or keywords used to perform operations on data, such as comparisons, arithmetic, logical checks, and pattern matching. They are used mainly in queries (e.g., inside the WHERE, SELECT, and HAVING clauses).

Here’s a breakdown of the main types of SQL operators:


1. Arithmetic Operators

Used for mathematical calculations.

  • + → Addition
  • - → Subtraction
  • * → Multiplication
  • / → Division
  • % → Modulus (remainder)

👉 Example:

SELECT price, price * 0.1 AS tax FROM products;

2. Comparison Operators

Used to compare values, return TRUE or FALSE.

  • = → Equal to
  • != or <> → Not equal to
  • > → Greater than
  • < → Less than
  • >= → Greater than or equal to
  • <= → Less than or equal to

👉 Example:

SELECT * FROM employees WHERE salary > 50000;

3. Logical Operators

Combine conditions in WHERE clauses.

  • AND → Returns true if both conditions are true
  • OR → Returns true if any condition is true
  • NOT → Negates a condition

👉 Example:

SELECT * FROM employees WHERE department = 'IT' AND salary > 60000;

4. Bitwise Operators (depends on SQL dialect, e.g., SQL Server, MySQL)

  • & → Bitwise AND
  • | → Bitwise OR
  • ^ → Bitwise XOR
  • ~ → Bitwise NOT
  • << → Left shift
  • >> → Right shift

5. Special Operators

  • BETWEEN → Check if value is within a range
  • IN → Check if value matches any in a list
  • LIKE → Pattern matching with wildcards (%, _)
  • IS NULL / IS NOT NULL → Check for null values
  • EXISTS → Check if a subquery returns rows
  • ANY / ALL → Compare value to a set/subquery

👉 Example:

SELECT * FROM products WHERE price BETWEEN 100 AND 500;
SELECT * FROM customers WHERE city IN ('London', 'Paris');
SELECT * FROM users WHERE name LIKE 'A%';

Leave a Reply