⥠SQL Operators are special symbols and keywords used to perform operations on data within SQL statements. They are commonly used in SELECT, WHERE, HAVING, JOIN, and UPDATE statements to compare values, combine conditions, perform calculations, and filter records.
đ What are SQL Operators?
An operator tells the database how to evaluate one or more values. For example, you can use operators to find employees whose salary is greater than a specific amount, retrieve students from a particular department, or calculate the total price of an order.
Information
đī¸ Types of SQL Operators
SQL operators are commonly divided into the following categories:
- âī¸ Comparison Operators
- đ Logical Operators
- đ§Ž Arithmetic Operators
- đ Special Operators
- đ Bitwise Operators (supported by some database systems)
âī¸ Comparison Operators
Comparison operators compare two values and return either TRUE, FALSE, or UNKNOWN (when NULL is involved).
| Operator | Description | Example |
|---|---|---|
| = | Equal to | Salary = 50000 |
| <> or != | Not equal to | Department <> 'HR' |
| > | Greater than | Age > 18 |
| < | Less than | Price < 100 |
| >= | Greater than or equal to | Marks >= 90 |
| <= | Less than or equal to | Quantity <= 10 |
Comparison Operator Example
SELECT *
FROM Employees
WHERE Salary >= 50000;đ Logical Operators
Logical operators combine multiple conditions in a query.
| Operator | Description |
|---|---|
| AND | Returns rows only if all conditions are true. |
| OR | Returns rows if at least one condition is true. |
| NOT | Reverses the result of a condition. |
Logical Operator Example
SELECT *
FROM Employees
WHERE Department = 'Sales'
AND Salary > 40000;Using OR
SELECT *
FROM Students
WHERE Department = 'Computer Science'
OR Department = 'Mathematics';Using NOT
SELECT *
FROM Products
WHERE NOT Category = 'Electronics';đ§Ž Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.
| Operator | Description | Example |
|---|---|---|
| + | Addition | Price + Tax |
| - | Subtraction | Salary - Deduction |
| * | Multiplication | Quantity * Price |
| / | Division | Total / Count |
| % | Modulus (supported by many databases) | Age % 2 |
Arithmetic Operator Example
SELECT ProductName,
Quantity,
Price,
Quantity * Price AS TotalAmount
FROM Products;đ Special Operators
SQL provides several special operators for searching, filtering, and testing values.
1ī¸âŖ BETWEEN
Returns values within a specified range (inclusive).
Using BETWEEN
SELECT *
FROM Employees
WHERE Salary BETWEEN 40000 AND 70000;2ī¸âŖ IN
Checks whether a value matches any value in a list.
Using IN
SELECT *
FROM Students
WHERE Department IN ('Computer Science', 'Physics');3ī¸âŖ LIKE
Searches for values matching a pattern using wildcard characters.
Using LIKE
SELECT *
FROM Employees
WHERE FullName LIKE 'A%';Tip
4ī¸âŖ IS NULL
Checks whether a column contains a NULL value.
Using IS NULL
SELECT *
FROM Employees
WHERE Email IS NULL;5ī¸âŖ EXISTS
Returns TRUE if a subquery returns one or more rows.
Using EXISTS
SELECT CustomerName
FROM Customers c
WHERE EXISTS (
SELECT 1
FROM Orders o
WHERE o.CustomerID = c.CustomerID
);đ Bitwise Operators
Some database systems support bitwise operators for manipulating binary values. Availability and syntax may vary depending on the database platform.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR (database-dependent) |
| ~ | Bitwise NOT |
đ Operator Precedence
When multiple operators appear in the same expression, SQL evaluates them according to a precedence order unless parentheses are used.
| Priority | Operator Group |
|---|---|
| 1 (Highest) | Arithmetic operators |
| 2 | Comparison operators |
| 3 | NOT |
| 4 | AND |
| 5 (Lowest) | OR |
Important
đŧ Real-World Example
Using Multiple Operators
SELECT EmployeeID,
FullName,
Salary
FROM Employees
WHERE Department = 'Sales'
AND Salary BETWEEN 40000 AND 70000
ORDER BY Salary DESC;This query retrieves employees from the Sales department whose salaries are between 40000 and 70000, then sorts the results in descending order by salary.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ Operators are used to compare, calculate, and filter data.
- đ Comparison operators evaluate relationships between values.
- đ Logical operators combine multiple conditions.
- đ Arithmetic operators perform mathematical calculations.
- đ Special operators like BETWEEN, IN, LIKE, IS NULL, and EXISTS simplify common filtering tasks.
- đ Parentheses help control the order of evaluation.