Skip to content
C

Division


Division

Definition

Division (symbol: ÷) answers questions of the form "which X are related to EVERY Y in a given set?" — e.g. "which students have taken ALL of the required courses?" It's the most conceptually advanced basic relational algebra operation, with no single simple SQL keyword — it must be expressed as a combination of other operations.

How It Works

sql
SELECT student_id FROM enrollments WHERE course_id IN (SELECT course_id FROM required_courses) GROUP BY student_id HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM required_courses);

The logic: for each student, count how many of the REQUIRED courses they've completed (the WHERE ... IN filters enrollments down to only required courses; GROUP BY + COUNT(DISTINCT ...) counts how many distinct required courses each student has). A student only satisfies "has taken ALL required courses" if that count equals the TOTAL number of required courses — exactly what the HAVING clause checks.

Edge Cases and Pitfalls

  • COUNT(DISTINCT course_id), not plain COUNT(course_id), is essential here — without DISTINCT, a student accidentally enrolled twice in the same required course would be miscounted as having satisfied more requirements than they actually have.
  • Division is easy to get subtly wrong: a common mistake is comparing against the student's OWN total enrollment count instead of the required-courses count — that answers a different question ("has this student taken as many courses as required courses exist," which could be true by coincidence without actually being the SAME courses).
  • Division generalizes beyond "has taken all courses" to any "relates to every member of a set" question — e.g. "which suppliers supply every part in a given category," "which drivers have driven every route" — the same WHERE-IN / GROUP BY / HAVING-COUNT-equals-total pattern applies.

Key Takeaways

  • Division answers "which X relates to EVERY Y in a set" — there's no single SQL keyword; it's built from WHERE/GROUP BY/HAVING/COUNT(DISTINCT).
  • The core pattern: filter to the relevant Y's, group by X, and require the distinct count to equal the total size of the Y set.
  • This is one of the genuinely hardest, most-tested relational algebra concepts precisely because it has no direct syntactic shortcut.

Mock Test

  • Division - Quick Test

    8 questions on Division.

    8 questions · 8 min · Medium
    Start Mock Test

Coding Problem

  • Division: Students Who Completed All Required Courses
    Hard · sql
    Solve Problem