Algebra, subqueries and views
The relational algebra under SQL, queries inside queries, CASE expressions, views and set operations.
About 8 hours of study
Learning objectives
- Translate between relational algebra and SQL
- Write scalar, IN, EXISTS and correlated subqueries
- Name steps with WITH
- Use CASE, views and set operations
Lessons
- 1
Relational algebra
The mathematics under SQL: select σ, project π, product ×, join ⋈, union ∪, difference − and rename ρ, each with the SQL that does the same.
- 2
Subqueries and WITH
A query inside a query: compare with a value worked out by another SELECT, test membership with IN and EXISTS, correlate a subquery with each row, and name steps with WITH.
- 3
CASE, views and set operations
Turn values into labels with CASE, save a query as a view, and combine query results with UNION, INTERSECT and EXCEPT.
Module practice
- From algebra to SQLEasy
- Set difference: students not in the clubMedium
- Division: who took every required courseHard
- Above the class averageEasy
- Above their department's averageMedium
- The second-highest salaryHard
- Letter gradesEasy
- A view that keeps up with the dataMedium
- In exactly one of the two teamsHard