Sobes.tech
Junior — Senior

Order of applying filters and joins in an SQL query and the occurrence of a Cartesian product

livecode

Task condition

In the database query, the JOIN operator is used to combine the tables table1 and table2, as well as the WHERE condition to select the required records.

  1. Which step occurs first: applying the WHERE condition or executing the JOIN?
  2. For what reason can a Cartesian product occur when using JOIN, where each row of the first table is joined with all rows of the second?
  3. What techniques can prevent the appearance of a Cartesian product when joining tables?
SELECT * FROM table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.key_id
WHERE t1.id > 100;