💻 Introduction
In SQL, operators are special symbols or keywords used to perform operations on values. They are widely used in queries to filter, compare, and calculate results. In this guide, we’ll explore the main categories of SQL operators:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Before proceeding, make sure you’re familiar with Introduction to SQL and SQL Data Types.
1️⃣ Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator | Description | Example |
---|---|---|
+ | Addition | SELECT 10 + 5; |
– | Subtraction | SELECT 10 – 5; |
* | Multiplication | SELECT 10 * 5; |
/ | Division | SELECT 10 / 5; |
% | Modulus (remainder) | SELECT 10 % 3; |
SELECT EmpID, Salary, Salary + 1000 AS RevisedSalary
FROM Employees;
2️⃣ Comparison Operators
Comparison operators are used to compare two values and return TRUE or FALSE.
Operator | Description | Example |
---|---|---|
= | Equal to | SELECT * FROM Employees WHERE Salary = 50000; |
<> or != | Not equal to | SELECT * FROM Employees WHERE DeptID <> 10; |
> | Greater than | SELECT * FROM Employees WHERE Salary > 50000; |
< | Less than | SELECT * FROM Employees WHERE Salary < 50000; |
>= | Greater than or equal to | SELECT * FROM Employees WHERE Salary >= 50000; |
<= | Less than or equal to | SELECT * FROM Employees WHERE Salary <= 50000; |
3️⃣ Logical Operators
Logical operators are used to combine multiple conditions in SQL queries.
Operator | Description | Example |
---|---|---|
AND | Returns TRUE if all conditions are TRUE | WHERE Salary > 50000 AND DeptID = 2 |
OR | Returns TRUE if any condition is TRUE | WHERE DeptID = 2 OR DeptID = 3 |
NOT | Reverses the condition | WHERE NOT DeptID = 2 |
SELECT * FROM Employees
WHERE Salary > 40000 AND Department = 'IT';
4️⃣ Bitwise Operators (in SQL Server)
Bitwise operators perform operations on binary representations of numbers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | SELECT 5 & 3; — Result: 1 |
| | Bitwise OR | SELECT 5 | 3; — Result: 7 |
^ | Bitwise XOR | SELECT 5 ^ 3; — Result: 6 |
~ | Bitwise NOT | SELECT ~5; — Result: -6 |
<< | Bitwise Left Shift | SELECT 5 << 1; — Result: 10 |
>> | Bitwise Right Shift | SELECT 5 >> 1; — Result: 2 |
✅ Key Takeaways
- Arithmetic operators (+, -, *, /, %) perform calculations
- Comparison operators (=, <, >, <=, >=, <>) filter records
- Logical operators (AND, OR, NOT) combine conditions
- Bitwise operators (&, |, ^, ~, <<, >>) are available in SQL Server
- Operators make SQL queries more powerful and flexible
📚 Continue Learning
💬 Got a question about SQL Operators? Drop it in the comments below and let’s discuss!