Skip to content

Chapter 5 of 8

Aggregates, Joins, and Subqueries

SQL is not limited to reading individual rows. Aggregate functions such as COUNT(), SUM(), AVG(), MAX(), and MIN() perform calculations across sets of rows, producing summary values like totals or averages. When these aggregates are combined with GROUP BY, the table is partitioned into groups that share the same values in the listed columns, and an aggregate is computed per group. For example, SELECT department, AVG(salary) FROM employees GROUP BY department; gives the average salary for each department. While WHERE filters individual rows before grouping, HAVING filters the resulting groups; HAVING AVG(salary) > 50000 keeps only departments whose average salary exceeds that threshold.

Most real-world queries combine data from multiple tables. Joins make this possible by combining rows from two or more tables based on related columns. An INNER JOIN returns only rows with matching values in both tables, while a LEFT JOIN returns all rows from the left table along with their matches from the right, filling non-matches with NULLs. The RIGHT JOIN does the mirror image, and a FULL OUTER JOIN returns all rows from both sides, again with NULLs where no match exists. A special case is the self-join, where a table is joined to itself, which is helpful for traversing hierarchical relationships such as an employee-to-manager chain.

Sometimes a query needs to draw on the results of another query. A subquery is a SELECT statement nested inside another query, used in the WHERE, FROM, or SELECT clauses, such as SELECT name FROM users WHERE id IN (SELECT user_id FROM orders);. A correlated subquery goes further by referencing columns from the outer query, so it executes once per outer row; this is useful for checks like SELECT name FROM users u WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = u.id);, which finds users who have placed at least one order. Finally, UNION combines the result sets of two queries and removes duplicates, while UNION ALL keeps duplicates; both require the queries to return compatible columns in the same order.

All chapters
  1. 1Database Fundamentals and SQL
  2. 2Tables, Keys, and Data Types
  3. 3Defining Database Structure with DDL
  4. 4Querying and Modifying Data with DML
  5. 5Aggregates, Joins, and Subqueries
  6. 6Indexes, Views, and Advanced Features
  7. 7Transactions, ACID, and Normalization
  8. 8SQL versus NoSQL and Database Security

Drill it

Reading is not remembering. These come from the SQL And Databases deck:

Q

What is a database?

A database is an organized collection of structured data stored and accessed electronically from a computer system. Databases allow efficient storage, retrieval...

Q

What is a Relational Database Management System (RDBMS)?

An RDBMS is a software system that manages relational databases, organizing data into tables with rows and columns related by keys. It supports SQL for querying...

Q

What are the main types of databases?

Databases are broadly classified into relational (SQL-based, like MySQL) and non-relational (NoSQL, like MongoDB for documents or Cassandra for wide-column stor...

Q

What is SQL?

SQL stands for Structured Query Language, a standard language for managing and manipulating relational databases. It includes sublanguages like DDL for schema d...