SQL Operators Reference
This section provides detailed information on the various operators available in SQL Server, used for performing operations on one or more values.
Arithmetic Operators
Used for performing mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | SELECT 5 + 3; |
- |
Subtraction | SELECT 10 - 4; |
* |
Multiplication | SELECT 6 * 7; |
/ |
Division | SELECT 20 / 5; |
% |
Modulo (Remainder) | SELECT 10 % 3; |
Comparison Operators
Used for comparing two expressions. They return TRUE, FALSE, or UNKNOWN.
Operator | Description | Example |
---|---|---|
= |
Equal to | SELECT * FROM Customers WHERE City = 'London'; |
> |
Greater than | SELECT * FROM Products WHERE Price > 50.00; |
< |
Less than | SELECT * FROM Orders WHERE OrderDate < '2023-01-01'; |
>= |
Greater than or equal to | SELECT * FROM Employees WHERE Salary >= 60000; |
<= |
Less than or equal to | SELECT * FROM Inventory WHERE Quantity <= 10; |
<> or != |
Not equal to | SELECT * FROM Suppliers WHERE Country <> 'USA'; |
IS NULL |
Checks if a value is NULL | SELECT * FROM Customers WHERE Email IS NULL; |
IS NOT NULL |
Checks if a value is not NULL | SELECT * FROM Products WHERE Description IS NOT NULL; |
Logical Operators
Used to combine conditional statements or compare the results of conditional statements.
Operator | Description | Example |
---|---|---|
AND |
Combines two conditions. Returns TRUE if both conditions are TRUE. | SELECT * FROM Products WHERE Price > 50 AND Category = 'Electronics'; |
OR |
Combines two conditions. Returns TRUE if at least one condition is TRUE. | SELECT * FROM Customers WHERE City = 'London' OR City = 'Paris'; |
NOT |
Reverses the result of a condition. | SELECT * FROM Orders WHERE NOT OrderStatus = 'Shipped'; |
ALL |
Returns TRUE if all subquery values meet the condition. | SELECT ProductName FROM Products WHERE Price > ALL (SELECT Price FROM Products WHERE Category = 'Discontinued'); |
ANY |
Returns TRUE if any subquery value meets the condition. | SELECT ProductName FROM Products WHERE Price > ANY (SELECT Price FROM Products WHERE Category = 'SpecialOffer'); |
BETWEEN |
Checks if a value is within a range (inclusive). | SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 70000; |
IN |
Checks if a value is present in a list of values. | SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'Spain'); |
LIKE |
Performs pattern matching. | SELECT * FROM Customers WHERE CustomerName LIKE 'A%'; |
EXISTS |
Checks if a subquery returns any rows. | SELECT * FROM Customers WHERE EXISTS (SELECT * FROM Orders WHERE Orders.CustomerID = Customers.CustomerID); |
String Operators
Used for concatenating strings or performing string manipulations.
Operator | Description | Example |
---|---|---|
+ |
Concatenates two strings. | SELECT 'Microsoft' + ' ' + 'SQL Server'; |
|| |
Concatenates two strings (ANSI standard). | SELECT 'Microsoft' || ' ' || 'SQL Server'; |
Set Operators
Used to combine the result sets of two or more SELECT statements.
Operator | Description | Example |
---|---|---|
UNION |
Combines result sets and removes duplicate rows. | SELECT City FROM Customers UNION SELECT City FROM Suppliers; |
UNION ALL |
Combines result sets and keeps duplicate rows. | SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers; |
INTERSECT |
Returns only rows that appear in both result sets. | SELECT City FROM Customers INTERSECT SELECT City FROM Suppliers; |
EXCEPT |
Returns rows from the first result set that do not appear in the second. | SELECT City FROM Customers EXCEPT SELECT City FROM Suppliers; |