Overview
SQL Operators are used to specify 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 such as Not Equal and Equal 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.
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
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
Examples
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.
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.
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.
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.
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.
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
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
Answer: A. AND
Answer: B. OR
Answer: A. LIKE
Answer: B. +
Top Tutorials
Related Articles