SQL Operators

Welcome to The Coding College! This tutorial covers SQL Operators, essential building blocks for performing operations on data. Operators allow you to filter, manipulate, and calculate data efficiently within SQL queries.

What Are SQL Operators?

SQL Operators are symbols or keywords used to specify conditions, perform calculations, and manipulate data in SQL queries. They are categorized based on their functionality.

Types of SQL Operators

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Special Operators

1. Arithmetic Operators

Arithmetic operators are used for mathematical calculations in SQL.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

Example

SELECT 10 + 5 AS Addition,  
       10 - 5 AS Subtraction,  
       10 * 5 AS Multiplication,  
       10 / 5 AS Division,  
       10 % 3 AS Modulus;  

Result:

AdditionSubtractionMultiplicationDivisionModulus
1555021

2. Comparison Operators

Comparison operators compare two values and return a boolean result (TRUE, FALSE, or NULL).

OperatorDescriptionExample
=Equal toa = b
<>Not equal toa <> b
!=Not equal to (Alternate)a != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example

SELECT * FROM employees WHERE salary > 50000;  

This query retrieves employees with a salary greater than 50,000.

3. Logical Operators

Logical operators are used to combine multiple conditions in SQL.

OperatorDescriptionExample
ANDReturns TRUE if all conditions are TRUEa AND b
ORReturns TRUE if any condition is TRUEa OR b
NOTReverses the result of a conditionNOT a

Example

SELECT * FROM employees  
WHERE department = 'IT' AND salary > 60000;  

This retrieves IT department employees with a salary above 60,000.

4. Bitwise Operators

Bitwise operators perform operations on binary data.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b

5. Special Operators

Special operators provide additional functionality for working with patterns, ranges, or null values.

OperatorDescriptionExample
BETWEENChecks if a value is within a rangea BETWEEN x AND y
INChecks if a value matches any value in a lista IN (x, y, z)
LIKEChecks if a value matches a patterna LIKE 'pattern%'
IS NULLChecks if a value is NULLa IS NULL

Example: Special Operators

SELECT * FROM employees  
WHERE salary BETWEEN 40000 AND 60000;  

SQL Operator Precedence

SQL evaluates operators in a specific order of precedence. Parentheses can override this order.

Precedence Order:

  1. Arithmetic Operators (+, -, *, /, %)
  2. Comparison Operators (=, >, <, etc.)
  3. Logical Operators (NOT, AND, OR)

Example

SELECT * FROM employees  
WHERE NOT (department = 'HR' AND salary > 40000);  

Best Practices

  1. Use Parentheses for Clarity:
    Parentheses ensure clarity and avoid ambiguity in complex queries.
  2. Optimize Logical Conditions:
    Use AND and OR carefully to minimize unnecessary conditions.
  3. Test Complex Queries:
    Break down queries to test individual conditions before combining them.

Conclusion

SQL Operators are powerful tools for building queries that filter, manipulate, and retrieve data efficiently. By mastering these operators, you can handle even the most complex database requirements.

Leave a Comment