SQL - Operators
SQL operators are used to perform operations on data within SQL queries. They allow you to compare values, perform arithmetic operations, manipulate strings, and more. Here are some commonly used SQL operators:
Arithmetic Operators:
- Addition (+): Adds two values together.
- Subtraction (-): Subtracts one value from another.
- Multiplication (*): Multiplies two values.
- Division (/): Divides one value by another.
- Modulus (%): Returns the remainder of a division operation.
Comparison Operators:
- Equal to (=): Checks if two values are equal.
- Not equal to (!= or <>): Checks if two values are not equal.
- Greater than (>), Greater than or equal to (>=): Performs comparisons based on value magnitude.
- Less than (<), Less than or equal to (<=): Performs comparisons based on value magnitude.
- BETWEEN: Checks if a value is within a specified range.
- LIKE: Performs pattern matching with wildcard characters (% and _).
Logical Operators:
- AND: Combines multiple conditions, and all conditions must be true for the result to be true.
- OR: Combines multiple conditions, and at least one condition must be true for the result to be true.
- NOT: Negates a condition, returning true if the condition is false, and vice versa.
String Operators:
- Concatenation (||): Combines two or more strings together.
- LIKE: Performs pattern matching with wildcard characters (% and _).
- SUBSTRING: Extracts a portion of a string based on specified starting position and length.
Aggregate Functions:
- COUNT: Returns the number of rows or non-null values in a column.
- SUM: Calculates the sum of values in a column.
- AVG: Calculates the average of values in a column.
- MIN: Returns the minimum value in a column.
- MAX: Returns the maximum value in a column.
These operators allow you to build complex queries and manipulate data in various ways. Understanding and utilizing these operators effectively is crucial for querying and manipulating data in SQL databases.