热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

Chapter 7

Aggregate Functions, GROUP BY, HAVING, and Window Functions

Use PostgreSQL for reporting, analytics, summaries, rankings, and advanced query calculations.

Inside this chapter

  1. Aggregation for Reporting
  2. Core Aggregate Functions
  3. Using HAVING
  4. Window Functions for Advanced Analysis

Series navigation

Study the chapters in sequence for the clearest path from beginner PostgreSQL concepts to advanced query design and production operations. Use the navigation at the bottom of every page to move chapter by chapter.

Tutorial Home

Chapter 7

Aggregation for Reporting

Most applications need summaries, not just raw rows. Managers want total revenue, counts by status, averages by region, and trends over time. PostgreSQL can compute these directly and efficiently close to the data.

Chapter 7

Core Aggregate Functions

SELECT
    order_status,
    COUNT(*) AS total_orders
FROM orders
GROUP BY order_status;

Functions like COUNT, SUM, AVG, MIN, and MAX are the base of reporting SQL.

Chapter 7

Using HAVING

SELECT
    customer_id,
    COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 5;

WHERE filters rows before grouping, while HAVING filters grouped results. That distinction is fundamental in SQL reporting work.

Chapter 7

Window Functions for Advanced Analysis

SELECT
    order_id,
    customer_id,
    order_date,
    ROW_NUMBER() OVER (
        PARTITION BY customer_id
        ORDER BY order_date DESC
    ) AS customer_order_rank
FROM orders;

Window functions are a major PostgreSQL strength. They let you rank, compare, and calculate running values without collapsing rows the way standard aggregation does. They are extremely useful in analytics, finance, operational dashboards, and interview scenarios.

版权所有 © 2026,WithoutBook。