Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

SQL Functions and Operators in PostgreSQL

Introduction

PostgreSQL provides a rich set of built-in functions and operators that allow users to perform various operations on data. Understanding these functions and operators is crucial for effective data manipulation and retrieval.

SQL Functions

Functions in PostgreSQL are reusable code blocks that accept parameters, perform operations, and return a value. They can be categorized into several types:

  • Aggregate Functions
  • Scalar Functions
  • Window Functions
  • Set-returning Functions

1. Aggregate Functions

Aggregate functions operate on a set of values and return a single value. Common examples include:

  • COUNT()
  • SUM()
  • AVG()
  • MAX()
  • MIN()
SELECT COUNT(*), AVG(salary) FROM employees;

2. Scalar Functions

Scalar functions operate on a single value and return a single value. Examples include:

  • UPPER()
  • LOWER()
  • TRIM()
  • ROUND()
SELECT UPPER(first_name) FROM employees;

3. Window Functions

Window functions perform calculations across a specified range of rows related to the current row. They are often used with OVER() clause.

SELECT employee_id, salary, AVG(salary) OVER (PARTITION BY department_id) AS avg_department_salary FROM employees;

4. Set-returning Functions

These functions return a set of rows, such as:

  • GENERATE_SERIES()
  • UNNEST()

SQL Operators

Operators in PostgreSQL are special symbols used to perform operations on one, two, or more operands. They can be categorized as follows:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • String Operators

1. Arithmetic Operators

Used to perform basic mathematical operations:

  • +
  • -
  • *
  • /
  • %
SELECT price, price * 0.9 AS discounted_price FROM products;

2. Comparison Operators

Used to compare two values:

  • =
  • !=
  • >
  • <
  • >=
  • <=

3. Logical Operators

Used to combine multiple conditions:

  • AND
  • OR
  • NOT

4. Bitwise Operators

Used for bitwise operations on integer types:

  • &
  • |
  • ^
  • <<
  • >>

5. String Operators

Used to manipulate string data:

  • || (concatenation)
  • LIKE (pattern matching)
  • ILIKE (case-insensitive pattern matching)
SELECT 'Hello' || ' World';

Best Practices

When using SQL functions and operators in PostgreSQL, consider the following best practices:

  • Always use explicit data types.
  • Be aware of null values and how they affect functions and operators.
  • Optimize your queries by avoiding unnecessary use of functions in WHERE clauses.
  • Utilize indexes where applicable to improve performance.

FAQ

What is the difference between scalar and aggregate functions?

Scalar functions operate on a single value and return a single value, while aggregate functions operate on a set of values and return a single result.

Can I create my own functions in PostgreSQL?

Yes, PostgreSQL allows you to create user-defined functions using various programming languages such as SQL, PL/pgSQL, and more.

What are window functions?

Window functions perform calculations across a set of table rows that are related to the current row. They are particularly useful for running totals, moving averages, and ranking.