PostgreSQL Operators
Overview
Operators are symbols or keywords used to perform operations. PostgreSQL supports various types of operators including arithmetic, comparison, logical, string, and other special operators.
Arithmetic Operators
Basic Arithmetic Operations
Practical Examples
Comparison Operators
Basic Comparisons
Comparison Examples
Logical Operators
AND Operator
OR Operator
NOT Operator
Combining Logical Operators
String Operators
Concatenation Operator
Pattern Matching Operators
LIKE Operator
ILIKE Operator (Case-Insensitive)
SIMILAR TO Operator
Regular Expression Operators
Range Operators
BETWEEN Operator
IN Operator
NULL Operators
IS NULL and IS NOT NULL
IS DISTINCT FROM
Array Operators
JSON Operators
Bitwise Operators
Type Cast Operators
Subquery Operators
EXISTS Operator
ANY/SOME Operator
ALL Operator
Operator Precedence
From highest to lowest:
::(type cast)[](array element).(table/column name separator)-(unary minus)^(exponentiation)*,/,%(multiply, divide, modulo)+,-(add, subtract)||(string concatenation)BETWEEN,IN,LIKE,ILIKE,SIMILAR TO<,>,=,<=,>=,<>(comparison)IS NULL,IS NOT NULL,IS DISTINCT FROMNOT(logical NOT)AND(logical AND)OR(logical OR)
Use Parentheses to Control Precedence
Practical Examples
Example 1: E-commerce Product Filtering
Example 2: User Data Analysis
Example 3: Order Statistics
Performance Optimization Tips
Best Practices
- Use parentheses for clarity: Even when not required, use parentheses to make logic clearer
- Choose the right operator: Use the most appropriate operator for the task
- Handle NULL properly: Use IS NULL instead of = NULL
- Consider performance: Create indexes on frequently used columns
- Match types: Ensure data types match in comparisons
- Use standard SQL: Prefer standard SQL operators for better portability
Summary
PostgreSQL operators are fundamental to building queries:
- Arithmetic operators: +, -, *, /, %, ^
- Comparison operators: =, <>, <, >, <=, >=
- Logical operators: AND, OR, NOT
- String operators: ||, LIKE, ILIKE, ~
- Range operators: BETWEEN, IN
- NULL operators: IS NULL, IS NOT NULL
- Special operators: EXISTS, ANY, ALL
Understanding and correctly using operators is crucial for writing efficient SQL queries.
PostgreSQL Expressions
Expression Overview
Expressions are combinations of values and operators in PostgreSQL that compute results. Expressions can be used in SELECT, WHERE, HAVING, and other clauses.
Conditional Expressions
CASE Expression
Simple CASE Expression
Searched CASE Expression
Nested CASE Expression
COALESCE Expression
NULLIF Expression
GREATEST and LEAST Expressions
Window Expressions
Expression Best Practices
- Use parentheses for readability: Use parentheses in complex expressions to clarify precedence
- Avoid excessive nesting: Deeply nested expressions are hard to maintain
- Use meaningful aliases: Provide clear aliases for complex expressions
- Handle NULL values: Use COALESCE or NULLIF to handle NULL values
- Consider performance: Complex expressions may impact query performance
- Type matching: Ensure data types are compatible in expressions
Expression Summary
PostgreSQL expressions provide powerful data processing capabilities:
- CASE expressions: Conditional logic
- COALESCE: NULL value handling
- NULLIF: Conditional NULL return
- GREATEST/LEAST: Multi-value comparison
- Window expressions: Advanced analytics
Mastering expressions enables writing more flexible and powerful SQL queries.