Management & CommerceVery hardBusiness analyst · SQL test

Table p(id, cat) has ids 1, 2, 3 in category X, ids 4, 5 in Y and id 6 in Z. How many rows does SELECT * FROM p a JOIN p b ON a.cat = b.cat AND a.id < b.id return?

  1. A. 8
  2. B. 4
  3. C. 6
  4. D. 14

Correct answer: B. 4

Each unordered pair within a category appears once: X has 3 pairs, Y has 1, Z none – 4 in total. Using a.id <> b.id counts each pair twice (8); with no id condition, each row also pairs with itself (14).

More Operations, Supply Chain & Business Analytics questions

Exams that ask this topic