By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
You’re analyzing sales data, and your boss asks: "Show me every possible combination of product and region—even if no sales exist yet—so we can spot untapped markets."
Or you’re debugging a user activity log, and you need to: "Find all users who logged in on the same day as another user, but from a different country."
These are not standard INNER JOIN or LEFT JOIN problems. They require CROSS JOIN (for exhaustive combinations) and Self-Join (for comparing rows within the same table).
INNER JOIN
LEFT JOIN
What breaks if you ignore this?- You’ll write convoluted loops in Python/Pandas instead of a single SQL query.- You’ll miss edge cases (e.g., unsold products in a region) because you didn’t generate all combinations.- You’ll struggle with recursive relationships (e.g., employee-manager hierarchies) without Self-Join.
sql SELECT a.column1, b.column2 FROM table_a a CROSS JOIN table_b b;
sql SELECT a.employee_name, b.manager_name FROM employees a JOIN employees b ON a.manager_id = b.employee_id;
CROSS JOIN
FROM employees e
WHERE
LIMIT
employees.manager_id
employees.employee_id
products
regions
employees
Self-Join
Scenario: You need to analyze sales potential for every product in every region, even if no sales exist yet.
-- Products table CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(100) ); -- Regions table CREATE TABLE regions ( region_id INT PRIMARY KEY, region_name VARCHAR(100) ); -- Insert sample data INSERT INTO products VALUES (1, 'Laptop'), (2, 'Phone'), (3, 'Tablet'); INSERT INTO regions VALUES (1, 'North'), (2, 'South'), (3, 'East'), (4, 'West');
SELECT p.product_name, r.region_name FROM products p CROSS JOIN regions r;
Verification:- Count rows: SELECT COUNT(*) FROM products × SELECT COUNT(*) FROM regions = 3 × 4 = 12 rows.- If you get fewer, you accidentally used INNER JOIN instead of CROSS JOIN.
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM regions
-- Only show combinations where product_id + region_id is even (arbitrary filter) SELECT p.product_name, r.region_name FROM products p CROSS JOIN regions r WHERE (p.product_id + r.region_id) % 2 = 0;
Scenario: You need to generate a report of every employee and their manager.
CREATE TABLE employees ( employee_id INT PRIMARY KEY, employee_name VARCHAR(100), manager_id INT, FOREIGN KEY (manager_id) REFERENCES employees(employee_id) ); -- Insert sample data (manager_id is NULL for the CEO) INSERT INTO employees VALUES (1, 'Alice (CEO)', NULL), (2, 'Bob', 1), (3, 'Charlie', 1), (4, 'David', 2), (5, 'Eve', 2);
SELECT e.employee_name AS employee, m.employee_name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.employee_id;
Verification:- The CEO (Alice) has no manager (NULL).- Everyone else correctly points to their manager.
NULL
-- Find all pairs of employees who share the same manager SELECT e1.employee_name AS employee1, e2.employee_name AS employee2, m.employee_name AS shared_manager FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id JOIN employees m ON e1.manager_id = m.employee_id WHERE e1.employee_id < e2.employee_id; -- Avoid duplicate pairs (e.g., Bob-Charlie and Charlie-Bob)
e
m
a
b
sql -- Self-Join to find employees with the same manager SELECT e1.name, e2.name, m.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id JOIN employees m ON e1.manager_id = m.employee_id;
employees e1, employees e2
e1.manager_id = e2.employee_id
❌ LEFT JOIN (all rows from left table + matches)
"How do you find employees who report to the same manager?"
manager_id
❌ Recursive CTE (overkill for this case)
"What’s the output of SELECT * FROM table1 CROSS JOIN table2 if table1 has 3 rows and table2 has 4 rows?"
SELECT * FROM table1 CROSS JOIN table2
ON
"You need to generate a report of all product-region pairs, even if no sales exist. Which join do you use?"- ✅ CROSS JOIN (exhaustive combinations).- ❌ LEFT JOIN (only includes products with sales).- ❌ INNER JOIN (only includes products with sales in a region).
Find all pairs of employees who have the same job title.(Assume an employees table with employee_id, name, and job_title.)
employee_id
name
job_title
SELECT e1.name AS employee1, e2.name AS employee2, e1.job_title FROM employees e1 JOIN employees e2 ON e1.job_title = e2.job_title WHERE e1.employee_id < e2.employee_id; -- Avoid duplicates (e.g., Alice-Bob and Bob-Alice)
Why it works:- Self-Join on job_title finds all employees with the same title.- e1.employee_id < e2.employee_id ensures each pair is listed only once.
e1.employee_id < e2.employee_id
FROM table1 CROSS JOIN table2
FROM table1 a JOIN table1 b ON a.id = b.parent_id
WHERE (a.id + b.id) % 2 = 0
WHERE a.id < b.id
LIMIT 1000
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.