Introduction
Understanding relationships helps you connect data from different tables. This makes your data useful and meaningful.
Jump into concepts and practice - no test required
Understanding relationships helps you connect data from different tables. This makes your data useful and meaningful.
SELECT columns FROM table1 JOIN table2 ON table1.common_column = table2.common_column;
SELECT Customers.Name, Orders.OrderID FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
SELECT Employees.Name, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This example creates two tables, Customers and Orders, inserts data, and then shows which products each customer bought by joining on CustomerID.
CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, Name VARCHAR(50) ); CREATE TABLE Orders ( OrderID INT PRIMARY KEY, CustomerID INT, Product VARCHAR(50) ); INSERT INTO Customers VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO Orders VALUES (101, 1, 'Book'), (102, 2, 'Pen'), (103, 1, 'Notebook'); SELECT Customers.Name, Orders.Product FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY Customers.Name;
Relationships let you avoid repeating data in many tables.
Always use the correct columns to join tables to get accurate results.
Relationships connect data across tables.
JOINs use common columns to link tables.
Understanding relationships helps answer real questions from data.
Employees(emp_id, name, dept_id)Departments(dept_id, dept_name)SELECT name, dept_name FROM Employees JOIN Departments ON Employees.dept_id = Departments.dept_id;
SELECT name, dept_name FROM Employees JOIN Departments WHERE Employees.dept_id = Departments.dept_id;
Orders(order_id, customer_id, product_id)Customers(customer_id, customer_name)Products(product_id, product_name)