0
0
PostgreSQLquery~20 mins

CROSS JOIN behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cross Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CROSS JOIN with two small tables
Given two tables Colors and Shapes with the following rows:

Colors:
id | name
1 | Red
2 | Blue

Shapes:
id | name
1 | Circle
2 | Square

What is the output of the query:
SELECT Colors.name AS color, Shapes.name AS shape FROM Colors CROSS JOIN Shapes ORDER BY color, shape;
A[{"color": "Red", "shape": "Circle"}, {"color": "Blue", "shape": "Circle"}, {"color": "Red", "shape": "Square"}, {"color": "Blue", "shape": "Square"}]
B[{"color": "Blue", "shape": "Circle"}, {"color": "Blue", "shape": "Square"}, {"color": "Red", "shape": "Circle"}, {"color": "Red", "shape": "Square"}]
C[{"color": "Red", "shape": "Circle"}, {"color": "Red", "shape": "Square"}, {"color": "Blue", "shape": "Circle"}, {"color": "Blue", "shape": "Square"}]
D[{"color": "Circle", "shape": "Red"}, {"color": "Circle", "shape": "Blue"}, {"color": "Square", "shape": "Red"}, {"color": "Square", "shape": "Blue"}]
Attempts:
2 left
💡 Hint
Remember that CROSS JOIN pairs every row from the first table with every row from the second table.
🧠 Conceptual
intermediate
1:00remaining
Understanding row count after CROSS JOIN
If table A has 5 rows and table B has 3 rows, how many rows will the result have after performing a CROSS JOIN between A and B?
A15 rows
B8 rows
C5 rows
D3 rows
Attempts:
2 left
💡 Hint
Think about how many pairs you can make by combining each row from A with each row from B.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in CROSS JOIN query
Which of the following queries will cause a syntax error in PostgreSQL?
ASELECT * FROM table1 CROSS JOIN;
BSELECT * FROM table1, table2;
CSELECT * FROM table1 CROSS JOIN table2 ON table1.id = table2.id;
DSELECT * FROM table1 CROSS JOIN table2;
Attempts:
2 left
💡 Hint
Check if the CROSS JOIN clause is complete with both table names.
optimization
advanced
2:00remaining
Performance impact of CROSS JOIN on large tables
You have two large tables: Employees with 10,000 rows and Departments with 100 rows. You run a CROSS JOIN between them without any filtering. What is the main performance concern?
AThe query will fail because CROSS JOIN is not allowed on large tables.
BThe query will return 10,000 rows, which is easy to handle.
CThe query will return 100 rows, so performance impact is minimal.
DThe query will return 1,000,000 rows, which can cause high memory and CPU usage.
Attempts:
2 left
💡 Hint
Think about how many combinations are created by pairing every row from both tables.
🔧 Debug
expert
2:30remaining
Why does this CROSS JOIN query return fewer rows than expected?
You have two tables:

Authors: 4 rows
Books: 5 rows

You run this query:
SELECT * FROM Authors CROSS JOIN Books WHERE Authors.id = Books.author_id;

But the result has only 5 rows. Why?
AThe query is invalid because CROSS JOIN cannot have a WHERE clause.
BCROSS JOIN always returns the smaller table's row count, so 5 rows is expected.
CThe WHERE clause filters the CROSS JOIN result to only matching author-book pairs, reducing rows.
DThe database automatically converts CROSS JOIN to INNER JOIN, so only matching rows appear.
Attempts:
2 left
💡 Hint
Consider how WHERE filters affect the Cartesian product result.