Operators
Absolutely! Let’s do a full in-depth guide on SQL Operators, categorized by types, with separate explanations and examples using the employees and departments tables. I’ll structure it for clarity and mastery.
🌐 SQL Operators – Types, Uses, and Examples
In SQL, operators are symbols or keywords that perform operations on data values. They are broadly classified into 6 major types:
Arithmetic Operators
Comparison / Relational Operators
Logical Operators
String / Concatenation Operators
Special Operators
Subquery Operators
1️⃣ Arithmetic Operators
Purpose: Perform mathematical calculations on numeric data.
| Operator | Meaning | Example |
+ | Addition | salary + 1000 |
- | Subtraction | salary - 500 |
* | Multiplication | salary * 2 |
/ | Division | salary / 2 |
% | Modulus | employee_id % 2 |
Example – Employees Table
-- Increase salary by 10%
SELECT first_name, salary, salary * 1.10 AS new_salary
FROM employees;
-- Check if employee_id is even or odd
SELECT employee_id, employee_id % 2 AS remainder
FROM employees;
2️⃣ Comparison / Relational Operators
Purpose: Compare two values and return TRUE/FALSE.
| Operator | Meaning | Example |
= | Equal to | salary = 5000 |
!= / <> | Not equal | department_id <> 2 |
> | Greater than | salary > 4000 |
< | Less than | salary < 6000 |
>= | Greater than or equal | salary >= 5000 |
<= | Less than or equal | salary <= 6000 |
BETWEEN ... AND ... | Within a range | salary BETWEEN 3000 AND 7000 |
IN | Matches a list | department_id IN (1,2,3) |
NOT IN | Does not match a list | department_id NOT IN (4,5) |
LIKE | Pattern match | first_name LIKE 'J%' |
NOT LIKE | Pattern not match | last_name NOT LIKE '%son' |
IS NULL | Check for NULL | manager_id IS NULL |
IS NOT NULL | Check for NOT NULL | manager_id IS NOT NULL |
Example – Employees Table
-- Employees with salary between 3000 and 7000
SELECT first_name, salary
FROM employees
WHERE salary BETWEEN 3000 AND 7000;
-- Employees not in department 1 or 2
SELECT first_name, department_id
FROM employees
WHERE department_id NOT IN (1,2);
-- Employees whose last name starts with 'S'
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE 'S%';
3️⃣ Logical Operators
Purpose: Combine multiple conditions.
| Operator | Meaning | Example |
AND | Both conditions true | salary > 5000 AND department_id = 2 |
OR | At least one true | department_id = 1 OR department_id = 2 |
NOT | Negates a condition | NOT department_id = 3 |
ANY / SOME | Compare with any value from subquery | salary > ANY (SELECT salary FROM employees) |
ALL | Compare with all values from subquery | salary > ALL (SELECT salary FROM employees) |
EXISTS | Check if subquery returns rows | EXISTS (SELECT 1 FROM employees WHERE department_id = 2) |
Example – Employees Table
-- Employees in department 1 or 2 with salary > 5000
SELECT first_name, salary, department_id
FROM employees
WHERE (department_id = 1 OR department_id = 2) AND salary > 5000;
-- Employees whose department does NOT exist in dept table
SELECT first_name, department_id
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM departments d WHERE d.department_id = e.department_id);
4️⃣ String / Concatenation Operators
Purpose: Combine or manipulate string values.
| Operator / Function | Meaning | Example |
CONCAT() | Combine strings | CONCAT(first_name, ' ', last_name) |
| ` | ` | |
+ | Combine strings (SQL Server) | first_name + ' ' + last_name |
Example – Employees Table
-- Full name
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
-- Display salary info in sentence
SELECT CONCAT(first_name, ' earns $', salary) AS info
FROM employees;
5️⃣ Special Operators
Purpose: Special conditions like range checks, set membership, pattern matching, NULL handling.
| Operator | Description | Example |
BETWEEN ... AND ... | Range inclusive | salary BETWEEN 3000 AND 7000 |
NOT BETWEEN | Exclude range | salary NOT BETWEEN 3000 AND 7000 |
IN / NOT IN | Matches / excludes list | department_id IN (1,2,3) |
LIKE / NOT LIKE | Pattern match | first_name LIKE 'J%' |
IS NULL / IS NOT NULL | Check NULL | manager_id IS NULL |
EXISTS | Checks subquery | EXISTS (SELECT 1 FROM employees WHERE department_id=2) |
Example – Employees Table
-- Employees NOT between salary 3000 and 7000
SELECT first_name, salary
FROM employees
WHERE salary NOT BETWEEN 3000 AND 7000;
-- Employees with a manager
SELECT first_name, manager_id
FROM employees
WHERE manager_id IS NOT NULL;
6️⃣ Subquery Operators
Purpose: Compare or filter based on subquery results.
| Operator | Description | Example |
= | Equal to subquery result | salary = (SELECT MAX(salary) FROM employees) |
!= / <> | Not equal | department_id <> (SELECT department_id FROM departments WHERE location_id=100) |
IN / NOT IN | Match multiple subquery values | department_id IN (SELECT department_id FROM departments) |
ANY / SOME | Compare with any subquery value | salary > ANY (SELECT salary FROM employees WHERE department_id=2) |
ALL | Compare with all subquery values | salary > ALL (SELECT salary FROM employees WHERE department_id=2) |
EXISTS | Checks if subquery returns rows | EXISTS (SELECT 1 FROM departments WHERE location_id = 100) |
Example – Employees Table
-- Employees with salary greater than any employee in department 2
SELECT first_name, salary
FROM employees
WHERE salary > ANY (SELECT salary FROM employees WHERE department_id = 2);
-- Employees in departments that exist in dept table
SELECT first_name, department_id
FROM employees e
WHERE department_id IN (SELECT department_id FROM departments);
✅ Summary
SQL Operators Types:
Arithmetic –
+,-,*,/,%Comparison / Relational –
=,!=,<,>,<=,>=,BETWEEN,IN,LIKE,IS NULLLogical –
AND,OR,NOT,ANY,ALL,EXISTSString / Concatenation –
CONCAT(),||,+Special –
BETWEEN,NOT BETWEEN,IN,NOT IN,LIKE,NOT LIKE,IS NULL,EXISTSSubquery –
=,!=,IN,NOT IN,ANY,ALL,EXISTS