I've been trying to solve SQL exercises from http://www.sql-ex.ru/ for some time.
You can find database description below:

Short database description "Computer firm":

The database scheme consists of four tables:
Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)
The table "Product" includes information about the maker, model number, and type ('PC', 'Laptop', or 'Printer'). It is assumed that model numbers in the Product table are unique for all the makers and product types. Each PC uniquely specifying by a code in the table "PC" is characterized by model (foreign key referencing to Product table), speed (of the processor in MHz), total amount of RAM - ram (in Mb), hard disk drive capacity - hd (in Gb), CD ROM speed - cd (for example, '4x'), and the price. The table "Laptop" is similar to that one of PCs except for the CD ROM speed, which is replaced by the screen size - screen (in inches). For each printer in the table "Printer" it is told whether the printer is color or not (color attribute is 'y' for color printers; otherwise it is 'n'), printer type (laser, jet, or matrix), and the price.

Now, there is an exercise:

Find the makers of the PCs that have speed not less than 450MHz. Result set: Maker.

And my solution is:

Select DISTINCT maker from Product, PC where PC.speed>=450 and Product.model=PC.model

It's correct. But I started thinking and I have a problem. I don't know why this solution:

Select DISTINCT maker from Product, PC where PC.speed>=450 and  Product.type='PC'

is not correct. Looks like I still miss some basic knowledge. I'm not sure what this second query does. Any help?

Dani AI

Generated

Good call by — the second statement is doing a Cartesian product because it never ties Product rows to PC rows. In plain terms: you filter Product for type='PC' and you filter PC for speed>=450, but you do not say how a Product row matches a PC row. SQL therefore pairs every filtered Product with every filtered PC, producing combinations that can include makers who do not actually have a matching high-speed PC.

Concrete example: imagine Product has (maker=A, model=M1, type=PC) and (maker=B, model=M2, type=PC), and PC has a single row for model=M2 with speed=500. The incorrect query will combine (M1) with that PC row and return maker A even though A’s model M1 has no PC at 450+.

Correct approaches explicitly express the relationship between tables. Examples:

SELECT DISTINCT p.maker
FROM Product p
JOIN PC pc ON p.model = pc.model
WHERE pc.speed >= 450;
SELECT DISTINCT maker
FROM Product
WHERE model IN (SELECT model FROM PC WHERE speed >= 450);
SELECT DISTINCT p.maker
FROM Product p
WHERE EXISTS (
  SELECT 1 FROM PC pc WHERE pc.model = p.model AND pc.speed >= 450
);

Notes and tips: prefer explicit JOIN syntax to avoid accidental Cartesian products; adding p.type = 'PC' is harmless for clarity but redundant if referential integrity guarantees Product.model points to a PC row; use DISTINCT (or GROUP BY) to remove duplicate maker rows when a model appears multiple times; run EXPLAIN (or the optimizer plan) if results or performance seem surprising.

Recommended Answers

All 2 Replies

Google for "cartesian product".
When you query two tables, the result set is the cartesian product of those two tables, which means, each row of table1 is combined with each row of table2, so you get a result set of n1 * n2 rows.
The query which you propose filters some rows from table1 and some from table2 and builds the cartesian product of those two filtered sets. But it still contains all filtered rows from table1 and all filtered rows from table2 and any combination of them.
To get to a meaningful result you will have to filter the two tables with some common criteria, for example the product model. Now you get a result set which in effect applies all filter criteria to both tables and not only to one.

Yes, the cartesian product was the missing part of puzzle;) Now everything is clear. Thx a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.