SQL operators are used to perform mathematical, logical, and relational operations on data in SQL queries. In this comprehensive guide, we will cover the 6 different types of SQL operators and provide 45 code examples to help you understand how to use them.
The 6 types of SQL operators are:
We will also discuss the following topics:
By the end of this guide, you will have a good understanding of SQL operators and be able to use them to write powerful and efficient SQL queries.
SQL operators are special words or symbols that are used to perform tasks on data in SQL queries. These tasks can be anything from complex comparisons to basic arithmetic operations.
There are six main types of SQL operators:
Arithmetic operators are used to perform basic mathematical operations on data. The following table shows the most common arithmetic operators in SQL:
Operator | Description | Example |
---|---|---|
+ | Addition | SELECT 1 + 2; |
- | Subtraction | SELECT 3 - 1; |
* | Multiplication | SELECT 4 * 5; |
/ | Division | SELECT 6 / 3; |
% | Modulus (remainder) | SELECT 7 % 3; |
Bitwise operators are used to perform operations on individual bits of data. The following table shows the most common bitwise operators in SQL:
Operator | Description | Example |
---|---|---|
& | Bitwise AND | SELECT 1 & 2; |
Bitwise OR | ||
^ | Bitwise XOR | SELECT 1 ^ 2; |
~ | Bitwise NOT | SELECT ~1; |
<< | Bitwise left shift | SELECT 1 << 2; |
>> | Bitwise right shift | SELECT 1 >> 2; |
Comparison operators are used to compare two values. The following table shows the most common comparison operators in SQL:
Operator | Description | Example |
---|---|---|
= | Equal to | SELECT * FROM customers WHERE customer_id = 1; |
<> | Not equal to | SELECT * FROM customers WHERE customer_id <> 1; |
< | Less than | SELECT * FROM customers WHERE customer_id < 10; |
> | Greater than | SELECT * FROM customers WHERE customer_id > 10; |
<= | Less than or equal to | SELECT * FROM customers WHERE customer_id <= 10; |
>= | Greater than or equal to | SELECT * FROM customers WHERE customer_id >= 10; |
IS NULL | Is null | SELECT * FROM customers WHERE customer_name IS NULL; |
IS NOT NULL | Is not null | SELECT * FROM customers WHERE customer_name IS NOT NULL; |
Compound operators are a combination of two or more operators. The following table shows the most common compound operators in SQL:
Operator | Description | Example |
---|---|---|
+= | Addition assignment | SELECT customer_id += 1; |
-= | Subtraction assignment | SELECT customer_id -= 1; |
*= | Multiplication assignment | SELECT customer_id *= 2; |
/= | Division assignment | SELECT customer_id /= 2; |
%= | Modulus assignment | SELECT customer_id %= 3; |
Logical operators are used to combine two or more conditions. The following table shows the most common logical operators in SQL:
Operator | Description | Example |
---|---|---|
AND | Both conditions must be true | SELECT * FROM customers WHERE customer_id = 1 AND customer_name = 'John Doe'; |
OR | Either condition can be true | SELECT * FROM customers WHERE customer_id = 1 OR customer_name = 'John Doe'; |
NOT | Negates the condition | SELECT * FROM customers WHERE customer_id <> 1 AND customer_name <> 'John Doe'; |
String operators are used to perform operations on strings. The following table shows the most common string operators in SQL:
Operator | Description | Example |
---|---|---|
Concatenation | ||
LIKE | Pattern matching | SELECT * FROM customers WHERE customer_name LIKE '%Doe'; |
NOT LIKE | Negative pattern matching | SELECT * FROM customers WHERE customer_name NOT LIKE '%Doe'; |
Here are some code examples of how to use SQL operators:
-- Addition
SELECT 1 + 2;
-- Output: 3
-- Subtraction
SELECT 3 -
Here are some more code examples of how to use SQL operators:
-- Multiplication
SELECT 4 * 5;
-- Output: 20
-- Division
SELECT 6 / 3;
-- Output: 2
-- Modulus (remainder)
SELECT 7 % 3;
-- Output: 1
-- Bitwise AND
SELECT 1 & 2;
-- Output: 0
-- Bitwise OR
SELECT 1 | 2;
-- Output: 3
-- Bitwise XOR
SELECT 1 ^ 2;
-- Output: 3
-- Bitwise NOT
SELECT ~1;
-- Output: -2
-- Bitwise left shift
SELECT 1 << 2;
-- Output: 4
-- Bitwise right shift
SELECT 1 >> 2;
-- Output: 0
-- Equal to
SELECT * FROM customers WHERE customer_id = 1;
-- Output: All customers with the customer ID of 1
-- Not equal to
SELECT * FROM customers WHERE customer_id <> 1;
-- Output: All customers other than those with the customer ID of 1
-- Less than
SELECT * FROM customers WHERE customer_id < 10;
-- Output: All customers with a customer ID less than 10
-- Greater than
SELECT * FROM customers WHERE customer_id > 10;
-- Output: All customers with a customer ID greater than 10
-- Less than or equal to
SELECT * FROM customers WHERE customer_id <= 10;
-- Output: All customers with a customer ID less than or equal to 10
-- Greater than or equal to
SELECT * FROM customers WHERE customer_id >= 10;
-- Output: All customers with a customer ID greater than or equal to 10
-- Is null
SELECT * FROM customers WHERE customer_name IS NULL;
-- Output: All customers with a null customer name
-- Is not null
SELECT * FROM customers WHERE customer_name IS NOT NULL;
-- Output: All customers with a non-null customer name
-- Addition assignment
SELECT customer_id += 1;
-- Output: Increases the customer ID by 1
-- Subtraction assignment
SELECT customer_id -= 1;
-- Output: Decreases the customer ID by 1
-- Multiplication assignment
SELECT customer_id *= 2;
-- Output: Multiplies the customer ID by 2
-- Division assignment
SELECT customer_id /= 2;
-- Output: Divides the customer ID by 2
-- Modulus assignment
SELECT customer_id %= 3;
-- Output: Sets the customer ID to the remainder of the division by 3
-- AND
SELECT * FROM customers WHERE customer_id = 1 AND customer_name = 'John Doe';
-- Output: All customers with the customer ID of 1 and the name John Doe
-- OR
SELECT * FROM customers WHERE customer_id = 1 OR customer_name = 'John Doe';
-- Output: All customers with the customer ID of 1 or the name John Doe
-- NOT
SELECT * FROM customers WHERE customer_id <> 1 AND customer_name <> 'John Doe';
-- Output: All customers other than those with the customer ID of 1 and the name John Doe
-- Concatenation
SELECT 'John' || ' Doe';
-- Output: 'John Doe'
-- Pattern matching
SELECT * FROM customers WHERE customer_name LIKE '%Doe';
-- Output: All customers with the name Doe in their name
-- Negative pattern matching
SELECT * FROM customers WHERE customer_name NOT LIKE '%Doe';
-- Output: All customers with the name Doe not in their name
SQL operators are a powerful tool for manipulating data in SQL queries. By understanding how to use SQL operators, you can write more complex and efficient queries.