SQL Operators
SQL operators are used to perform operations on data in SQL queries. They enable you to compare values, perform arithmetic calculations, and combine conditions. Here are some commonly used SQL operators:
- Comparison Operators
- Equal to: =
- Not equal to: <> or !=
- Greater than: >
- Less than: <
- Greater than or equal to: >=
- Less than or equal to: <=
-
Logical Operators:
- AND: Returns true if all conditions are true.
- OR: Returns true if at least one condition is true.
- NOT: Negates a condition, returns true if the condition is false.
-
Arithmetic Operators:
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Modulo (remainder): %
-
String Operators:
- Concatenation: || or CONCAT()
- Pattern matching:
- LIKE: Matches a pattern using wildcard characters (% for any number of characters, _ for a single character).
- NOT LIKE: Negation of LIKE.
- IN: Matches a value against a list of values.
- NOT IN: Negation of IN.
-
NULL-related Operators:
- IS NULL: Checks if a value is null.
- IS NOT NULL: Checks if a value is not null.
-
Set Operators:
- UNION: Combines the result sets of two or more SELECT statements, removing duplicates.
- UNION ALL: Similar to UNION, but includes duplicates.
- INTERSECT: Returns the common rows between two result sets.
- EXCEPT or MINUS: Returns the rows that exist in the first result set but not in the second.
-
Aggregate Functions:
- COUNT: Returns the number of rows.
- SUM: Returns the sum of values.
- AVG: Returns the average of values.
- MAX: Returns the maximum value.
- MIN: Returns the minimum value.
These are some of the commonly used SQL operators. The specific operators available may vary slightly depending on the database management system you are using. It's important to understand and use operators correctly to perform effective data retrieval, filtering, and calculations in SQL queries.