Skip to main content

Command Palette

Search for a command to run...

Operators

Updated
•6 min read•View as Markdown

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:

  1. Arithmetic Operators

  2. Comparison / Relational Operators

  3. Logical Operators

  4. String / Concatenation Operators

  5. Special Operators

  6. Subquery Operators


1️⃣ Arithmetic Operators

Purpose: Perform mathematical calculations on numeric data.

OperatorMeaningExample
+Additionsalary + 1000
-Subtractionsalary - 500
*Multiplicationsalary * 2
/Divisionsalary / 2
%Modulusemployee_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.

OperatorMeaningExample
=Equal tosalary = 5000
!= / <>Not equaldepartment_id <> 2
>Greater thansalary > 4000
<Less thansalary < 6000
>=Greater than or equalsalary >= 5000
<=Less than or equalsalary <= 6000
BETWEEN ... AND ...Within a rangesalary BETWEEN 3000 AND 7000
INMatches a listdepartment_id IN (1,2,3)
NOT INDoes not match a listdepartment_id NOT IN (4,5)
LIKEPattern matchfirst_name LIKE 'J%'
NOT LIKEPattern not matchlast_name NOT LIKE '%son'
IS NULLCheck for NULLmanager_id IS NULL
IS NOT NULLCheck for NOT NULLmanager_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.

OperatorMeaningExample
ANDBoth conditions truesalary > 5000 AND department_id = 2
ORAt least one truedepartment_id = 1 OR department_id = 2
NOTNegates a conditionNOT department_id = 3
ANY / SOMECompare with any value from subquerysalary > ANY (SELECT salary FROM employees)
ALLCompare with all values from subquerysalary > ALL (SELECT salary FROM employees)
EXISTSCheck if subquery returns rowsEXISTS (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 / FunctionMeaningExample
CONCAT()Combine stringsCONCAT(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.

OperatorDescriptionExample
BETWEEN ... AND ...Range inclusivesalary BETWEEN 3000 AND 7000
NOT BETWEENExclude rangesalary NOT BETWEEN 3000 AND 7000
IN / NOT INMatches / excludes listdepartment_id IN (1,2,3)
LIKE / NOT LIKEPattern matchfirst_name LIKE 'J%'
IS NULL / IS NOT NULLCheck NULLmanager_id IS NULL
EXISTSChecks subqueryEXISTS (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.

OperatorDescriptionExample
=Equal to subquery resultsalary = (SELECT MAX(salary) FROM employees)
!= / <>Not equaldepartment_id <> (SELECT department_id FROM departments WHERE location_id=100)
IN / NOT INMatch multiple subquery valuesdepartment_id IN (SELECT department_id FROM departments)
ANY / SOMECompare with any subquery valuesalary > ANY (SELECT salary FROM employees WHERE department_id=2)
ALLCompare with all subquery valuessalary > ALL (SELECT salary FROM employees WHERE department_id=2)
EXISTSChecks if subquery returns rowsEXISTS (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:

  1. Arithmetic – +, -, *, /, %

  2. Comparison / Relational – =, !=, <, >, <=, >=, BETWEEN, IN, LIKE, IS NULL

  3. Logical – AND, OR, NOT, ANY, ALL, EXISTS

  4. String / Concatenation – CONCAT(), ||, +

  5. Special – BETWEEN, NOT BETWEEN, IN, NOT IN, LIKE, NOT LIKE, IS NULL, EXISTS

  6. Subquery – =, !=, IN, NOT IN, ANY, ALL, EXISTS