MySQL Operators and Clauses

πŸ”Ή 1.Β Operators in MySQL

(A) Arithmetic Operators

Used for calculations.

SELECT 10 + 5;   -- 15
SELECT 10 - 5;   -- 5
SELECT 10 * 5;   -- 50
SELECT 10 / 5;   -- 2
SELECT 10 % 3;   -- 1 (modulus)

(B) Comparison Operators

Used in WHERE clauses.

=      β†’ Equal
<> or != β†’ Not equal
>      β†’ Greater than
<      β†’ Less than
>=     β†’ Greater or equal
<=     β†’ Less or equal
BETWEEN a AND b
LIKE 'A%'      β†’ Starts with A
IN (x,y,z)     β†’ Matches one of

Example:

SELECT * FROM employees WHERE age BETWEEN 25 AND 40;

(C) Logical Operators

Combine conditions.

AND     -- Both conditions true
OR      -- Either condition true
NOT     -- Negates condition

Example:

SELECT * FROM employees
WHERE age > 30 AND city = 'Delhi';

(D) Bitwise Operators

Operate on bits.

& (AND), | (OR), ^ (XOR), ~ (NOT), << (Left shift), >> (Right shift)

(E) Other Operators

  • IS NULL, IS NOT NULL

  • EXISTS (subquery)

  • ANY, ALL (used with subqueries).


πŸ”Ή 2. Clauses in MySQL

A clause is a keyword in SQL that adds conditions or modifies queries.

(A) WHERE

Filters rows.

SELECT * FROM employees WHERE salary > 50000;

(B) ORDER BY

Sorts results.

SELECT * FROM employees ORDER BY salary DESC;

(C) GROUP BY

Groups rows for aggregate functions (COUNT, SUM, AVG, MAX, MIN).

SELECT department, COUNT(*) 
FROM employees
GROUP BY department;

(D) HAVING

Filters groups (used with GROUP BY).

SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

(E) LIMIT

Restricts number of rows returned.

SELECT * FROM employees LIMIT 5;

(F) DISTINCT

Removes duplicates.

SELECT DISTINCT city FROM employees;

(G) FROM / JOIN

Defines source tables and joins.

SELECT e.name, d.dept_name
FROM employees e
JOIN departments d ON e.dept_id = d.id;

βœ… Quick Summary

  • Operators: Arithmetic (+, -, *, /), Comparison (=, <, >, BETWEEN, LIKE), Logical (AND, OR, NOT).

  • Clauses:

    • WHERE β†’ Filter rows.

    • ORDER BY β†’ Sort.

    • GROUP BY β†’ Aggregate.

    • HAVING β†’ Filter groups.

    • LIMIT β†’ Restrict rows.

    • DISTINCT β†’ Remove duplicates.

Back to blog

Leave a comment