SQL Operators – Understanding SQL Operators with Examples and Syntax

Welcome to our comprehensive guide on SQL (Structured Query Language) operators! In this blog post, we’re going to break down the various operators used in SQL, their syntax, and how to use them effectively.

You will also look at some examples, working with a hypothetical data set to give you a hands-on understanding. Let’s dive in!

What Are SQL Operators?

Operators in SQL are special reserved words used primarily in WHERE clause to perform operation(s) such as comparisons and arithmetic operations. These are the tools that allow us to specify the criteria you’re interested in – the records you want to include, exclude, combine, or compare.

There are several types of operators in SQL:

  1. Arithmetic operators
  2. Comparison operators
  3. Logical operators

Now, let’s take a closer look at each of these types of operators with the help of example data Products.

For instance, consider a Products table with the following data:

ProductIDPriceQuantityCost
1501025
2302017
3701540

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. They include + (addition), – (subtraction), * (multiplication), / (division), and % (modulus).

Let’s have a closer look at arithmetic operators. They’re used to perform mathematical operations:

OperatorDescriptionExample
+AdditionSELECT *, Cost + 5 as New_Cost FROM Products;
–SubtractionSELECT *, Price - Cost as Proft FROM Products;
*MultiplicationSELECT *, Price * Quantity as Revenue FROM Products;
/DivisionSELECT *, Cost / Price as Cost_Price_Ratio FROM Products;
%ModulusSELECT *, Price % Cost as Price_Cost_Modulus FROM Products;

Comparison Operators

Comparison operators are used to compare one expression with another. The result of a comparison operation is always a Boolean value – true, false, or unknown. They include = (equal), <> (not equal), > (greater than), < (less than), >= (greater than or equal), and <= (less than or equal).

OperatorDescriptionExample
=Equal toSELECT * FROM Products WHERE Price = 50;
<> or !=Not equal toSELECT * FROM Products WHERE Price <> 50;
>Greater thanSELECT * FROM Products WHERE Price > 50;
<Less thanSELECT * FROM Products WHERE Price < 50;
>=Greater than or equal toSELECT * FROM Products WHERE Price >= 50;
<=Less than or equal toSELECT * FROM Products WHERE Price <= 50;

Logical Operators

Logical operators are used to combine two or more conditions. They include AND, OR, NOT, IN, BETWEEN, LIKE, EXISTS, ALL, ANY, and SOME.

OperatorDescriptionExample
ANDTrue if all conditions are trueSELECT * FROM Products WHERE Price > 20 AND Quantity < 10;
ORTrue if at least one condition is trueSELECT * FROM Products WHERE Price < 20 OR Quantity > 10;
NOTTrue if the condition is falseSELECT * FROM Products WHERE NOT Price = 30;
INTrue if the value is in the listSELECT * FROM Products WHERE ProductID IN (1,2,3);
BETWEENTrue if the value is within the rangeSELECT * FROM Products WHERE Price BETWEEN 20 AND 30;
LIKETrue if the value matches the patternSELECT * FROM Products WHERE ProductName LIKE 'A%';
EXISTSTrue if the subquery returns at least one recordSELECT * FROM Products WHERE EXISTS (SELECT ProductID FROM Inventory WHERE Products.ProductID = Inventory.ProductID);

Conclusion

SQL operators are fundamental elements of SQL. They allow us to manipulate data, perform calculations, make comparisons, and establish logical conditions. The above examples should provide a solid basis for their usage. Always remember to use the right operator for your specific use case.