Introduction
Joins combine rows from two tables based on related columns to get useful combined information.
Jump into concepts and practice - no test required
SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;
SELECT * FROM customers JOIN orders ON customers.id = orders.customer_id;
SELECT * FROM employees JOIN departments ON employees.dept_id = departments.id;
CREATE TABLE students (id INT, name TEXT); CREATE TABLE scores (student_id INT, score INT); CREATE INDEX scores_student_id_idx ON scores(student_id); INSERT INTO students VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'); INSERT INTO scores VALUES (1, 85), (2, 90), (4, 75); EXPLAIN ANALYZE SELECT students.name, scores.score FROM students JOIN scores ON students.id = scores.student_id;
employees(emp_id, dept_id) and departments(dept_id, name), what join algorithm will PostgreSQL most likely use for this query?EXPLAIN SELECT * FROM employees JOIN departments ON employees.dept_id = departments.dept_id;Assuming both tables are large and
departments.dept_id is indexed.SELECT * FROM orders o JOIN customers c ON o.customer_id = c.customer_id;But PostgreSQL is using a Nested Loop Join causing slow performance. Which fix will most likely improve performance by enabling a better join algorithm?
sales(date, product_id, amount) and products(product_id, name). You want to join them on product_id efficiently. Which join algorithm should you prefer and why?