09:13

# Exercise 71

Find the PC makers with all personal computer models produced by them being present in the PC table.

Here is the typical incorrect query

Console
Execute
`SELECT DISTINCT makerFROM ProductWHERE model IN (SELECT model FROM PC);`
that arrived at the help desk accompanied by the following question: «Producer E has model 1260 in the PC table, yet the correct result set doesn’t contain him. Why?»

The key point of the task formulation is the word «ALL». Let’s have a look at the models by maker E. PC models produced by maker E are obtained by the following query:

Console
Execute
`SELECT model FROM Product WHERE maker='E' AND type='PC';`

Result:

model
1260
2111
2112

And now let’s check which of these models can be found in the PC table:

Console
Execute
`SELECT DISTINCT model FROM PC WHERE model IN(1260, 2111, 2112);`

As it turns out only one model out of three - 1260 – is present in PC table. According to the task, however, ALL three models should be there for maker E to be displayed.

Basically, the task solution boils down to relational division; however, each maker has his own divisor (the set of models by him). The simplified representation of relational division looks as follows:

`A(a, b) DIVIDEBY B(b)`

Here, the dividend (À) is a binary relation (with two attributes), and the divisor (B) is a unary one. The quotient contains the values of the first attribute in the dividend for which the second attribute has ALL of the values in the divisor.

Relational division doesn’t belong to primitive operations. This means it can be expressed through other (primitive) relational operations. The redundancy of relational algebra introduced by Codd is induced by its focus on practical application. SQL  itself is redundant too, which is proved by every exercise at the site that can be solved in different ways. In spite of that, there is no relational division counterpart in SQL. :-)

Finally, here's an example how relational division can be performed using other operations.

`A DIVIDEBY B := A[a] EXCEPT ((A[a] TIMES B) EXCEPT A) [a]`

Here, A[a] is the projection of the relation A onto attribute a; TIMES is the Cartesian product operation. An “interlinear” translation of the right part of the equation into  SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL language could look like this:

Console
Execute
`SELECT a FROM AEXCEPTSELECT a FROM (SELECT A.a, B.b FROM A, BEXCEPTSELECT a,b FROM A) X;`

It’s hardly advisable to use this word-for-word translation as an action guide; there are easier ways to solve this task. I don’t insist though. :-)