Quantifiers in Relational Calculus
Existential Quantifier (∃)
Read aloud is unavailable in this browser.
Asserting Existence: The Foundation of Database Queries
While the universal quantifier (∀) enables us to assert properties that must hold for all elements, the existential quantifier (∃) addresses an equally fundamental question: Does at least one element satisfy a given condition?
Consider these everyday database queries:
"Are there any customers who placed orders last month?"
"Does a student exist who is enrolled in both CS101 and MATH201?"
"Is there a supplier in California who can deliver within 24 hours?"
Each query asks about the existence of at least one entity satisfying specified conditions. This is the domain of the existential quantifier, denoted ∃ (read as "there exists" or "for some").
The existential quantifier is arguably the more intuitive of the two quantifiers—it directly corresponds to the natural human reasoning pattern of seeking examples. When we want to know if something is possible, we search for an instance. The existential quantifier formalizes this search into precise mathematical logic.
By the end of this page, you will master the mathematical semantics of existential quantification, understand its TRC syntax and evaluation, recognize its direct mapping to SQL's EXISTS clause, explore its relationship with joins and subqueries, and develop fluency in expressing existence conditions in formal query notation.
Mathematical Foundations of Existential Quantification
The existential quantifier, like its universal counterpart, originates from first-order predicate logic. It provides the formal mechanism to assert that a predicate is satisfied by at least one element in the domain of discourse.
Definition and Notation
The existential quantifier ∃ constructs statements asserting that at least one element satisfies a given predicate. The general form is:
∃x P(x)
This is read as: "There exists an x such that P(x)" or "For some x, P(x) holds."
Here:
- ∃ is the existential quantifier symbol (sometimes written as \exists in LaTeX or typed as E in ASCII representations)
- x is a bound variable (quantified variable)
- P(x) is a predicate (a formula containing x that evaluates to true or false)
Semantics: Truth Conditions
The statement ∃x P(x) is true if and only if there exists at least one element x₀ in the domain of discourse such that P(x₀) is true. Conversely, ∃x P(x) is false only when P(x) is false for every element x in the domain.
This creates an important asymmetry (complementary to universal quantification):
- To prove ∃x P(x) true: find just one witness x₀ where P(x₀) holds
- To prove ∃x P(x) false: verify that P(x) fails for all x (potentially infinite work)
This asymmetry has profound computational implications—checking existence can often terminate early upon finding a witness, while refuting existence requires exhaustive verification.
When proving an existential statement, you provide a witness—a specific element that satisfies the predicate. In database terms, a single matching tuple proves existence. Query optimizers exploit this by using index lookups and early termination: once one matching tuple is found, EXISTS can return TRUE immediately without scanning further.
The Empty Domain Case
When the domain of discourse is empty, the existential statement ∃x P(x) is FALSE for any predicate P.
Reasoning: There are no elements to serve as witnesses. No element exists, so no element can satisfy P.
This contrasts directly with universal quantification:
- Empty domain: ∀x P(x) is TRUE (vacuous truth)
- Empty domain: ∃x P(x) is FALSE (no witnesses)
Disjunction Connection
For a finite domain {a₁, a₂, ..., aₙ}, existential quantification is equivalent to a disjunction:
∃x P(x) ≡ P(a₁) ∨ P(a₂) ∨ ... ∨ P(aₙ)
This parallels how universal quantification generalizes conjunction. The existential quantifier generalizes "or" to arbitrary (potentially infinite) domains.
Duality with Universal Quantification
The existential and universal quantifiers are duals of each other, connected by negation:
∃x P(x) ≡ ¬∀x ¬P(x)
"There exists an x satisfying P" is equivalent to "Not all x fail P."
And reciprocally:
∀x P(x) ≡ ¬∃x ¬P(x)
This duality (known as De Morgan's laws for quantifiers) is the theoretical foundation for implementing one quantifier in terms of the other—crucial for SQL's implementation of universal quantification via NOT EXISTS.
| Property | Formal Statement | Explanation |
|---|---|---|
| Truth Condition | ∃x P(x) ≡ ¬∀x ¬P(x) | "Some satisfy P" means "not all fail P" |
| Empty Domain | If Domain = ∅, then ∃x P(x) is FALSE | No elements means no witnesses |
| Disjunction Connection | ∃x P(x) ≡ P(a₁) ∨ P(a₂) ∨ ... ∨ P(aₙ) for finite domain | Existence generalizes disjunction |
| Negation | ¬(∃x P(x)) ≡ ∀x ¬P(x) | "None exists" means "all fail" |
| Distribution over ∨ | ∃x (P(x) ∨ Q(x)) ≡ (∃x P(x)) ∨ (∃x Q(x)) | Existential distributes over disjunction |
Syntax in Tuple Relational Calculus
In Tuple Relational Calculus (TRC), the existential quantifier binds tuple variables that range over relations. Unlike universal quantification, which typically uses implication, existential quantification uses conjunction to specify conditions.
The Conjunction Pattern
The standard syntactic form for existential quantification in TRC is:
∃t (R(t) ∧ P(t))
This reads: "There exists a tuple t in relation R such that P(t) holds."
The conjunction (∧) is used because we want to assert:
- The tuple t is a member of relation R: R(t)
- AND the tuple satisfies the property P: P(t)
Why Conjunction, Not Implication?
For existence, we cannot use implication as we did with ∀. Consider the difference:
∃t (R(t) → P(t)) — This says: "There exists a tuple t such that IF t is in R, THEN P(t)."
This is almost always TRUE! Any tuple t NOT in R makes the implication true (since FALSE → anything is TRUE). We'd be counting all non-R-tuples as witnesses, which is not our intent.
∃t (R(t) ∧ P(t)) — This says: "There exists a tuple t that is in R AND satisfies P."
This correctly requires the witness to actually be in R and satisfy P.
12345678910111213141516171819202122232425
# General syntactic forms of existential quantification in TRC # Form 1: Basic existence in a relation∃t (R(t) ∧ P(t))# Reads: "There exists a tuple t in R satisfying P" # Form 2: With multiple conditions∃t (R(t) ∧ C₁(t) ∧ C₂(t))# Reads: "There exists a tuple t in R satisfying both C₁ and C₂" # Form 3: Explicit domain notation (compact)∃t ∈ R (P(t))# Reads: "There exists a tuple t in R such that P(t)" # Form 4: Relating two tuple variables∃t (R(t) ∧ t.foreignKey = s.primaryKey)# Reads: "There exists a tuple t in R linked to s" # Form 5: Nested existence (common pattern)∃t₁ (R₁(t₁) ∧ ∃t₂ (R₂(t₂) ∧ t₁.id = t₂.refId))# Reads: "There exist related tuples in R₁ and R₂" # Form 6: Existence with attribute constraints∃e (Employee(e) ∧ e.salary > 100000 ∧ e.department = 'Engineering')# Reads: "An engineering employee earning over 100K exists"Never use implication (→) with existential quantification for membership conditions. ∃t (R(t) → P(t)) is semantically wrong for existence queries—it allows tuples outside R to serve as witnesses. Always use conjunction: ∃t (R(t) ∧ P(t)).
Scope and Variable Binding
The scope of an existentially quantified variable extends to the entire formula within the quantifier's parentheses. Inside this scope:
- The variable is bound to the quantifier
- It can be referenced in any sub-formula
- It represents a single tuple for each successful "choice" during evaluation
Example with explicit scope:
∃e [ Employee(e) ∧ e.salary > 50000 ∧ ∃d (Department(d) ∧ d.id = e.deptId ∧ d.location = 'NYC') ]
Here, e is bound by the outer ∃e and d is bound by the inner ∃d. Each has its own scope.
Free Variables and Query Formation
In TRC query expressions of the form {t | P(t)}, the variable t in the result is free (not bound by any quantifier), while any existentially quantified variables inside P are bound.
Example: {e.name | Employee(e) ∧ ∃d (Department(d) ∧ d.id = e.deptId ∧ d.budget > 1000000)}
Here:
- e is free (appears in the result set specification)
- d is bound (quantified by ∃d)
The query returns names of employees whose departments have budgets over 1M.
Semantics and Evaluation Procedure
Understanding how database systems evaluate existentially quantified formulas reveals important optimization opportunities and behavioral characteristics.
Evaluation Algorithm
Given a formula ∃t (R(t) ∧ P(t)) and a database state D, the evaluation proceeds:
function evaluate_existential(R, P, D):
for each tuple t in D[R]: # Iterate tuples in relation R
if evaluate(P, t, D): # Evaluate P with t bound
return TRUE # Witness found!
return FALSE # No witnesses exist
Key observations:
- Early termination: Returns TRUE immediately upon finding first witness
- Worst-case: O(|R|) tuple examinations (when no witnesses exist)
- Best-case: O(1) if first tuple is a witness
- Empty relation: Returns FALSE (no tuples to serve as witnesses)
Evaluation Example
Consider the relation Employee(eid, name, salary, deptId) with tuples:
| eid | name | salary | deptId |
|---|---|---|---|
| E1 | Alice | 75000 | D1 |
| E2 | Bob | 95000 | D2 |
| E3 | Carol | 110000 | D1 |
| E4 | Dave | 65000 | D3 |
Evaluate: ∃e (Employee(e) ∧ e.salary > 100000)
Step-by-step:
- e = (E1, Alice, 75000, D1): 75000 > 100000? → FALSE
- e = (E2, Bob, 95000, D2): 95000 > 100000? → FALSE
- e = (E3, Carol, 110000, D1): 110000 > 100000? → TRUE ✓
- Witness found! → Result: TRUE (stops without checking E4)
Now evaluate: ∃e (Employee(e) ∧ e.salary > 200000)
- e = (E1, Alice, 75000, D1): 75000 > 200000? → FALSE
- e = (E2, Bob, 95000, D2): 95000 > 200000? → FALSE
- e = (E3, Carol, 110000, D1): 110000 > 200000? → FALSE
- e = (E4, Dave, 65000, D3): 65000 > 200000? → FALSE
- All tuples exhausted, no witness → Result: FALSE
| Formula | Domain Tuples | Evaluation Trace | Result |
|---|---|---|---|
| ∃e (Emp(e) ∧ e.sal > 90K) | {e1(75K), e2(95K)} | e1: ✗, e2: ✓ → STOP | TRUE |
| ∃e (Emp(e) ∧ e.sal > 200K) | {e1(75K), e2(95K), e3(110K)} | e1: ✗, e2: ✗, e3: ✗, exhausted | FALSE |
| ∃d (Dept(d) ∧ d.budget < 0) | ∅ (empty relation) | No tuples to check | FALSE |
| ∃e (Emp(e) ∧ e.dept = 'Sales') | {e1(Eng), e2(Sales)} | e1: Eng≠Sales ✗, e2: Sales=Sales ✓ | TRUE |
Modern database systems optimize existence checks using indexes. If the condition P involves indexed attributes, the system can directly probe the index rather than scanning the entire relation. For '∃e (Employee(e) ∧ e.deptId = 'D1')', an index on deptId allows immediate lookup—returning TRUE without scanning if the index entry exists.
Correlated vs. Non-Correlated Existence
Existential quantification often appears in contexts where the bound variable references free variables from an outer scope. This correlation affects evaluation strategy:
Non-correlated: ∃e (Employee(e) ∧ e.salary > 100000)
- The condition depends only on e
- Can be evaluated once for the entire query
- Result is cached and reused
Correlated: ∃p (Project(p) ∧ p.managerEid = e.eid)
- The condition references outer variable e (an employee)
- Must be re-evaluated for each binding of e
- More expensive but unavoidable for many queries
Nested Existential Quantification
When existential quantifiers are nested, evaluation follows the standard nesting pattern:
∃x (A(x) ∧ ∃y (B(y) ∧ C(x, y)))
Evaluation:
for each x in domain_x:
if A(x):
for each y in domain_y:
if B(y) AND C(x, y):
return TRUE # Found witnesses x and y
return FALSE
The outer quantifier finds candidate x values, and for each, the inner quantifier searches for a corresponding y.
Common Existential Quantification Patterns
Existential quantification appears in numerous database query patterns. Recognizing these patterns enables rapid query formulation.
Pattern 1: Simple Existence Check
Natural language: "Does any element in S satisfy property P?"
TRC form: ∃t (S(t) ∧ P(t))
Examples:
- Any product under $10? ∃p (Product(p) ∧ p.price < 10)
- Any employee in NYC? ∃e (Employee(e) ∧ e.city = 'NYC')
- Any order today? ∃o (Order(o) ∧ o.date = TODAY)
Pattern 2: Relationship Existence (Join Condition)
Natural language: "Find elements of A that have a corresponding element in B"
TRC form: {a | A(a) ∧ ∃b (B(b) ∧ link(a, b))}
This is the existential join—selecting A-elements that have matching B-elements.
Examples:
-
Customers with orders: {c | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id)}
-
Products that have been reviewed: {p | Product(p) ∧ ∃r (Review(r) ∧ r.productId = p.id)}
Pattern 3: Multi-Hop Relationships (Chained Existence)
Natural language: "Find A that relates to C through intermediate B"
TRC form: {a | A(a) ∧ ∃b (B(b) ∧ link₁(a, b) ∧ ∃c (C(c) ∧ link₂(b, c)))}
Examples:
- Students taking courses in the CS department: {s | Student(s) ∧ ∃e (Enrollment(e) ∧ e.sid = s.sid ∧ ∃c (Course(c) ∧ c.cid = e.cid ∧ c.deptId = 'CS'))}
1234567891011121314151617181920212223242526
# Pattern 4: Existence with Aggregation-like Conditions# "Customers who have placed at least one order this year"{ c.name | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id ∧ o.year = 2024) } # Pattern 5: Conditional Existence (If-Then via ∃)# "Employees who work in a department with budget > 1M"{ e.name | Employee(e) ∧ ∃d (Department(d) ∧ d.id = e.deptId ∧ d.budget > 1000000) } # Pattern 6: Self-Join via Existence# "Employees who share a department with someone named 'Alice'"{ e.name | Employee(e) ∧ e.name ≠ 'Alice' ∧ ∃a (Employee(a) ∧ a.name = 'Alice' ∧ a.deptId = e.deptId) } # Pattern 7: Existence with Comparison# "Products cheaper than at least one product in category 'Electronics'"{ p.name | Product(p) ∧ p.category ≠ 'Electronics' ∧ ∃e (Product(e) ∧ e.category = 'Electronics' ∧ p.price < e.price) } # Pattern 8: Triple Existence (Three-Way Relationship)# "Instructors who teach courses taken by CS students"{ i.name | Instructor(i) ∧ ∃t (Teaches(t) ∧ t.iid = i.iid ∧ ∃e (Enrollment(e) ∧ e.cid = t.cid ∧ ∃s (Student(s) ∧ s.sid = e.sid ∧ s.major = 'CS'))) }Words like "some", "any", "at least one", "has", "have", "exists" in natural language signal existential quantification. The key questions are: (1) What is being sought? (2) What conditions must the witness satisfy? (3) How does the witness relate to the outer query variables?
Pattern 9: Negated Existence (Anti-Pattern)
Natural language: "Find elements with NO matching related elements"
TRC form: {a | A(a) ∧ ¬∃b (B(b) ∧ link(a, b))}
This is the anti-join pattern—selecting elements that have no matches.
Examples:
-
Customers without orders: {c | Customer(c) ∧ ¬∃o (Order(o) ∧ o.custId = c.id)}
-
Products never reviewed: {p | Product(p) ∧ ¬∃r (Review(r) ∧ r.productId = p.id)}
Pattern 10: Multiple Independent Existences
Natural language: "Find elements satisfying multiple independent existence conditions"
TRC form: {a | A(a) ∧ ∃b (B(b) ∧ condB(a, b)) ∧ ∃c (C(c) ∧ condC(a, c))}
Examples:
-
Customers who have ordered AND have reviews: {c | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id) ∧ ∃r (Review(r) ∧ r.custId = c.id)}
-
Employees who manage projects AND supervise interns: {e | Employee(e) ∧ ∃p (Project(p) ∧ p.manager = e.id) ∧ ∃i (Intern(i) ∧ i.supervisor = e.id)}
Connection to SQL: The EXISTS Clause
Unlike universal quantification, existential quantification has a direct correspondence in SQL through the EXISTS clause. This makes translating TRC existential queries to SQL straightforward.
The EXISTS Clause
SQL's EXISTS is a Boolean predicate that returns TRUE if and only if the subquery produces at least one row:
EXISTS (SELECT ... FROM ... WHERE ...)
The TRC pattern ∃t (R(t) ∧ P(t)) maps directly to:
EXISTS (SELECT 1 FROM R t WHERE P)
Direct Translation Examples
TRC: ∃e (Employee(e) ∧ e.salary > 100000) SQL: EXISTS (SELECT 1 FROM Employee e WHERE e.salary > 100000)
TRC: {c | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id)} SQL:
SELECT c.*
FROM Customer c
WHERE EXISTS (
SELECT 1 FROM Order o WHERE o.custId = c.id
);
123456789101112131415161718192021222324252627282930313233343536373839404142
-- Pattern 1: Simple existence check-- TRC: ∃p (Product(p) ∧ p.price < 10)SELECT CASE WHEN EXISTS ( SELECT 1 FROM Product p WHERE p.price < 10) THEN 'Yes' ELSE 'No' END AS cheap_products_exist; -- Pattern 2: Customers who have placed orders-- TRC: {c | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id)}SELECT c.id, c.nameFROM Customer cWHERE EXISTS ( SELECT 1 FROM Order o WHERE o.custId = c.id); -- Pattern 3: Multi-hop relationship-- TRC: Students in courses taught by professors named 'Smith'SELECT DISTINCT s.sid, s.nameFROM Student sWHERE EXISTS ( SELECT 1 FROM Enrollment e WHERE e.sid = s.sid AND EXISTS ( SELECT 1 FROM Teaches t JOIN Instructor i ON t.iid = i.iid WHERE t.cid = e.cid AND i.name = 'Smith' )); -- Pattern 4: Negated existence (anti-join)-- TRC: {c | Customer(c) ∧ ¬∃o (Order(o) ∧ o.custId = c.id)}SELECT c.id, c.nameFROM Customer cWHERE NOT EXISTS ( SELECT 1 FROM Order o WHERE o.custId = c.id); -- Pattern 5: Multiple independent existences-- TRC: Customers who have ordered AND reviewedSELECT c.id, c.nameFROM Customer cWHERE EXISTS (SELECT 1 FROM Order o WHERE o.custId = c.id) AND EXISTS (SELECT 1 FROM Review r WHERE r.custId = c.id);While EXISTS directly implements existential quantification, equivalent results can sometimes be achieved with IN or JOIN. EXISTS (SELECT 1 FROM R WHERE R.fk = T.pk) is semantically equivalent to T.pk IN (SELECT fk FROM R) and to an inner join. Modern optimizers often transform between these forms for efficiency, but EXISTS most closely represents the logical intent of existence checking.
IN as Implicit Existence
The IN clause implicitly performs existence checking:
SELECT * FROM Customer c
WHERE c.id IN (SELECT o.custId FROM Order o)
This is logically equivalent to:
SELECT * FROM Customer c
WHERE EXISTS (SELECT 1 FROM Order o WHERE o.custId = c.id)
Both express: {c | Customer(c) ∧ ∃o (Order(o) ∧ o.custId = c.id)}
JOIN as Existence with Selection
Inner joins implicitly filter to rows where matches exist:
SELECT c.id, c.name, o.orderId
FROM Customer c
JOIN Order o ON c.id = o.custId
This includes the matching order data. If you only want customers (not the order details), EXISTS is cleaner. If you need columns from both tables, JOIN is appropriate.
Performance Considerations
- EXISTS can short-circuit: once a match is found, it returns TRUE without scanning further
- IN with subqueries may materialize the entire subquery result before checking membership
- JOIN returns all matching combinations (which may duplicate outer rows)
- Modern optimizers often transform between these forms for optimal execution
The "best" approach depends on:
- Query structure and complexity
- Available indexes
- Data distribution
- Optimizer capabilities
| TRC Pattern | SQL Pattern | Alternative SQL |
|---|---|---|
| ∃t (R(t) ∧ P(t)) | EXISTS (SELECT 1 FROM R t WHERE P) | SELECT 1 FROM R WHERE P LIMIT 1 |
| {a | A(a) ∧ ∃b (B(b) ∧ link(a,b))} | SELECT * FROM A WHERE EXISTS (SELECT 1 FROM B WHERE link) | SELECT * FROM A WHERE A.fk IN (SELECT pk FROM B) |
| ¬∃t (R(t) ∧ P(t)) | NOT EXISTS (SELECT 1 FROM R t WHERE P) | NOT IN (with NULL caution) |
| ∃x ∃y (R(x) ∧ S(y) ∧ C(x,y)) | EXISTS (SELECT 1 FROM R, S WHERE C) | Nested EXISTS if correlated |
Properties and Logical Equivalences
Understanding the logical properties of existential quantification enables query optimization and algebraic manipulation.
Fundamental Equivalences
1. Quantifier Duality
∃x P(x) ≡ ¬∀x ¬P(x)
"Some x satisfies P" ≡ "Not all x fail P"
This duality allows converting between quantifier types.
2. Negation of Existential
¬(∃x P(x)) ≡ ∀x ¬P(x)
"There is no x satisfying P" ≡ "All x fail P"
This is the logical basis for anti-joins and NOT EXISTS patterns.
3. Distribution over Disjunction
∃x (P(x) ∨ Q(x)) ≡ (∃x P(x)) ∨ (∃x Q(x))
Existential quantifier distributes into disjunction—we can check each disjunct separately.
4. Non-Distribution over Conjunction
∃x (P(x) ∧ Q(x)) ≢ (∃x P(x)) ∧ (∃x Q(x))
Caution: Existing does NOT distribute over conjunction. "Some x satisfies P and Q" doesn't mean "Some satisfies P" and "Some satisfies Q" (they could be different x's).
Counterexample: Domain {a, b} where P(a), Q(b), ¬P(b), ¬Q(a)
- ∃x (P(x) ∧ Q(x)): FALSE (no single x satisfies both P and Q)
- (∃x P(x)) ∧ (∃x Q(x)): TRUE (a satisfies P, b satisfies Q)
- •Quantifier Duality: ∃x P(x) ≡ ¬∀x ¬P(x) — Foundation for NOT EXISTS implementation of ∀
- •Double Negation: ∃x P(x) ≡ ¬¬∃x P(x) — Standard double negation elimination
- •Disjunctive Expansion: ∃x (P(x) ∨ Q(x)) ≡ (∃x P(x)) ∨ (∃x Q(x)) — Check disjuncts separately
- •Vacuous Quantification: ∃x P (where x not free in P) ≡ P ∧ (domain ≠ ∅) — Quantifying unused variables
- •Constant Extraction: ∃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) — Existential quantifiers commute
While ∃x ∃y and ∃y ∃x are equivalent (existential quantifiers commute), mixing with universal quantifiers changes meaning. ∃x ∀y P(x,y) means 'there exists an x that works for all y' (one x satisfies everyone), while ∀y ∃x P(x,y) means 'for each y, there's some x' (different x's for different y's). The difference is fundamental.
Skolemization and Witnesses
In logic, Skolemization is the process of eliminating existential quantifiers by introducing Skolem functions that produce witnesses:
∃x P(x) becomes P(c) where c is a constant witness ∃x ∀y P(x, y) becomes ∀y P(c, y) where c is a constant ∀y ∃x P(x, y) becomes ∀y P(f(y), y) where f is a Skolem function
While not directly used in query evaluation, Skolemization illustrates how existence claims can be thought of as "there is a specific element" claims.
Query Optimization Implications
These equivalences enable optimizers to:
1. Merge Multiple Existences
∃x P(x) ∧ ∃y Q(y) can sometimes be rewritten as a single existence check when relationships allow.
2. Push Existence Checks Down
Moving EXISTS into subqueries closer to relevant tables improves locality.
3. Convert to Joins
When appropriate, ∃ can be converted to semi-joins for efficient execution.
4. Use of Indexes
Rewriting existence checks to use indexed columns enables index-only scans.
Practical Examples with Step-by-Step Solutions
Let's work through detailed examples demonstrating existential quantification from problem statement to SQL implementation.
Example Schema
Student(sid, name, gpa, major, year)
Course(cid, title, credits, deptId)
Enrollment(sid, cid, grade, semester)
Department(deptId, deptName, budget)
Instructor(iid, name, deptId, salary)
Teaches(iid, cid, semester)
Prerequisite(cid, prereqCid)
Example 1: Simple Existence
Query: "Is there any student with a perfect 4.0 GPA?"
TRC: ∃s (Student(s) ∧ s.gpa = 4.0)
SQL:
SELECT CASE WHEN EXISTS (
SELECT 1 FROM Student s WHERE s.gpa = 4.0
) THEN 'Yes' ELSE 'No' END AS perfect_gpa_exists;
Example 2: Existence in Selection
Query: "Find students who have taken at least one course."
TRC: {s | Student(s) ∧ ∃e (Enrollment(e) ∧ e.sid = s.sid)}
SQL:
SELECT s.sid, s.name
FROM Student s
WHERE EXISTS (
SELECT 1 FROM Enrollment e WHERE e.sid = s.sid
);
Example 3: Existence with Specific Condition
Query: "Find courses that have at least one prerequisite."
TRC: {c | Course(c) ∧ ∃p (Prerequisite(p) ∧ p.cid = c.cid)}
SQL:
SELECT c.cid, c.title
FROM Course c
WHERE EXISTS (
SELECT 1 FROM Prerequisite p WHERE p.cid = c.cid
);
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
-- Example 4: Chained Existence (Multi-hop)-- Query: "Find instructors who have taught at least one student majoring in CS" -- TRC: { i.name | Instructor(i) ∧ -- ∃t (Teaches(t) ∧ t.iid = i.iid ∧-- ∃e (Enrollment(e) ∧ e.cid = t.cid ∧-- ∃s (Student(s) ∧ s.sid = e.sid ∧ s.major = 'CS'))) } SELECT DISTINCT i.iid, i.nameFROM Instructor iWHERE EXISTS ( SELECT 1 FROM Teaches t WHERE t.iid = i.iid AND EXISTS ( SELECT 1 FROM Enrollment e WHERE e.cid = t.cid AND EXISTS ( SELECT 1 FROM Student s WHERE s.sid = e.sid AND s.major = 'CS' ) )); -- Flattened version (equivalent, often more efficient):SELECT DISTINCT i.iid, i.nameFROM Instructor iWHERE EXISTS ( SELECT 1 FROM Teaches t JOIN Enrollment e ON e.cid = t.cid JOIN Student s ON s.sid = e.sid WHERE t.iid = i.iid AND s.major = 'CS'); -- Example 5: Negated Existence (Anti-join)-- Query: "Find courses that have never been taught" -- TRC: { c | Course(c) ∧ ¬∃t (Teaches(t) ∧ t.cid = c.cid) } SELECT c.cid, c.titleFROM Course cWHERE NOT EXISTS ( SELECT 1 FROM Teaches t WHERE t.cid = c.cid); -- Example 6: Multiple Independent Existences-- Query: "Find departments that have both instructors and allocated budget > 1M" -- TRC: { d | Department(d) ∧ d.budget > 1000000 ∧-- ∃i (Instructor(i) ∧ i.deptId = d.deptId) } SELECT d.deptId, d.deptNameFROM Department dWHERE d.budget > 1000000AND EXISTS ( SELECT 1 FROM Instructor i WHERE i.deptId = d.deptId);While logically correct, deeply nested EXISTS can be hard to read and may not optimize as well as flattened joins within a single EXISTS. When the nested existences share a common correlation pattern, consider rewriting with JOINs inside a single EXISTS subquery—the optimizer can then apply join optimization techniques.
Example 7: Existence with Aggregation-Like Semantics
Query: "Find students who have passed at least one course (grade ≥ 'C')."
TRC: {s | Student(s) ∧ ∃e (Enrollment(e) ∧ e.sid = s.sid ∧ e.grade ≥ 'C')}
SQL:
SELECT s.sid, s.name
FROM Student s
WHERE EXISTS (
SELECT 1 FROM Enrollment e
WHERE e.sid = s.sid AND e.grade >= 'C'
);
Example 8: Self-Referential Existence
Query: "Find courses that are prerequisites of some other course."
TRC: {c | Course(c) ∧ ∃p (Prerequisite(p) ∧ p.prereqCid = c.cid)}
SQL:
SELECT c.cid, c.title
FROM Course c
WHERE EXISTS (
SELECT 1 FROM Prerequisite p WHERE p.prereqCid = c.cid
);
-- This finds courses that serve AS prerequisites for other courses
Example 9: Existence Combined with Universal (Preview)
Query: "Find students who have taken some course that all CS majors have taken."
This combines ∃ (some course) with an implicit ∀ (all CS majors). We'll explore such combinations in later sections on quantifier interaction.
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.