![]() |
VOOZH | about |
Division in SQL is typically required when you want to find out entities that are interacting with all entities of a set of different types of entities. The division operator is used when we have to evaluate queries that contain the keyword 'all'.
You typically require the division operator in scenarios where you need to ascertain entities that meet all specified conditions across different datasets. Here are a few common examples:
These use cases involve checking against all elements of a subset, thereby necessitating a division-like operation.
In all these queries, the description after the keyword 'all' defines a set which contains some elements and the final result contains those units who satisfy these requirements.
Given two relations(tables): R(x,y), S(y).
R(x,y) div S(y) means gives all distinct values of x from R that are associated with all values of y in S.
Computation of Division: R(x,y) div S(y)
Steps:
Query:
SELECT * FROM R
WHERE x NOT IN (
SELECT x FROM (
(SELECT x , y FROM (SELECT y FROM S ) AS p CROSS JOIN (SELECT DISTINCT x FROM R) AS sp)
EXCEPT
(SELECT x , y FROM R)
)
AS r
);
SELECT * FROM R AS sx
WHERE NOT EXISTS (
(SELECT p.y FROM S AS p )
EXCEPT
(SELECT sp.y FROM R AS sp WHERE sp.x = sx.x )
);
In relational algebra, division can be achieved through a series of steps involving projection, Cartesian product, and set difference operations. Here’s how it can be structured:
1. Generate All Combinations: Compute the Cartesian product of all possible y values in S with distinct x values in R.
r1 ← πx(R) x S
2. Identify Incomplete Combinations: Subtract the actual dataset R from these combinations to find x values not associated with every y.
r2x ← πx(r1-R)
and,
3. Derive Result: Subtract the identified x values from all x values to get those associated with every y.
result ← πx(R)-r2x
R div S = πx(R)- πx((πx(R) x S) – R)
Let us look at some of the examples of SQL DIVISION.
Here,
Implementation 1: Using Cross Join and EXCEPT method we get,
Query:
SELECT * FROM suppliers
WHERE sid NOT IN (
SELECT sid FROM (
(SELECT sid, pid FROM (SELECT pid FROM parts) AS p CROSS JOIN (SELECT distinct sid FROM supplies) AS sp)
EXCEPT
(SELECT sid, pid FROM supplies))
AS r
);
Implementation 2: Using Correlated Subquery and NOT EXISTS we get,
Query:
SELECT * FROM suppliers AS s
WHERE NOT EXISTS (
( SELECT p.pid FROM parts AS p )
EXCEPT
(SELECT sp.pid FROM supplies sp WHERE sp.sid = s.sid )
);
Implementation 1: Using Cross Join and EXCEPT method we get,
SELECT * FROM employee AS e
WHERE ssn NOT IN (
SELECT essn FROM (
(SELECT essn, pno FROM (SELECT pno FROM project where dno=4) AS p CROSS JOIN (SELECT distinct essn FROM works_on) AS w)
EXCEPT (SELECT essn, pno FROM works_on)
)
AS r
);
Implementation 2: Using Correlated Subquery and NOT EXISTS we get,
SELECT * FROM employee AS e
WHERE NOT EXISTS (
(SELECT pno FROM project WHERE dno = 4)
EXCEPT
(SELECT pno FROM works_on WHERE essn = e.ssn)
);
Some more Examples:
- Division is not supported by SQL implementations. However, it can be represented using other operations.(like cross join, Except, In )
- For division correlated query seems simpler to write but may expensive to execute.
- When implementing division-like operations in SQL, especially using methods involving multiple joins or subqueries, scalability can become a significant issue.