SQL Operator: Comparison and Arithmetic

# SQL Operator: Comparison and Arithmetic

clickHere
Vikram Singh
Assistant Manager - Content
Updated on Nov 22, 2022 19:48 IST

While working with a large dataset you have to perform a different kind of operation to extract or manipulate the the data from the dataset. To perform these operation we nee different types of operators. In this article we will discuss Comparison and Arithmetic Operator in SQL.

Similar to the other programming languages SQL has different types of operators. Operators are the reserved keywords that are use to specify the conditions in an SQL statement.

In SQL there are mainly three different types of Operators

• Comparison Operator
• Arithmetic Operator
• Logical Operator

## Comparison Operator

Comparison operators are one of the most basic ways to put filters during writing queries to extract the data, and these operators are used with the WHERE clause in SQL.

Here is the list of comparison operators in SQL

Now we will take the Employee dataset (that contains Employee ID, Name, Gender, Department, Education, Month of Joining, and CTC) to do some example to get more understanding about these comparison operators.

Example 1: Find the employee detail who joined in the month of January.

Query

` `
`SELECT *FROM EmployeeWHERE Month of Joining = ‘January’;Copy code`

Output

Example 2: Find the employee details where the CTC is greater than or equal to 16.

Query

` `
`SELECT *FROM EmployeeWHERE CTC >= 16;Copy code`

Output

Example 3: Find the employee details where the CTC is less than 8.

Query

` `
`SELECT *FROM EmployeeWHERE CTC < 8;Copy code`

Output

Must Read: Types of Keys in Database

Example 4: Find the employee detail who have not joined in the month of January.

Query

` `
`SELECT *FROM EmployeeWHERE Month of Joining != ‘January’;Copy code`

Output

Now, let’s discuss Arithmetic operators in SQL.

Must Read: Window Function in SQL

## Arithmetic Operators

Similar to Comparison Operator, SQL has five different Arithmetic Operators, let’s explore them:

Now we will take the Sales dataset (that contains Month and Region-wise Sales in North, East, West, and South region) to do some examples to get more understanding about these arithmetic operators.

Example 1: Find the sum of sales of the region North and South.

Query

` `
`SELECT Month, North, South, North + SouthFROM Sales;Copy code`

Output

Must Read: Aggregate and Scalar Function in SQL

Example 2: Subtract 10 from the combined sales of North + South and then multiply 2 in the final result.

Query

` `
`SELECT Month, North, South, (North + South -10) * 2 AS New RecordFROM Sales;Copy code`

Output

Example 3: Find the average sales of North and South region.

Query

` `
`SELECT Month, North, South (North + South) / 2 AS Avg SalesFROM Sales;Copy code`

Output

Must Read: Difference between SQL and NoSQL

Must Check: SQL Online Course and Certifications

## Conclusion

In this article, we have discussed comparison and arithmetic operators in SQL in the most simplified way with the help of examples.