We have to perform several operations to retrieve or alter any existing data from the database. SQL offers several operators to perform database management system operations. Here through this article, we will discuss several SQL operators and some basic information and examples. Let us discuss operators in SQL now.
Table of Contents
What are Operators?
SQL Operators are like SQL keywords that can perform arithmetic, comparison, and logical operations. They are like conjunctions and can be used to specify the operations and conditions in SQL statements. They can serve multiple conditions in the statement like:
- Arithmetic Operators
- Logical Operators
- Comparison Operators
As there are several types of operators in SQL anyone can take any SQL course to learn more operators. Let us understand them with examples in the next section.
Operators in SQL
1. Arithmetic Operators
Arithmetic operators can be used to perform arithmetic operations that include addition, subtraction, multiplication, and division.
|+||Addition||This operator is used to add two given values|
|–||Subtraction||To subtract the two given values, we can use this operator|
|*||Multiplication||If we have to multiply the two given values, then we can use this operator|
|/||Division||To divide the two given values, we can use the division operator|
|%||Modulus||This operator returns the remainder obtained by dividing the two given values|
Example of Arithmetic Operators in SQL:
SELECT 80 + 10;
SELECT 80 – 10;
SELECT 80 * 10;
SELECT 80 / 10;
SELECT 80 % 10;
The output of the above SQL commands with arithmetic operators will be:
So, these are the arithmetic operators of SQL. Let us now see the Comparison operators in SQL.
2. Comparison Operators in SQL
Comparison operators are used to comparing the two values that may be either less than or greater than or equal to each other. The table of comparison operators is:
|=||Equal to||To compare the two values, we can use this operator.|
|>||Greater than||This Boolean operator returns TRUE if the value of the left operand is greater than the right operand value.|
|<||Less than||This Boolean operator returns TRUE if the value of the left operand is less than the right operand value.|
|<=||Less than or Equal to||This Boolean operator returns TRUE if the value of the left operand is less than or equal to the right operand value.|
|>=||Greater than or Equal to||This Boolean operator returns TRUE if the value of the left operand is greater than or equal to the right operand value.|
|< > Or !=||Not Equal to||This operator is used to check whether the two given values are equal to each other or not.|
|! >||Not greater than||To check whether the left operand is greater than or not than the right operand and this operator returns TRUE if it is found the truth.|
|!<||Not less than||To check whether the left operand is less than or not than the right operand and this operator returns TRUE if it is found the truth.|
To compare the logical values, these operators are used in SQL. Now let us discuss a few of the
logical operators in SQL.
Example of “=”,”<” and “>” Operator
SELECT * FROM Employees WHERE Age = 20;
SELECT * FROM Employees WHERE Age < 20;
SELECT * FROM Employees WHERE Age > 20;
3. Logical Operators in SQL
Let us now discuss the logical operators in SQL. There are few cases as specified in the following table where we can use these logical operators:
|Operator||Where to Use the Operator?|
|ALL||This operator is used to compare some specific values to the rest of the values of any set.|
|ANY||To compare any specific value of a set to the rest of the values, we can use this operator.|
|IN||To compare any specific value to the literals, we can use this operator.|
|BETWEEN||To search any value within a given range, we can use this operator.|
|AND and OR||These operators are used to define multiple conditions in the WHERE clause.|
|NOT||To reverse the output of the logical operator, the NOT operator can be used.|
|LIKE||If we want to compare a pattern through wildcard operators, then we can use the LIKE operator.|
|SOME||To compare some of the values to any specific value,we can use the SOME operator.|
|EXISTS||We can use this operator to search the presence of any row in the table.|
Example of BETWEEN and IN Operators
SELECT * FROM Employees WHERE Age BETWEEN 20 AND 40;
SELECT * FROM Employees WHERE Age IN (‘20’,‘ 40’);
These two queries are examples of SQL operators.
Related Read: SQL Project Ideas & Topics
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
SQL has many uses and operators, and you can use and practice these operators to know more about them. SQL operators help in fast data processing. Moreover, to compare any two values, these operators can be directly used. The operators are being asked during many interviews of fresher and experienced candidates. Learning SQL commands and an operator can help you in clearing interviews.
If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s Executive PG Programme in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.