PostgreSQL Operators

PostgreSQL supports a wide range of operators for mathematical calculations, logical comparisons, pattern matching, and more. Operators allow you to manipulate and compare data efficiently in queries and expressions.

Key Topics

1. Types of Operators

  • Arithmetic Operators: +, -, *, /, %
  • Comparison Operators: =, <, >, <=, >=, <>/!=
  • Logical Operators: AND, OR, NOT
  • Pattern Matching Operators: LIKE, ILIKE

2. Example Usage

SELECT first_name, salary
FROM employees
WHERE salary > 50000 AND department = 'Sales';

This query uses comparison (>) and logical (AND) operators to retrieve employees in Sales with a salary over 50,000.

Best Practices

  • Use the correct operator for the intended data type to avoid unexpected results.
  • Take advantage of indexes on columns used in comparison operators for performance.

Key Takeaways

  • Operators are fundamental for filtering, calculating, and joining data.
  • Logical operators combine or negate conditions in queries.
  • Pattern matching operators (LIKE, ILIKE) are powerful for text searches.