๐Ÿ“Š Module 3: Algebra & Querying

What Happens When You Ask a Database a Question?


๐ŸŒŸ Lesson Objective

By the end of this module, you will:

  • Understand the basic operations of relational algebra and how they relate to table logic
  • Learn how relational algebra maps to simple SQL queries
  • Use small example tables to visualize set-based operations
  • See why query order and structure can affect performance

๐Ÿฅ Why This Matters

When you ask a question using SQL, the database doesnโ€™t just โ€œlook upโ€ the answerโ€”it transforms and filters sets of rows based on underlying rules. Relational algebra is the theoretical language that explains how that process works.

You wonโ€™t be writing algebra expressions in real life, but understanding them will help you:

  • Write better queries
  • Understand what SQL is doing behind the scenes
  • Improve performance by thinking like the database engine

๐Ÿ“Š Section 1: The Building Blocks of Relational Algebra

๐Ÿ“˜ Relational Algebra is a formal system that describes how to manipulate sets of rows from tables. Each operation takes one or more tables (called relations) and produces a new table as a result.

Letโ€™s start with the most basic operations.


๐Ÿ” 1. Selection (Filtering Rows)

Think of this as filtering specific rows from a table based on a condition.

Table: Players

PlayerIDNameAgeMembershipStatus
101Sam32Active
102Jordan29Inactive
103Taylor41Active

Goal: Get all active players

Result:

PlayerIDNameAgeMembershipStatus
101Sam32Active
103Taylor41Active

SQL Equivalent:

SELECT * FROM Players WHERE MembershipStatus = ‘Active’;


๐Ÿ“ข 2. Projection (Selecting Columns)

This operation lets you focus on specific columns from a table.

Goal: Show only player names and ages

Result:

NameAge
Sam32
Jordan29
Taylor41

SQL Equivalent:

SELECT Name, Age FROM Players;


๐ŸŸข 3. Union (Merging Unique Rows from Two Tables)

Combines all rows from two tables and removes duplicates.

ClubA_Players:

Name
Sam
Taylor

ClubB_Players:

Name
Jordan
Taylor

Result:

Name
Sam
Taylor
Jordan

SQL Equivalent:

SELECT * FROM ClubA_Players

UNION

SELECT * FROM ClubB_Players;


๐Ÿ”ด 4. Set Difference (Whatโ€™s in One Table but Not the Other)

Returns rows in the first table that are not present in the second.

Registered_2024:

Name
Sam
Taylor

Registered_2025:

Name
Taylor

Result:

Name
Sam

SQL Equivalent:

SELECT * FROM Registered_2024

EXCEPT

SELECT * FROM Registered_2025;


๐Ÿ”ต 5. Intersection (Shared Rows)

Returns only the rows that appear in both tables.

ClubA_Players:

Name
Sam
Taylor

ClubB_Players:

Name
Jordan
Taylor

Result:

Name
Taylor

SQL Equivalent:

SELECT * FROM ClubA_Players

INTERSECT

SELECT * FROM ClubB_Players;


๐Ÿ”— 6. Cartesian Product (All Combinations of Rows)

Combines every row from one table with every row from another.

Players:

PlayerIDName
101Sam
102Jordan

Matches:

MatchIDCourt
201CourtA
202CourtB

Result:

PlayerIDNameMatchIDCourt
101Sam201CourtA
101Sam202CourtB
102Jordan201CourtA
102Jordan202CourtB

SQL Equivalent:

SELECT * FROM Players, Matches;


๐Ÿ”€ Section 2: Introducing Joins

๐Ÿ“˜ A join connects related rows from two tablesโ€”usually using a primary key and a foreign key.

Letโ€™s define both terms before proceeding:

  • Primary Key: A column (or set of columns) that uniquely identifies each row in a table.
  • Foreign Key: A column in one table that refers to the primary key in another table, creating a relationship between the two.

Joins are built on a Cartesian product plus a filter condition.

Letโ€™s say we want player names and match dates:

Players Table:

PlayerIDName
101Sam
102Jordan

Matches Table:

MatchIDPlayer1IDDate
2011012024-06-10
2021022024-06-11

Here:

  • PlayerID in Players is a primary key
  • Player1ID in Matches is a foreign key referring to PlayerID

Goal: Match player names to their match dates

Result:

NameDate
Sam2024-06-10
Jordan2024-06-11

SQL Equivalent:

SELECT Name, Date

FROM Players JOIN Matches

ON Players.PlayerID = Matches.Player1ID;


๐Ÿšฆ Section 3: Why Query Order and Structure Matter

Even though the result of a query is the same, the order of operations can affect performance:

Example:

— Slow version

SELECT * FROM Players JOIN Matches ON … WHERE Age > 40;

— Faster version

SELECT * FROM (SELECT * FROM Players WHERE Age > 40) AS p

JOIN Matches ON p.PlayerID = Matches.Player1ID;

Filtering early reduces how much data is joined.

๐Ÿ“˜ This is called query optimization. You donโ€™t need to master it nowโ€”but keep it in mind as queries get more complex.


โœ… Self-Check
  1. What does filtering rows look like in SQL?
  2. What does the union operation do?
  3. What kind of operation combines every row from two tables?
  4. Why might filtering first be faster than joining first?
  5. What is the difference between a primary key and a foreign key?

๐Ÿ“˜ Glossary
TermDefinition
Relational AlgebraA system of operations used to manipulate relational tables
SelectionFilters rows based on a condition
ProjectionSelects specific columns
Cartesian ProductCombines every row from two tables
JoinCombines rows from two tables based on related keys
UnionCombines results from two tables, removing duplicates
IntersectionReturns only rows that appear in both tables
DifferenceReturns rows that appear in one table but not the other
Primary KeyA column that uniquely identifies each row in a table
Foreign KeyA column that refers to a primary key in another table

Module 3: Algebra & Querying – PDF Version

Scroll to Top