Operators Reference

This section provides a comprehensive reference for the operators supported in Azure SQL Database.

Comparison Operators

Equals (=)

Tests if two expressions are equal.

SELECT * FROM Products WHERE Price = 19.99;

Not Equals (!= or <>)

Tests if two expressions are not equal.

SELECT * FROM Customers WHERE Country <> 'USA';

Greater Than (>)

Tests if the left expression is greater than the right expression.

SELECT * FROM Orders WHERE OrderDate > '2023-01-01';

Less Than (<)

Tests if the left expression is less than the right expression.

SELECT * FROM Employees WHERE Salary < 50000;

Greater Than or Equal To (>=)

Tests if the left expression is greater than or equal to the right expression.

SELECT * FROM Inventory WHERE Quantity >= 100;

Less Than or Equal To (<=)

Tests if the left expression is less than or equal to the right expression.

SELECT * FROM Scores WHERE Score <= 75;

Logical Operators

AND

Combines two conditions; returns true if both conditions are true.

SELECT * FROM Employees WHERE Department = 'Sales' AND HireDate > '2022-06-01';

OR

Combines two conditions; returns true if at least one condition is true.

SELECT * FROM Products WHERE Category = 'Electronics' OR Category = 'Appliances';

NOT

Reverses the result of a condition.

SELECT * FROM Users WHERE NOT IsActive;

ALL

A comparison operator that returns true if the comparison is true for ALL values in a subquery.

SELECT ProductName FROM Products WHERE Price > ALL (SELECT Price FROM SpecialOffers);

ANY

A comparison operator that returns true if the comparison is true for ANY value in a subquery.

SELECT ProductName FROM Products WHERE Price > ANY (SELECT Price FROM DiscountedItems);

EXISTS

Tests for the existence of rows in a subquery.

SELECT OrderID FROM Orders WHERE EXISTS (SELECT CustomerID FROM Customers WHERE Customers.CustomerID = Orders.CustomerID AND Country = 'Canada');

IN

Selects a value that matches any value in a list.

SELECT * FROM Employees WHERE City IN ('London', 'Paris', 'Berlin');

BETWEEN

Selects values within a given range. The range is inclusive.

SELECT * FROM Orders WHERE OrderDate BETWEEN '2023-07-01' AND '2023-07-31';

LIKE

Searches for a specified pattern in a column.

SELECT * FROM Customers WHERE ContactName LIKE 'A%';

Arithmetic Operators

Addition (+)

Adds two values.

SELECT Quantity + 10 AS NewQuantity FROM Stock;

Subtraction (-)

Subtracts one value from another.

SELECT Price - Discount AS FinalPrice FROM Products;

Multiplication (*)

Multiplies two values.

SELECT UnitPrice * Quantity AS TotalCost FROM OrderDetails;

Division (/)

Divides one value by another.

SELECT SUM(TotalRevenue) / COUNT(*) AS AverageRevenue FROM Sales;

Modulo (%)

Returns the remainder of a division.

SELECT Number, Number % 2 AS Remainder FROM Numbers WHERE Number % 2 = 0;

Bitwise Operators

These operators perform bit-by-bit operations on integer operands.

Bitwise AND (&)

Performs a bitwise AND operation.

SELECT Value FROM Flags WHERE Flags & 1 = 1;

Bitwise OR (|)

Performs a bitwise OR operation.

SELECT Value FROM Permissions WHERE Permissions | 4 = 4;

Bitwise XOR (^)

Performs a bitwise XOR operation.

SELECT Value FROM Settings WHERE Settings ^ 10 = 5;

Bitwise NOT (~)

Performs a bitwise NOT operation.

SELECT Value FROM Data WHERE ~Data <> 0;

Left Shift (<<)

Shifts bits to the left.

SELECT Value << 2 AS ShiftedValue FROM BinaryData;

Right Shift (>>)

Shifts bits to the right.

SELECT Value >> 1 AS ShiftedValue FROM BinaryData;