I was hit with the following question today during an interview:
How many rows would the following SQL statement return given the two
tables A and B, where both A and B each have exactly 10 rows?:
Select * from A, B;
My answer was the obvious one: 20. However, my interviewer told me that it was allegedly 100, although he said that he didn’t buy that himself. Can anyone shed some light on this?
That query returns a Cartesian product of tables
AandB. Every row in table A will be matched with every row in table B. 10 rows * 10 rows = 100 rows.You were probably interpreting that as a
UNION, where all the rows in table B are appended to the bottom of the rows from table A. That query would look like this:Note that this would only work if the structures of
AandBwere identical.