bytes

tutorials

sql

sql operators

Module - 7 Filtering in SQL

Lesson - 1 SQL Operators

**Overview**

SQL Operators are used to specifying conditions in an SQL statement. The combination of values, operators, and SQL statements can be used to retrieve data from one or more tables in a database. Common operators include comparison operators (e.g. =, >, <, >=, <=, <>), logical operators (e.g. AND, OR, NOT), arithmetic operators (e.g. +, -, *, /), and set operators (e.g. UNION, INTERSECT, EXCEPT). SQL also includes special operators, such as LIKE and BETWEEN, for additional functionality.

**SQL Arithmetic Operators**

A retail store uses SQL to track customer purchases. The store could use the WHERE clause to filter customer purchase data by product type and apply the greater than operator (>) to find the customers who have purchased more than a certain amount of a particular product type.Let's help the store employee with it.

- +: Used to add two values
- -: Used to subtract one value from another
- *: Used to multiply two values
- /: Used to divide one value by another
- %: Used to calculate the remainder of a division

Examples

```
SELECT 5 + 5;
```

This statement will add 5 and 5 together and return the result of 10.

```
SELECT 10 - 5;
```

This statement will subtract 5 from 10 and return the result of 5.

```
SELECT 10 * 5;
```

This statement will multiply 10 and 5 together and return the result of 50.

```
SELECT 10 / 5;
```

This statement will divide 10 by 5 and return the result of 2.

```
SELECT 10 % 5;
```

This statement will calculate the remainder of 10 divided by 5 and return the result of 0.

**SQL Bitwise Operators**

- Bitwise AND ( & )
- Bitwise OR ( | )
- Bitwise XOR ( ^ )

For example, if we wanted to retrieve all records from a table whose ID is either 1, 4, or 5, we could use the following SQL query:

**Table:**

ID | Name | Address |
---|---|---|

1 | Bob | 123 Main St |

2 | Joe | 456 Main St |

3 | Jane | 789 Main St |

4 | Sam | 123 Elm St |

5 | Tom | 456 Elm St |

```
SELECT * FROM table WHERE ID & (1 | 4 | 5);
```

This query uses the bitwise OR (|) operator to combine the values 1, 4, and 5 into a single expression. The bitwise AND (&) operator is then used to compare this expression with the values in the ID column. If any of the values match, that record will be returned.

**SQL Comparison Operators**

- = (equal to)
- <> (not equal to)
- >(greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)

**Examples**

**= (equal to)**

**Table:**

id | name | age | nationality |
---|---|---|---|

1 | Alice | 35 | USA |

2 | Bob | 36 | UK |

3 | Chris | 35 | Canada |

```
SELECT * FROM table WHERE age = 35;
```

This statement will select all records from the table where the age of the record is equal to 35.

**<> (not equal to)**

```
SELECT * FROM table WHERE age <> 35;
```

This statement will select all records from the table where the age of the record is not equal to 35.

**>(greater than)**

```
SELECT * FROM table WHERE age > 35;
```

This statement will select all records from the table where the age of the record is greater than 35.

**< (less than)**

```
SELECT * FROM table WHERE age < 35;
```

This statement will select all records from the table where the age of the record is less than 35.

**>= (greater than or equal to)**

```
SELECT * FROM table WHERE age >= 35;
```

This statement will select all records from the table where the age of the record is greater than or equal to 35.

**<= (less than or equal to)**

```
SELECT * FROM table WHERE age <= 35;
```

This statement will select all records from the table where the age of the record is less than or equal to 35.

**Key takeaways**

- SQL Operators are used to filtering, group, and sort data within relational databases.
- The different types of SQL Operators include comparison, logical, arithmetic, and set operators.
- Comparison operators allow you to compare values within a query and return specific results.
- Logical operators are used to combining multiple conditions within a query.
- Arithmetic operators are used to performing mathematical calculations on data from a database.
- Set operators allow you to combine the results of multiple queries into one.
- SQL Operators are essential for database querying and manipulation.

**Conclusion**

The retail store used SQL to filter and sort customer purchase data and successfully identify customers who had purchased more than a certain amount of a particular product type. This allowed them to target those customers with special offers and discounts, which resulted in increased sales.

**Quiz**

**Which of the following is a SQL operator?**- AND
- OR
- XOR
- ALL

**Answer**: A. AND

**Which of the following is a logical operator?**- AVG
- OR
- BETWEEN
- MAX

**Answer**: B. OR

**Which of the following is a comparison operator?**- LIKE
- IN
- MOD
- INT

**Answer**: A. LIKE

**Which of the following is an arithmetic operator?**- BETWEEN
- +
- AND
- IS

**Answer**: B. +

Made with

in Bengaluru, India - Join AlmaBetter
- Sign Up
- Become A Coach
- Coach Login

- Contact Us
- admissions@almabetter.com
- 08046008400

- Location
- 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025

- Follow Us

© 2022 AlmaBetter