Quantifiers in Relational Calculus
Universal Quantifier (∀)
Read aloud is unavailable in this browser.
The Power of Asserting Over All
In the realm of database querying, we frequently encounter questions that require us to make assertions about every element in a set. Consider these natural language queries:
"Find students who have passed all their courses."
"List suppliers who can supply every part we need."
"Identify managers who supervise employees in all departments."
These queries share a common logical structure—they require verifying that a condition holds universally across an entire set. This is precisely the domain of the universal quantifier, denoted by the symbol ∀ (read as "for all" or "for every").
The universal quantifier is one of the two fundamental quantifiers in first-order logic and, by extension, in relational calculus. It provides the formal machinery to express and verify conditions that must hold for every element in a defined domain—a capability that distinguishes truly expressive query languages from simple pattern-matching systems.
By the end of this page, you will understand the precise mathematical semantics of the universal quantifier, master its syntax in tuple relational calculus, recognize common patterns where universal quantification applies, trace the connection between ∀ expressions and SQL's NOT EXISTS patterns, and develop intuition for transforming natural language "for all" queries into formal calculus expressions.
Mathematical Foundations of Universal Quantification
Before examining universal quantification in the database context, we must establish its rigorous mathematical foundation. The universal quantifier originates from first-order predicate logic (also called first-order logic or predicate calculus), developed by Gottlob Frege in the late 19th century and formalized by Bertrand Russell, Alfred North Whitehead, and others.
Definition and Notation
The universal quantifier ∀ is used to construct statements asserting that a predicate holds for all elements in a specified domain. The general form is:
∀x P(x)
This is read as: "For all x, P(x) holds" or equivalently "For every x, P(x) is true."
Here:
- ∀ is the universal quantifier symbol
- x is a bound variable (also called a quantified variable)
- P(x) is a predicate (a formula containing x that evaluates to true or false for any given value of x)
Semantics: Truth Conditions
The statement ∀x P(x) is true if and only if P(x) evaluates to true for every element x in the domain of discourse. Conversely, ∀x P(x) is false if there exists even one element x₀ in the domain such that P(x₀) is false.
This asymmetry is crucial:
- To prove ∀x P(x) true: we must verify P(x) for all x (potentially infinite work)
- To prove ∀x P(x) false: we need only find one counterexample x₀ where P(x₀) is false
In database query evaluation, this asymmetry is computationally significant. Verifying universal statements often requires examining all tuples, while refuting them may require finding just one counterexample. Query optimizers exploit this by rewriting ∀ expressions using negation and ∃ (existential quantifier), which we'll explore in depth.
Domain of Discourse
The domain of discourse (or universe of discourse) specifies the set of elements over which the variable x ranges. In standard mathematical logic, this domain is typically declared implicitly or explicitly before formulas are evaluated. In relational calculus:
- For Tuple Relational Calculus (TRC), variables range over tuples in specific relations
- For Domain Relational Calculus (DRC), variables range over domain values (atomic values from attribute domains)
The domain specification is critical because the truth of ∀x P(x) depends entirely on which elements x can take. Consider:
∀x (x² ≥ 0)
- If the domain is real numbers: TRUE (squares of reals are non-negative)
- If the domain includes complex numbers: FALSE (i² = -1 < 0)
The Empty Domain Case
A subtlety arises when the domain is empty. By convention in classical logic:
∀x P(x) is TRUE when the domain is empty
This is called vacuous truth. The reasoning: there are no counterexamples to refute the statement. While this may seem counterintuitive, it prevents logical inconsistencies and has important database implications—a query asking "find employees who have passed all courses" will include employees who have taken no courses (since there's no course they haven't passed).
| Property | Formal Statement | Explanation |
|---|---|---|
| Truth Condition | ∀x P(x) ≡ ¬∃x ¬P(x) | "All satisfy P" means "none fail P" |
| Vacuous Truth | If Domain = ∅, then ∀x P(x) is TRUE | No counterexamples exist in empty sets |
| Conjunction Connection | ∀x P(x) ≡ P(a₁) ∧ P(a₂) ∧ ... ∧ P(aₙ) for finite domain | Universal quantification generalizes conjunction |
| Negation | ¬(∀x P(x)) ≡ ∃x ¬P(x) | Denying "all" creates "at least one not" |
| Distribution over ∧ | ∀x (P(x) ∧ Q(x)) ≡ (∀x P(x)) ∧ (∀x Q(x)) | Universal quantifier distributes over conjunction |
Syntax in Tuple Relational Calculus
In Tuple Relational Calculus (TRC), the universal quantifier binds tuple variables that range over relations. The general syntactic form is:
∀t ∈ R (P(t))
or equivalently (with implicit relation binding):
∀t (R(t) → P(t))
Let's dissect each component and understand how TRC incorporates universal quantification.
Tuple Variables and Relation Binding
A tuple variable in TRC represents an arbitrary tuple. When we write ∀t ∈ R, we declare that t ranges over all tuples in relation R. The formula following the quantifier is then evaluated for each such tuple.
The Implication Pattern
A critical syntactic pattern emerges with universal quantification. When we want to express "for all tuples t in relation R that satisfy condition C, property P holds," the standard form uses implication:
∀t (R(t) → (C(t) → P(t)))
Or more commonly combined:
∀t ((R(t) ∧ C(t)) → P(t))
This reads: "For every tuple t, if t is in R and satisfies C, then t satisfies P."
Why Implication?
The implication (→) is essential because:
- Domain Restriction: We typically care about a subset of the domain (e.g., only tuples in a specific relation)
- Conditional Assertions: Our "for all" claims are usually conditional—"for all that satisfy some prerequisite"
- Avoiding Undefined Behavior: Without implication, we'd assert P for tuples not even in the relation
Consider: "All employees in the Sales department earn more than 50000."
Correct: ∀e (Employee(e) ∧ e.dept = 'Sales' → e.salary > 50000)
Incorrect: ∀e (e.salary > 50000) — This asserts all tuples everywhere have salary > 50000
123456789101112131415161718192021
# General syntactic forms of universal quantification in TRC # Form 1: Explicit relation membership with implication∀t (R(t) → P(t))# Reads: "For all tuples t, if t is in relation R, then P(t) holds" # Form 2: With additional conditions∀t ((R(t) ∧ C(t)) → P(t))# Reads: "For all tuples t in R satisfying C, P(t) holds" # Form 3: Explicit domain notation (compact)∀t ∈ R (P(t))# Reads: "For all tuples t in R, P(t) holds" # Form 4: Nested structure with attribute access∀t (Enrollment(t) → t.grade ≥ 'C')# Reads: "Every enrollment has grade C or better" # Form 5: Multiple conditions with conjunction∀t ((Student(t) ∧ t.year = 'Senior') → t.creditsCompleted ≥ 90)# Reads: "All senior students have completed at least 90 credits"A frequent mistake is using conjunction (∧) instead of implication (→) after the quantifier: ∀t (R(t) ∧ P(t)) means "every tuple in the universe is both in R and satisfies P"—almost always false and not what we intend. Always use implication when the quantifier should apply conditionally to relation members.
Attribute Access and Tuple Structure
When accessing attributes of a tuple variable, standard notation uses dot notation:
- t.attribute_name — accesses the value of attribute_name in tuple t
- t[i] — positional access (less common in TRC)
Scope and Variable Binding
The scope of a universally quantified variable extends to the entire formula following the quantifier (within parentheses). Within this scope:
- The variable is bound to the quantifier
- It cannot be rebound by another quantifier of the same name (shadowing is typically prohibited or requires renaming)
- Any use of the variable refers to the quantified iteration
Example with explicit scope:
∀t [ (Student(t) → t.gpa ≥ 2.0) ]
The brackets show the scope of t—everywhere t appears within, it refers to the tuple bound by ∀t.
Semantics and Evaluation Procedure
Understanding how database systems evaluate universally quantified formulas is essential for writing correct queries and predicting their behavior.
Evaluation Algorithm
Given a formula ∀t ∈ R (P(t)) and a database state D, the evaluation proceeds:
function evaluate_universal(R, P, D):
for each tuple t in D[R]: # Iterate all tuples in relation R
if NOT evaluate(P, t, D): # Evaluate P with t bound
return FALSE # Counterexample found
return TRUE # All tuples satisfied P
This immediately reveals:
- Worst-case complexity: O(|R|) evaluations of P
- Early termination: Stops at first counterexample
- Empty relation: Returns TRUE (vacuous truth)
Evaluation Example
Consider the relation Student(sid, name, gpa, major) with tuples:
| sid | name | gpa | major |
|---|---|---|---|
| S1 | Alice | 3.5 | CS |
| S2 | Bob | 2.8 | Math |
| S3 | Carol | 3.9 | CS |
| S4 | Dave | 2.2 | Physics |
Evaluate: ∀t (Student(t) → t.gpa ≥ 2.0)
Step-by-step:
- t = (S1, Alice, 3.5, CS): 3.5 ≥ 2.0 → TRUE ✓
- t = (S2, Bob, 2.8, Math): 2.8 ≥ 2.0 → TRUE ✓
- t = (S3, Carol, 3.9, CS): 3.9 ≥ 2.0 → TRUE ✓
- t = (S4, Dave, 2.2, Physics): 2.2 ≥ 2.0 → TRUE ✓
- All tuples satisfied → Result: TRUE
Now evaluate: ∀t (Student(t) → t.gpa ≥ 3.0)
- t = (S1, Alice, 3.5, CS): 3.5 ≥ 3.0 → TRUE ✓
- t = (S2, Bob, 2.8, Math): 2.8 ≥ 3.0 → FALSE ✗
- Counterexample found → Result: FALSE
| Formula | Domain Tuples | Evaluation Trace | Result |
|---|---|---|---|
| ∀t (Emp(t) → t.sal > 0) | {e1, e2, e3} where all sal > 0 | e1: ✓, e2: ✓, e3: ✓ | TRUE |
| ∀t (Emp(t) → t.sal > 50000) | {e1(60K), e2(45K), e3(70K)} | e1: ✓, e2: ✗ → STOP | FALSE |
| ∀t (Dept(t) → t.budget ≥ 0) | ∅ (empty relation) | No tuples to check | TRUE (vacuous) |
| ∀t ((Emp(t) ∧ t.rank='Sr') → t.sal > 80K) | {e1(Jr,60K), e2(Sr,90K)} | e1: Jr≠Sr→skip, e2: Sr∧90K>80K✓ | TRUE |
When the antecedent of the implication (the condition before →) is FALSE for a tuple, that tuple does NOT falsify the universal statement. This is because (FALSE → anything) is TRUE in classical logic. This explains why ∀t ((Emp(t) ∧ t.rank='Sr') → t.sal > 80K) correctly ignores non-senior employees—they make the antecedent false, thus the implication true.
The Implication Truth Table
Understanding the implication operator is crucial for reasoning about universal quantification:
| P | Q | P → Q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
Key insight: P → Q is only FALSE when P is TRUE and Q is FALSE. In all other cases, including when P is FALSE, the implication is TRUE.
Practical implications for queries:
- Tuples not matching the antecedent condition automatically satisfy the universal statement
- Only tuples that do match the antecedent can potentially falsify the statement
- This is why "all employees in Sales earn > 50K" correctly ignores non-Sales employees
Nested Quantifier Evaluation
When universal quantifiers are nested or combined with other quantifiers, evaluation becomes more complex. The general principle: outer quantifiers iterate first, and for each iteration, inner quantifiers are fully evaluated.
For ∀x ∀y P(x, y):
for each x in domain_x:
for each y in domain_y:
if NOT P(x, y):
return FALSE
return TRUE
The order matters for mixed quantifiers (∀∃ vs ∃∀), which we'll explore in later sections.
Common Universal Quantification Patterns
Certain query patterns recur frequently in database applications, each naturally expressible using universal quantification. Recognizing these patterns accelerates query formulation.
Pattern 1: Universal Property Verification
Natural language: "All elements in set S satisfy property P"
TRC form: ∀t (S(t) → P(t))
Examples:
- All products have positive price: ∀p (Product(p) → p.price > 0)
- Every employee has an assigned department: ∀e (Employee(e) → e.deptId IS NOT NULL)
- All orders are for existing customers: ∀o (Order(o) → ∃c (Customer(c) ∧ c.id = o.custId))
Pattern 2: Superset/Subset Verification
Natural language: "All elements of A are also in B"
TRC form: ∀t (A(t) → B(t)) — expresses A ⊆ B
Examples:
- All managers are employees: ∀m (Manager(m) → Employee(m))
- All CS majors are students: ∀s ((Student(s) ∧ s.major='CS') → Student(s))
Pattern 3: Universal Relationship (Division-like)
Natural language: "Find entities that are related to ALL entities in another set"
TRC form: {x | Condition(x) ∧ ∀y (Set(y) → Relationship(x, y))}
This is the famous division operation from relational algebra, expressible in calculus via universal quantification.
Examples:
-
Students enrolled in ALL courses: {s.name | Student(s) ∧ ∀c (Course(c) → ∃e (Enrollment(e) ∧ e.sid = s.sid ∧ e.cid = c.cid))}
-
Suppliers who supply ALL parts: {s.sname | Supplier(s) ∧ ∀p (Part(p) → ∃sp (Supply(sp) ∧ sp.sid = s.sid ∧ sp.pid = p.pid))}
1234567891011121314151617181920212223242526
# Pattern 1: Universal Property Verification# "All active employees have completed training"{ } # Returns TRUE/FALSE∀e ((Employee(e) ∧ e.status = 'Active') → ∃t (Training(t) ∧ t.empId = e.id ∧ t.completed = TRUE)) # Pattern 2: Superset Verification # "All premium customers have made at least one purchase"∀c ((Customer(c) ∧ c.tier = 'Premium') → ∃o (Order(o) ∧ o.customerId = c.id)) # Pattern 3: Division - Find entities related to ALL in another set# "Find students who have taken ALL required courses"{ s.name | Student(s) ∧ ∀c ((Course(c) ∧ c.required = TRUE) → ∃e (Enrollment(e) ∧ e.studentId = s.id ∧ e.courseId = c.id)) } # Pattern 4: Universal Constraint Verification# "No department has negative budget" (equivalently: all depts have budget ≥ 0)∀d (Department(d) → d.budget ≥ 0) # Pattern 5: Referential Integrity Check# "Every foreign key references an existing primary key"∀o (Order(o) → ∃c (Customer(c) ∧ c.id = o.customerId))When you see natural language containing "all", "every", "each", "no... fails", or "none... lacks", think universal quantification. The key is identifying: (1) What is the universal set being quantified over? (2) What property must hold for each element? (3) Are there conditions restricting which elements we care about?
Pattern 4: Double Universal (All-All Patterns)
Natural language: "For every X and every Y, some relationship holds"
TRC form: ∀x ∀y ((Condition(x) ∧ Condition(y)) → Relationship(x, y))
Examples:
-
All employees in all departments have positive salary: ∀e ∀d ((Employee(e) ∧ Department(d) ∧ e.deptId = d.id) → e.salary > 0)
-
All pairs of products from the same category have compatible units: ∀p ∀q ((Product(p) ∧ Product(q) ∧ p.category = q.category) → p.unit = q.unit)
Pattern 5: Universal Negation (None Pattern)
Natural language: "No element in S satisfies P" (equivalent to "All elements in S fail P")
TRC form: ∀t (S(t) → ¬P(t)) — equivalently: ¬∃t (S(t) ∧ P(t))
Examples:
- No product has negative price: ∀p (Product(p) → ¬(p.price < 0))
- None of our orders are from invalid regions: ∀o (Order(o) → ¬(o.region = 'INVALID'))
Connection to SQL: The NOT EXISTS Transformation
SQL does not have a direct FOR ALL clause. Instead, universal quantification is expressed through a transformation based on the logical equivalence:
∀x P(x) ≡ ¬∃x ¬P(x)
In words: "All x satisfy P" is equivalent to "There does not exist an x that fails P."
This equivalence is fundamental and is how database systems implement universal quantification.
The Transformation Process
Original TRC query: Find suppliers who supply all parts
{s | Supplier(s) ∧ ∀p (Part(p) → ∃sp (Supply(sp) ∧ sp.sid = s.sid ∧ sp.pid = p.pid))}
Step 1: Identify the universal quantification
- ∀p (Part(p) → ∃sp (...))
Step 2: Apply the equivalence ∀ → ¬∃¬
- ¬∃p ¬(Part(p) → ∃sp (...))
Step 3: Simplify ¬(A → B) = ¬(¬A ∨ B) = (A ∧ ¬B)
- ¬∃p (Part(p) ∧ ¬∃sp (...))
Step 4: Convert to SQL using NOT EXISTS
12345678910111213141516171819202122232425262728
-- Original query: Find suppliers who supply ALL parts-- TRC: {s | Supplier(s) ∧ ∀p (Part(p) → ∃sp (Supply(sp) ∧ sp.sid = s.sid ∧ sp.pid = p.pid))} -- Transformation: ∀ → ¬∃¬-- "For all parts, supplier supplies it" -- BECOMES -- "There is no part that supplier does NOT supply" SELECT s.sid, s.snameFROM Supplier sWHERE NOT EXISTS ( -- "There exists a part..." SELECT 1 FROM Part p WHERE NOT EXISTS ( -- "...that this supplier does NOT supply" SELECT 1 FROM Supply sp WHERE sp.sid = s.sid AND sp.pid = p.pid )); -- Reading this query:-- For each supplier s, check:-- NOT EXISTS a part p such that:-- NOT EXISTS a supply record linking s to p-- Which means:-- There is no part that s fails to supply-- Which means:-- s supplies ALL partsThe NOT EXISTS (... NOT EXISTS ...) pattern is the SQL hallmark of universal quantification. Whenever you see double negation in SQL, you're likely looking at a "for all" query in disguise. This pattern is also called the "relational division" pattern in SQL.
More SQL Examples
Let's trace several universal quantification queries to their SQL equivalents:
Query 1: "Students who passed all their exams"
TRC: {s | Student(s) ∧ ∀e ((Exam(e) ∧ e.sid = s.sid) → e.grade ≥ 60)}
SQL:
SELECT s.sid, s.name
FROM Student s
WHERE NOT EXISTS (
SELECT 1 FROM Exam e
WHERE e.sid = s.sid
AND NOT (e.grade >= 60) -- Failed exam
);
Query 2: "Managers who supervise employees in every department"
TRC: {m | Manager(m) ∧ ∀d (Department(d) → ∃e (Employee(e) ∧ e.manager = m.id ∧ e.deptId = d.id))}
SQL:
SELECT m.id, m.name
FROM Manager m
WHERE NOT EXISTS (
SELECT 1 FROM Department d
WHERE NOT EXISTS (
SELECT 1 FROM Employee e
WHERE e.manager = m.id AND e.deptId = d.id
)
);
Query 3: "Customers who have ordered every product in our catalog"
TRC: {c | Customer(c) ∧ ∀p (Product(p) → ∃o ∃oi (Order(o) ∧ OrderItem(oi) ∧ o.custId = c.id ∧ oi.orderId = o.id ∧ oi.productId = p.id))}
SQL:
SELECT c.id, c.name
FROM Customer c
WHERE NOT EXISTS (
SELECT 1 FROM Product p
WHERE NOT EXISTS (
SELECT 1
FROM Order o
JOIN OrderItem oi ON o.id = oi.orderId
WHERE o.custId = c.id AND oi.productId = p.id
)
);
| TRC Pattern | SQL Pattern | Semantic Meaning |
|---|---|---|
| ∀t (R(t) → P(t)) | NOT EXISTS (SELECT * FROM R WHERE NOT P) | All R-tuples satisfy P |
| ∀t ((R(t) ∧ C(t)) → P(t)) | NOT EXISTS (SELECT * FROM R WHERE C AND NOT P) | All R-tuples satisfying C also satisfy P |
| ∀x ∀y (...) | NOT EXISTS (NOT EXISTS (...)) | Nested universal requires nested NOT EXISTS |
| ∀x (A(x) → ∃y B(x,y)) | NOT EXISTS A WHERE NOT EXISTS B | Division pattern |
Properties and Logical Equivalences
Mastering universal quantification requires understanding its algebraic properties and logical equivalences. These are essential for query optimization and verification.
Fundamental Equivalences
1. Quantifier Duality (De Morgan for Quantifiers)
∀x P(x) ≡ ¬∃x ¬P(x)
This is the central equivalence enabling SQL implementation. Its dual:
∃x P(x) ≡ ¬∀x ¬P(x)
2. Negation of Universal
¬(∀x P(x)) ≡ ∃x ¬P(x)
"It's not the case that all satisfy P" ≡ "At least one fails P"
3. Distribution over Conjunction
∀x (P(x) ∧ Q(x)) ≡ (∀x P(x)) ∧ (∀x Q(x))
Universal quantifier distributes into conjunction—we can check each conjunct separately.
4. Non-Distribution over Disjunction
∀x (P(x) ∨ Q(x)) ≢ (∀x P(x)) ∨ (∀x Q(x))
Caution: Universal does NOT distribute over disjunction. "All are P or Q" doesn't mean "All are P" or "All are Q."
Counterexample: {a, b} where P(a), Q(b), ¬P(b), ¬Q(a)
- ∀x (P(x) ∨ Q(x)): TRUE (a satisfies P, b satisfies Q)
- (∀x P(x)) ∨ (∀x Q(x)): FALSE (not all satisfy P, not all satisfy Q)
- •Quantifier Duality: ∀x P(x) ≡ ¬∃x ¬P(x) — The foundation of SQL implementation
- •Double Negation: ∀x P(x) ≡ ¬¬∀x P(x) ≡ ¬∃x ¬P(x) — Eliminating double negatives
- •Implication Expansion: ∀x (P(x) → Q(x)) ≡ ∀x (¬P(x) ∨ Q(x)) — Used in query simplification
- •Vacuous Quantification: ∀x P (where x not free in P) ≡ P — Quantifying unused variables has no effect
- •Conjunction Distribution: ∀x (P ∧ Q(x)) ≡ P ∧ ∀x Q(x) (when x not free in P) — Pull constants out
- •Quantifier Ordering: ∀x ∀y P(x,y) ≡ ∀y ∀x P(x,y) — Universal quantifiers commute
While ∀x ∀y and ∀y ∀x are equivalent (universal quantifiers commute), ∀x ∃y and ∃y ∀x are NOT equivalent! The order of mixed quantifiers changes meaning dramatically. ∀x ∃y P(x,y) means 'for each x, there exists a y' (different y's for different x's), while ∃y ∀x P(x,y) means 'there exists a single y that works for all x's.'
Query Optimization Implications
These equivalences enable query optimizers to:
1. Push/Pull Quantifiers
Optimizers may push quantifiers into subexpressions or pull them out to find more efficient evaluation orders.
2. Simplify Nested Structures
Using distribution laws to partially evaluate or simplify universal conditions.
3. Exploit Empty Domain Detection
If the optimizer detects an empty domain for a universal quantifier, it can short-circuit to TRUE without evaluation.
4. Transform to Joins
In some cases, universal quantification combined with existence tests can be transformed into more efficient join operations or anti-joins.
The Prenex Normal Form
Any relational calculus formula can be converted to prenex normal form, where all quantifiers appear at the beginning:
Q₁x₁ Q₂x₂ ... Qₙxₙ P(x₁, x₂, ..., xₙ)
Where each Qᵢ is either ∀ or ∃, and P is a quantifier-free matrix.
This standardization aids optimization and theoretical analysis, though practical query optimizers may use more sophisticated representations.
Practical Examples with Step-by-Step Solutions
Let's work through comprehensive examples demonstrating how to formulate universal quantification queries from natural language specifications.
Example Schema
Student(sid, name, gpa, major)
Course(cid, title, credits, deptId)
Enrollment(sid, cid, grade, semester)
Department(deptId, deptName, budget)
Instructor(iid, name, deptId, salary)
Teaches(iid, cid, semester)
Example 1: Simple Universal Property
Query: "Find all students with GPA above 3.0"
Analysis: This is peculiar—it's NOT a universal quantification query despite containing "all". We're not asserting that ALL students have GPA > 3.0; we're selecting students that do.
Correct formulation: {s | Student(s) ∧ s.gpa > 3.0}
Lesson: "Find all X that satisfy P" uses selection, not universal quantification. Universal quantification would be: "Verify that all X satisfy P."
Example 2: Universal Verification
Query: "Do all CS students have GPA above 2.0?"
Analysis: This IS universal quantification—we're verifying a property holds universally.
TRC: ∀s ((Student(s) ∧ s.major = 'CS') → s.gpa > 2.0)
Evaluation:
- Returns TRUE if every CS student has GPA > 2.0
- Returns FALSE if any CS student has GPA ≤ 2.0
Example 3: Division Pattern
Query: "Find students enrolled in all courses offered by the CS department."
Step 1 - Identify components:
- Outer entity: Student
- Universal set: Courses where deptId = 'CS'
- Relationship: Enrollment linking student to course
Step 2 - Formulate: {s.name | Student(s) ∧ ∀c ((Course(c) ∧ c.deptId = 'CS') → ∃e (Enrollment(e) ∧ e.sid = s.sid ∧ e.cid = c.cid))}
Step 3 - Explain: "Return the name of each student s such that for every course c in CS, there exists an enrollment e connecting s to c."
12345678910111213141516171819202122232425262728293031323334353637
# Example 4: Students who passed all their enrolled courses# "Passed" means grade ≥ 'C' { s.name | Student(s) ∧ ∀e ((Enrollment(e) ∧ e.sid = s.sid) → e.grade ≥ 'C') } # Explanation:# For student s, check that every enrollment e belonging to s has grade ≥ C# Students with no enrollments satisfy this vacuously # Example 5: Departments where all instructors earn above 60000 { d.deptName | Department(d) ∧ ∀i ((Instructor(i) ∧ i.deptId = d.deptId) → i.salary > 60000) } # Explanation:# For department d, check that every instructor i in d has salary > 60000# Departments with no instructors satisfy vacuously # Example 6: Instructors who have taught in every semester when courses were offered { i.name | Instructor(i) ∧ ∀s ((∃t Teaches(t) ∧ t.semester = s) → ∃t2 (Teaches(t2) ∧ t2.iid = i.iid ∧ t2.semester = s)) } # Note: This quantifies over semester VALUES, not tuples directly# The inner existence ensures we only consider semesters where courses were taught # Example 7: Courses taken by ALL students (universal on students, not courses) { c.title | Course(c) ∧ ∀s (Student(s) → ∃e (Enrollment(e) ∧ e.sid = s.sid ∧ e.cid = c.cid)) } # Explanation:# For course c, check that every student s has an enrollment in c# Returns courses that are mandatory for all studentsIn Example 4, a student with no enrollments passes the condition vacuously (there's no enrollment to check). Depending on business requirements, you may want to exclude such cases by adding: ∃e (Enrollment(e) ∧ e.sid = s.sid) to ensure the student has at least one enrollment.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.