SQLMarrow / Course Academy Experience

SQL & Databases Masterclass Course

Welcome to the ultimate postgresql tutorial for beginners 2026. Work through interactive sql practice problems with solutions, master our comprehensive sql window functions tutorial modules, and prepare for tough sql interview questions at Google and Amazon entirely in your browser. Claim your course completion XP and build production-ready database mastery today!

COURSE PROGRESS
0%
0 of 40 Modules Solved
COURSE SYLLABUS
40 Lessons
LESSON WORKSPACEIntermediate

SQL Lesson 10: Queries with aggregates (Pt. 1)

Introduction & Core Concept

When analyzing enterprise data, you rarely want to read through individual transaction rows. Instead, you want big-picture summary metrics: "What is our total revenue?", "What is our average order value?", or "How many active users do we have?"

To compute these metrics, SQL provides Aggregate Functions. These functions ingest an entire column of data across multiple rows and collapse them into a single, highly summarized statistic.

Core Aggregate Functions:

  • COUNT(column): Returns the total number of populated (non-NULL) rows. Use COUNT(*) to count all rows regardless of nulls.
  • SUM(column): Computes the mathematical addition of all values.
  • AVG(column): Calculates the mathematical mean.
  • MIN(column): Identifies the absolute lowest value.
  • MAX(column): Identifies the absolute highest value.
  • Why & Where We Use It
  • Why We Use It: Allows organizations to distill millions of raw transactional events into clear, actionable executive KPIs in milliseconds.
  • Where We Use It: Financial balance sheets, executive overview dashboards, inventory audits, and traffic tracking.
  • Real-World Example

    ApexBank executives want an immediate health check of their retail banking division. They run a query calculating the total number of active accounts, the total summation of all cash deposits, and the average balance held per account.

    Best Practices: What to Do & What NOT to Do
  • What to Do: Combine COUNT(DISTINCT column) to count unique occurrences. For example, COUNT(DISTINCT user_id) across sales logs tells you exactly how many *unique* customers made purchases, rather than total orders.
  • What NOT to Do: Never mix unaggregated standalone columns with aggregate functions without using a GROUP BY clause. Doing so triggers a strict syntax parsing exception!
  • Syntax & Pro Tips
    SELECT 
      COUNT(*) AS total_accounts, 
      SUM(balance) AS gross_deposits, 
      AVG(balance) AS average_balance
    FROM bank_accounts;
    Interactive Sandboxed Terminal (Preloaded DB Schema: APEXBANK)
    SQL Query WorkspaceSQLite v3.45 (WASM Mode)
    QUICK INSERT:
    1