SQL Operators

SQL operators are used to perform various operations on data stored in a database. They are used to specify conditions in SQL statements and to manipulate data.

Operators are categorized based on their functionality. Below, we provide tables for each category of SQL operators, including the operator symbol, name, and description. Following each table, we present examples demonstrating the use of each operator.


1. Arithmetic Operators

OperatorNameDescription
+AdditionAdds two numbers.
-SubtractionSubtracts the right operand from the left operand.
*MultiplicationMultiplies two numbers.
/DivisionDivides the left operand by the right operand.
%ModulusReturns the remainder of a division operation.

Examples:

Assume we have a table named Products with the following columns:

</>
Copy
CREATE TABLE Products (
    ProductID INT,
    ProductName VARCHAR(50),
    Price DECIMAL(10, 2),
    Quantity INT
);

Sample data:

</>
Copy
INSERT INTO Products (ProductID, ProductName, Price, Quantity) VALUES
(1, 'Laptop', 800.00, 10),
(2, 'Smartphone', 500.00, 20),
(3, 'Tablet', 300.00, 15);

a) Addition (+): Calculate the total value of all products.

</>
Copy
SELECT ProductName, Price * Quantity AS TotalValue
FROM Products;

b) Subtraction (-): Reduce the price of all products by $50.

</>
Copy
SELECT ProductName, Price - 50 AS DiscountedPrice
FROM Products;

c) Multiplication (*): Calculate the total revenue if all products are sold.

</>
Copy
SELECT SUM(Price * Quantity) AS TotalRevenue
FROM Products;

d) Division (/): Calculate the average price per product.

</>
Copy
SELECT AVG(Price) AS AveragePrice
FROM Products;

e) Modulus (%): Find products with an odd quantity.

</>
Copy
SELECT ProductName
FROM Products
WHERE Quantity % 2 <> 0;

2. Comparison Operators

OperatorNameDescription
=Equal toChecks if the values of two operands are equal.
>Greater thanChecks if the value of the left operand is greater than the right operand.
<Less thanChecks if the value of the left operand is less than the right operand.
>=Greater than or equal toChecks if the value of the left operand is greater than or equal to the right operand.
<=Less than or equal toChecks if the value of the left operand is less than or equal to the right operand.
<>Not equal toChecks if the values of two operands are not equal.

Examples:

Using the same Products table:

a) Equal to (=): Find products priced at $500.

</>
Copy
SELECT ProductName
FROM Products
WHERE Price = 500.00;

b) Greater than (>): Find products priced above $500.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price > 500.00;

c) Less than (<): Find products priced below $500.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price < 500.00;

d) Greater than or equal to (>=): Find products priced at least $500.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price >= 500.00;

e) Less than or equal to (<=): Find products priced at most $500.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price <= 500.00;

f) Not equal to (<>): Find products that are not priced at $500.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price <> 500.00;

3. Logical Operators

OperatorNameDescription
ANDLogical ANDReturns true if both conditions are true.
ORLogical ORReturns true if at least one condition is true.
NOTLogical NOTReverses the result of a condition.

Examples:

a) AND Operator: Find products that are priced above $300 and have a quantity greater than 10.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price > 300 AND Quantity > 10;

b) OR Operator: Find products that are priced above $700 or have a quantity less than 15.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price > 700 OR Quantity < 15;

c) NOT Operator: Find products that are not priced at $800.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE NOT Price = 800.00;

4. Other SQL Operators

OperatorNameDescription
INSet MembershipChecks if a value is present in a list of values.
BETWEENRange ComparisonChecks if a value falls within a specified range.
LIKEPattern MatchingPerforms a search for a specified pattern in a column.
IS NULLNULL CheckChecks if a column has a NULL value.

Examples:

a) IN Operator: Find products with prices of either $300 or $800.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price IN (300.00, 800.00);

b) BETWEEN Operator: Find products priced between $300 and $800.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price BETWEEN 300.00 AND 800.00;

c) LIKE Operator: Find products that start with the letter ‘S’.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE ProductName LIKE 'S%';

d) IS NULL Operator: Find products that have no price specified.

</>
Copy
SELECT ProductName 
FROM Products 
WHERE Price IS NULL;

Conclusion

SQL operators are used to manipulate and filter data within databases. In this tutorial, we covered:

  • Arithmetic Operators: Perform mathematical calculations.
  • Comparison Operators: Compare values.
  • Logical Operators: Combine multiple conditions.
  • Other SQL Operators: Pattern matching, range checking, and NULL value handling.