What are the Operators?
Every database admin or the geeks working on databases uses SQL queries to manipulate the data and retrieve the tables’ results. This manipulation of data is performed with the help of operators. An operator is a keyword used in SQL queries to manipulate the data, and it returns the results based on the functionality of that operator.
These operators allow us to perform multiple functionalities like performing arithmetic operations, comparing two expressions, etc. It generally also acts as a conjunction between multiple conditions in a query. The operators can either be unary operators or binary operators.
Unary operators use only one operand for their execution, whereas Binary operators use two operands. Operands are nothing but the objects on which we can use the operators. For example, consider the following statement: 1 + 2. Here 1 and 2 are the operands, whereas + is the operator. In SQL, instead of using only the numbers, we generally use tables to obtain the results.
In this article, we shall look into the vast range of operators offered in SQL. After listing all the operators, we shall see a few examples for better understanding.
Type of Operators
The operators are categorized based upon their functionality. The types of operators are as follows: –
1. Arithmetic Operators
Arithmetic operators are used to performing arithmetic operations like addition, subtraction, division, multiplication, etc. These operators generally take in numeric operands to manipulate the tables. The different operators that come under this category are as follows: –
|+||Addition||To add the values on either side of the operator.|
|–||Subtraction||To subtract the right-hand operand from the left-hand operand|
|*||Multiplication||To multiply the values on each side of the operator.|
|/||Division||To divide the left-hand operand by the right-hand operand.|
|%||Modulus||To divide the left-hand operand by the right-hand operand and return the remainder.|
2. Comparison Operators
Comparison operators are used to comparing the two operands and return the result based upon the comparison’s output. It will check if the row returns True or False for each row, and then it will return all the rows which match the criteria. The different operators that come under this category are as follows: –
|=||Equal to||It checks if the values of both the operands are equal or not. It will return the TRUE rows.|
|<> or !=||Not equal to||It checks if the values of both the operands are equal or not. If they are not equal, then it will return TRUE.|
|>||Greater than||It checks if the value of the left operand is greater than the right operand or not.|
|<||Less than||It checks if the value of the left operand is less than the right operand or not.|
|!>||Not greater than||It checks if the left operand’s value is not greater than or equal to the right operand.|
|!<||Not less than||It checks if the left operand’s value is not less than or equal to the right operand or not.|
|>=||Greater than or equal to||It checks if the left operand’s value is greater than or equal to the right operand or not.|
|<=||Less than or equal to||It checks if the left operand’s value is less than or equal to the right operand or not.|
3. Logical Operators
Logical operators are the boolean operators that take two expressions as operands and provide output as True or False. The expressions give output as True or False, and the logical operators are used to combine these expressions.
These operators come in handy when we are dealing with complex SQL queries. These operators perform similarly to what we know as logic gates. The different operators that come under this category are as follows: –
|ALL||Returns the rows for which all of the subqueries meet the condition specified|
|AND||Returns the rows for which all the conditions separated by AND return True|
|ANY||Returns the rows for which any of the subqueries meet the condition|
|SOME||Returns the rows for which any of the subqueries meet the condition|
|LIKE||Returns the rows for which the operand matches a pattern|
|IN||Returns the rows for which if the operand is equal to one of the lists of expressions|
|NOT||Returns the rows for which the condition(s) is NOT TRUE|
|OR||Returns the rows for which any of the conditions separated by OR is TRUE|
|BETWEEN||Returns the rows for which the operand is within the range of comparisons|
|EXISTS||Returns the rows for which the subquery returns one or more records|
|NULL||Returns the rows where the operands are NULL.|
4. Compound Operators
Compound operators are used to executing some operations and then set the original value to the query’s value. These operators are not available in some databases like SQLite. You can use them in T-SQL (Transact-SQL), which is an extension of SQL. The different operators that come under this category are as follows: –
|+=||Adds the value to the original value and sets the original value to the result.|
|-=||Subtracts the value from the original value and sets the original value to the result.|
|*=||Multiplies the original value by some value and sets the original value to the result.|
|/=||Divides the original value by some value and sets the original value to the result.|
|%=||Performs modulus and sets the original value to the result.|
|&=||Performs Bitwise AND operation and sets the original value to the result.|
|^-=||Performs Bitwise EX-OR operation and sets the original value to the result.|
||*=||Performs Bitwise OR operation and sets the original value to the result.|
5. Bitwise Operators
Bitwise operators are used to performing bitwise manipulations between the two expressions containing integers. These operators first convert the integers into bits and then operate on each bit to obtain the result.
The result is then again converted into an integer format to provide the outcome. These operators are seldomly used in SQL queries. The different operators that come under this category are as follows: –
|&||Used to perform bitwise AND operation|
||||Used to perform bitwise OR operation|
|^||Used to perform bitwise EX-OR operation|
Also Read: Interesting SQL Projects on GitHub
6. Set Operators
Set operators are used when we want to merge multiple queries using different tables. You can refer to the Venn diagrams to better understand how these set operations exactly work. The different operators that come under this category are as follows: –
|UNION||This operator will perform the union of both expressions.|
|UNION ALL||Similar in function to the union. The difference is this operator gives the result without removing any duplications.|
|INTERSECT||This operator will display the rows which are present in both expressions.|
|MINUS||This operator will display the rows present in the first expression but absent in the second expression.|
For illustration purposes, we will refer to one table named ‘cities’ and demonstrate how the operators work using this table. The below image that consists of a set of records will help you understand how the table looks and its different columns.
7. Arithmetic Operations
Query: – SELECT latd + 2, latd – 2, latd * 2, latd / 2, latd % 2 FROM cities;
In this query, we have used arithmetic operators to showcase how they work on the columns. The arithmetic operators deal with the numeric type of data and provide output accordingly.
8. Comparison Operations
Query: – SELECT * FROM cities WHERE latm <= 20;
This query will show all the rows where late is less than or equal to 20. Similarly, according to your use case, we can use other logical operations like <, >, etc.according to your use-case.
9. Logical Operations
Query: – SELECT * FROM cities WHERE lats BETWEEN 20 AND 50;
This query will display all the rows where the ‘lats’ column’s value lies in the range of 20 to 50, including the end values.
10. Bitwise Operators
Query: – SELECT CAST(latd as bit) | ’11’ FROM Cities;
The above query will perform bitwise OR operation on the operands. First, we need to convert the integer data type to bit values. To achieve this, we make use of the cast function. This function can help you to convert a value into another data type.
SELECT and FROM Cities UNION SELECT name FROM demo;
This query will list all the land’s column’s values and ‘name’ column from ‘cities’ and ‘demo’ tables.
Read: SQL vs PL/SQL
This article shed some light on what operators are in SQL and the different types of SQL operators. The list of operators mentioned here is not exhaustive. It may also happen that your database may not support some of the operators listed. For example, the operators shown in compound operators are supported in Transact-SQL, which is nothing but an SQL extension.
There can always be some alternate workarounds for performing such operations. Now with a huge amount of data being generated, SQL may not be an efficient solution for data of humungous sizes. Still, it acts as a great tool for storing different applications’ metadata and serving as a back-end database to these applications.
If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s PG Diploma 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.