Open In App

SQL Operators

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

SQL operators are important in database management systems (DBMS) as they allow us to manipulate and retrieve data efficiently. Operators in SQL perform arithmetic, logical, comparison, bitwise, and other operations to work with database values. Understanding SQL operators is crucial for performing complex data manipulations, calculations, and filtering operations in queries.

In this guide, we’ll explain the different types of SQL operators, including arithmetic operators, comparison operators, logical operators, bitwise operators, and more. We’ll provide clear examples to demonstrate how they work, helping you optimize your SQL queries for better performance and accuracy.

Operators in SQL

SQL operators are symbols or keywords used to perform operations on data in SQL queries. These operations can include mathematical calculations, data comparisons, logical manipulations, other data-processing tasks. Operators help in filtering, calculating, and updating data in databases, making them crucial for query optimization and accurate data management.

Types of SQL Operators

SQL operators can be categorized based on the type of operation they perform. Here are the primary types of SQL operators:

  • Arithmetic Operator
  • Comparison Operator
  • Logical Operator
  • Bitwise Operators
  • Compound Operators
  • Special Operators

Each of these operators is essential for performing different types of operations on data in SQL databases.

SQL Arithmetic Operators

Arithmetic operators in SQL are used to perform mathematical operations on numeric data types in SQL queries. Some common arithmetic operators:

Operator Description
+

The addition is used to perform an addition operation on the data values.

This operator is used for the subtraction of the data values.

/

This operator works with the ‘ALL’ keyword and it calculates division operations.

*

This operator is used for multiplying data values.

%

Modulus is used to get the remainder when data is divided by another.

Example: Arithmetic Operations

In this example, we calculates a 5% increment on employee salaries and returns both the original and updated salary values.

Query:

SELECT emp_salary, emp_salary * 1.05 AS "Revised Salary" FROM employee;

Output:

SQL-Arithmetic-Operator-Example

Arithmetic Operation Example

SQL Comparison Operators

Comparison Operators in SQL are used to compare one expression’s value to other expressions. SQL supports different types of comparison operator, which are described below:

Operator Description
= Equal to.
> Greater than.
< Less than.
>= Greater than equal to.
<= Less than equal to.
<> Not equal to.

Example: Comparison Operation

In this example, we will retrieve all records from the “MATHS” table where the value in the “MARKS” column is equal to 50.

Query:

SELECT * FROM MATHS WHERE MARKS=50;

Output:

sql comparison operators example output

SQL Logical Operators

Logical Operators in SQL are used to combine or manipulate conditions in SQL queries to retrieve or manipulate data based on specified criteria..

Operator Description
AND

Logical AND compares two Booleans as expressions and returns true when both expressions are true.

OR

Logical OR compares two Booleans as expressions and returns true when one of the expressions is true.

NOT

Not takes a single Boolean as an argument and change its value from false to true or from true to false.

Example: Logical Operation

In this example, retrieve all records from the “employee” table where the “emp_city” column is equal to ‘Allahabad‘ and the “emp_country” column is equal to ‘India’.

Query:

SELECT * FROM employee WHERE emp_city = 
'Allahabad' AND emp_country = 'India';

Output:

sql logical operators example output

SQL Bitwise Operators

Bitwise operators in SQL are used to perform bitwise operations on binary values in SQL queries, manipulating individual bits to perform logical operations at the bit level. Some SQL Bitwise Operators are:

Operator

Description

&

Bitwise AND operator

|

Bitwise OR operator

^

Bitwise XOR (exclusive OR) operator

~

Bitwise NOT (complement) operator

<<

Left shift operator

>>

Right shift operator

SQL Compound Operators

Compound operators combine an operation with assignment. These operators modify the value of a column and store the result in the same column in a single step. Some Compound operators are:

Operator

Description

+=

Add and assign

-=

Subtract and assign

*=

Multiply and assign

/=

Divide and assign

%=

Modulo and assign

&=

Bitwise AND and assign

^=

Bitwise XOR and assign

|=

Bitwise OR and assign

SQL Special Operators

SQL also provides several special operators that serve specific functions such as filtering data based on a range, checking for existence, and comparing sets of values.

Operators               Description 
ALL 

ALL is used to select all records of a SELECT STATEMENT. It compares a value to every value in a list of results from a query. The ALL must be preceded by the comparison operators and evaluated to TRUE if the query returns no rows.

ANY

ANY compares a value to each value in a list of results from a query and evaluates to true if the result of an inner query contains at least one row.

BETWEEN

The SQL BETWEEN operator tests an expression against a range. The range consists of a beginning, followed by an AND keyword and an end expression.

IN

The IN operator checks a value within a set of values separated by commas and retrieves the rows from the table that match.

EXISTS

The EXISTS checks the existence of a result of a subquery. The EXISTS subquery tests whether a subquery fetches at least one row. When no data is returned then this operator returns ‘FALSE’.  

SOME SOME operator evaluates the condition between the outer and inner tables and evaluates to true if the final result returns any one row. If not, then it evaluates to false.
UNIQUE The UNIQUE operator searches every unique row of a specified table.

Example: Special Operator (BETWEEN)

In this example, we will retrieve all records from the “employee” table where the “emp_id” column has a value that falls within the range of 101 to 104 (inclusive).

Query:

SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;

Output:

sql special operator example output

Conclusion

SQL operators are essential tools for working with data in a relational database. Whether you’re performing arithmetic operations, logical comparisons, bitwise manipulations, or using special operators to filter data, understanding these operators is key to writing efficient and effective SQL queries. By mastering SQL operators, we can simplify complex data operations, enhance query performance, and manipulate data more effectively. Whether you’re a beginner or an advanced SQL user, the knowledge of operators will help you unlock the full potential of SQL queries and database management systems.



Next Article
Article Tags :

Similar Reads