SQL operators are fundamental building blocks in crafting queries to retrieve, manipulate, and work with data in databases. These operators allow you to define conditions, perform calculations, and join datasets, among other tasks. In this chapter, we'll delve into various types of SQL operators, ranging from basic to advanced, providing in-depth explanations, examples, and insights to help you fully grasp how they work.
In SQL, operators are used to perform operations on data values within SQL queries. They can be used to:
Understanding how these operators work will allow you to create more powerful and flexible queries, making your interactions with databases more efficient.
There are several categories of SQL operators, each serving different purposes:
Arithmetic operators are used to perform basic mathematical operations, such as addition, subtraction, multiplication, and division.
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulo – returns the remainder)
SELECT salary, salary + 1000 AS new_salary
FROM employees;
salary
for each employee and returns the updated salary as new_salary
.
+--------+------------+
| salary | new_salary |
+--------+------------+
| 50000 | 51000 |
| 60000 | 61000 |
+--------+------------+
Comparison operators are used to compare two values and return a Boolean result (TRUE
, FALSE
, or UNKNOWN
). These operators are often used in WHERE
clauses to filter data.
=
: Equal to!=
or <>
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toBETWEEN
: Between a range of values (inclusive)IN
: Matches any value in a setLIKE
: Pattern matching using wildcardsIS NULL
: Checks if a value is NULL
SELECT name, salary
FROM employees
WHERE salary > 50000;
+--------+--------+
| name | salary |
+--------+--------+
| Bob | 60000 |
| Charlie| 70000 |
+--------+--------+
Logical operators are used to combine multiple conditions in a WHERE
clause. The result of these operations will be either TRUE
, FALSE
, or UNKNOWN
.
AND
: Returns TRUE
if both conditions are true.OR
: Returns TRUE
if either condition is true.NOT
: Reverses the result of a condition.
SELECT name, department
FROM employees
WHERE department = 'HR' AND salary > 50000;
+--------+------------+
| name | department |
+--------+------------+
| Charlie| HR |
+--------+------------+
Bitwise operators operate at the bit level and are used for bitwise manipulation of integer data. They are more common in low-level programming, but they can be useful in SQL for certain types of calculations.
&
(AND)|
(OR)^
(XOR)~
(NOT)<<
(Shift left)>>
(Shift right)
SELECT 5 & 3 AS result;
AND
on the numbers 5 and 3, returning the result of the bitwise operation.
+--------+
| result |
+--------+
| 1 |
+--------+
Set operators are used to combine the results of two or more SELECT
statements. SQL provides the following set operators:
UNION
: Combines two result sets, removing duplicates.UNION ALL
: Combines two result sets, including duplicates.INTERSECT
: Returns rows that are common to both result sets.EXCEPT
: Returns rows from the first result set that are not present in the second result set.
SELECT name FROM employees
UNION
SELECT name FROM contractors;
+--------+
| name |
+--------+
| Alice |
| Bob |
| David |
+--------+
Often, you will combine multiple types of operators in a single query to perform complex operations.
SELECT name, department, salary
FROM employees
WHERE department = 'Finance'
AND salary > 60000
OR department = 'HR';
+--------+------------+--------+
| name | department | salary |
+--------+------------+--------+
| Charlie| HR | 70000 |
| Bob | Finance | 65000 |
+--------+------------+--------+
AND
, OR
), use parentheses to ensure the correct order of operations.NULL
values explicitly when necessary.SQL operators are the backbone of writing efficient queries that interact with data meaningfully. Whether you are performing arithmetic, comparing values, combining conditions, or manipulating sets, understanding SQL operators will help you write more effective and concise queries. Happy coding !❤️