SQL Operators: A Comprehensive Guide with 6 Types and 45 Code Examples

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:

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • String operators
  • Date and time operators
  • Set operators

We will also discuss the following topics:

  • How to use SQL operators to write effective queries
  • How to avoid common mistakes when using SQL operators
  • Best practices for using SQL operators

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
  • Bitwise operators
  • Comparison operators
  • Compound operators
  • Logical operators
  • String operators

Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations on data. The following table shows the most common arithmetic operators in SQL:

OperatorDescriptionExample
+AdditionSELECT 1 + 2;
-SubtractionSELECT 3 - 1;
*MultiplicationSELECT 4 * 5;
/DivisionSELECT 6 / 3;
%Modulus (remainder)SELECT 7 % 3;

Bitwise operators

Bitwise operators are used to perform operations on individual bits of data. The following table shows the most common bitwise operators in SQL:

OperatorDescriptionExample
&Bitwise ANDSELECT 1 & 2;
  Bitwise OR
^Bitwise XORSELECT 1 ^ 2;
~Bitwise NOTSELECT ~1;
<<Bitwise left shiftSELECT 1 << 2;
>>Bitwise right shiftSELECT 1 >> 2;

Comparison operators

Comparison operators are used to compare two values. The following table shows the most common comparison operators in SQL:

OperatorDescriptionExample
=Equal toSELECT * FROM customers WHERE customer_id = 1;
<>Not equal toSELECT * FROM customers WHERE customer_id <> 1;
<Less thanSELECT * FROM customers WHERE customer_id < 10;
>Greater thanSELECT * FROM customers WHERE customer_id > 10;
<=Less than or equal toSELECT * FROM customers WHERE customer_id <= 10;
>=Greater than or equal toSELECT * FROM customers WHERE customer_id >= 10;
IS NULLIs nullSELECT * FROM customers WHERE customer_name IS NULL;
IS NOT NULLIs not nullSELECT * FROM customers WHERE customer_name IS NOT NULL;

Compound operators

Compound operators are a combination of two or more operators. The following table shows the most common compound operators in SQL:

OperatorDescriptionExample
+=Addition assignmentSELECT customer_id += 1;
-=Subtraction assignmentSELECT customer_id -= 1;
*=Multiplication assignmentSELECT customer_id *= 2;
/=Division assignmentSELECT customer_id /= 2;
%=Modulus assignmentSELECT customer_id %= 3;

Logical operators

Logical operators are used to combine two or more conditions. The following table shows the most common logical operators in SQL:

OperatorDescriptionExample
ANDBoth conditions must be trueSELECT * FROM customers WHERE customer_id = 1 AND customer_name = 'John Doe';
OREither condition can be trueSELECT * FROM customers WHERE customer_id = 1 OR customer_name = 'John Doe';
NOTNegates the conditionSELECT * FROM customers WHERE customer_id <> 1 AND customer_name <> 'John Doe';

String operators

String operators are used to perform operations on strings. The following table shows the most common string operators in SQL:

OperatorDescriptionExample
  Concatenation
LIKEPattern matchingSELECT * FROM customers WHERE customer_name LIKE '%Doe';
NOT LIKENegative pattern matchingSELECT * FROM customers WHERE customer_name NOT LIKE '%Doe';

Code examples

Here are some code examples of how to use SQL operators:

Arithmetic 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 operators

-- 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

Comparison operators

-- 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

Compound operators

-- 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

Logical operators

-- 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

String operators

-- 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

Conclusion

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.

#sql 

SQL Operators: A Comprehensive Guide with 6 Types and 45 Code Examples
1.20 GEEK