Imagine you have two tables: Customers and Orders. Each order belongs to a customer. Why do we need to use a JOIN to get a list of customers with their orders?
Think about how you can see information from both tables together in one list.
JOINs let us combine rows from different tables using a common column, like customer ID, so we can see related information together.
Given these tables:
Customers:
1, Alice
2, Bob
Orders:
101, 1
102, 2
103, 1
What is the result of this query?
SELECT Customers.Name, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID;
INNER JOIN returns rows where the join condition matches in both tables.
The INNER JOIN matches customers with their orders by customer ID, so Alice has orders 101 and 103, Bob has order 102.
Which option contains a syntax error in the JOIN clause?
SELECT * FROM Customers JOIN Orders ON Customers.ID = Orders.CustomerID;
Look for missing keywords or misplaced clauses in the JOIN syntax.
Option C is missing the ON keyword before the join condition, causing a syntax error.
You have two large tables: Employees and Departments. You want to list employees with their department names. Which option is best to improve query speed?
Think about how indexes and selecting fewer columns affect speed.
Using INNER JOIN on indexed columns and selecting only necessary columns reduces data processed and speeds up the query.
Tables:
Students: 1, John
2, Mary
3, Alex
Enrollments: 1, Math
2, Science
Query:
SELECT Students.Name, Enrollments.Course FROM Students INNER JOIN Enrollments ON Students.ID = Enrollments.StudentID;
Why does Alex not appear in the result?
Think about how INNER JOIN works when one table has missing matching rows.
INNER JOIN returns only rows where the join condition matches in both tables. Alex has no matching enrollment, so is excluded.