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!
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
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
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.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;