Course contentsShow
← Computer Science
Lesson 1559 of 2,87236. Database FundamentalsFree lesson

Relational Algebra Basics

Introduction to fundamental operations (select, project, union, difference) that form the theoretical basis of querying relational databases.

Relational Algebra Basics

You'll learn: The fundamental mathematical operations that enable all queries on relational databases.

What Is Relational Algebra?

Relational algebra is the theoretical foundation beneath SQL and other database query languages. It defines a set of operations that take one or more tables (relations) as input and produce a new table as output. Think of it as the "math of databases"—just like arithmetic operations (+, −, ×, ÷) work on numbers, relational algebra operations work on tables.

These operations are procedural, meaning you describe how to retrieve data step-by-step, which later influenced the design of SQL.

The Fundamental Operations

Select (σ)

Filters rows based on a condition. Like using a sieve—you keep only rows that meet your criteria.

Example: Select all customers where age > 25.

Project (π)

Chooses specific columns from a table. Imagine cutting away unwanted columns from a spreadsheet.

Example: Project just the name and email columns from a customer table.

Union (∪)

Combines rows from two tables with identical schemas, removing duplicates. Like merging two compatible lists.

Example: Union of active customers and VIP customers (assuming same columns).

Difference (−)

Returns rows in the first table that aren't in the second. Think of subtraction for sets.

Example: All customers minus those who made purchases = customers with no orders.

Why It Matters

These operations form building blocks. More complex queries combine them: you might select rows matching a condition, then project only certain columns, then take the union with another filtered table. Understanding relational algebra helps you reason about query optimization and what's actually happening when you run SQL.

Key Takeaway: Relational algebra provides four foundational operations—select (filter rows), project (choose columns), union (combine tables), and difference (subtract tables)—that form the mathematical basis for all database queries.