Introduction
JOINs let you bring together data from two or more tables that are connected. This helps you see related information in one place.
JOINs let you bring together data from two or more tables that are connected. This helps you see related information in one place.
SELECT columns FROM table1 JOIN table2 ON table1.common_column = table2.common_column;
SELECT customers.name, orders.order_date FROM customers JOIN orders ON customers.id = orders.customer_id;
SELECT employees.name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.id;
This example creates two tables, adds data, and joins them to show customer names with their order dates.
CREATE TABLE customers (id INT, name VARCHAR(20)); CREATE TABLE orders (id INT, customer_id INT, order_date DATE); INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO orders VALUES (101, 1, '2024-01-10'), (102, 2, '2024-01-11'); SELECT customers.name, orders.order_date FROM customers JOIN orders ON customers.id = orders.customer_id;
JOINs only show rows where the related columns match unless you use other JOIN types.
Always check that the columns you join on have matching data types.
JOINs combine rows from related tables using matching columns.
This helps you see connected data in one result.
Use the ON clause to specify how tables relate.
orders and customers on the column customer_id?employees(id, name, dept_id)departments(dept_id, dept_name)SELECT employees.name, departments.dept_name FROM employees JOIN departments ON employees.dept_id = departments.dept_id;
SELECT * FROM orders JOIN customers ON orders.customer_id customers.customer_id;
students(id, name, class_id)classes(class_id, class_name)Pass the test and this pattern moves unknown