We use INNER JOIN with table aliases to combine rows from two tables based on a related column, making queries shorter and easier to read.
INNER JOIN with table aliases in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
SELECT alias1.column1, alias2.column2 FROM table1 AS alias1 INNER JOIN table2 AS alias2 ON alias1.common_column = alias2.common_column;
Use AS to give a short name (alias) to tables.
The ON clause defines how the tables are related.
Examples
c and o.SQL
SELECT c.name, o.order_date FROM customers AS c INNER JOIN orders AS o ON c.customer_id = o.customer_id;
e and d are used without AS keyword to join employees and departments.SQL
SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
p and c to get product and category names.SQL
SELECT p.product_name, c.category_name FROM products p INNER JOIN categories c ON p.category_id = c.category_id;
Sample Program
This creates two tables, inserts data, and selects customer names with their order dates using INNER JOIN and aliases.
SQL
CREATE TABLE customers ( customer_id INT, name VARCHAR(50) ); CREATE TABLE orders ( order_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'), (103, 1, '2024-01-12'); SELECT c.name, o.order_date FROM customers AS c INNER JOIN orders AS o ON c.customer_id = o.customer_id ORDER BY o.order_date;
Important Notes
Table aliases make queries shorter and easier to read.
INNER JOIN returns only rows where there is a match in both tables.
Always use the alias when referring to columns after aliasing tables.
Summary
INNER JOIN combines rows from two tables where keys match.
Table aliases are short names for tables to simplify queries.
Use ON to specify how tables relate when joining.
Practice
1. What does an
INNER JOIN with table aliases do in SQL?easy
Solution
Step 1: Understand INNER JOIN purpose
INNER JOIN returns rows where matching keys exist in both tables.Step 2: Role of table aliases
Aliases are short names to simplify table references in queries.Final Answer:
Combines rows from two tables where the join condition matches, using short names for tables. -> Option DQuick Check:
INNER JOIN + aliases = matched rows with short table names [OK]
Hint: INNER JOIN matches rows; aliases shorten table names [OK]
Common Mistakes:
- Confusing INNER JOIN with DELETE or UPDATE
- Thinking aliases create new tables
- Ignoring the join condition in INNER JOIN
2. Which of the following is the correct syntax for an INNER JOIN with table aliases?
easy
Solution
Step 1: Check INNER JOIN syntax
Correct syntax uses INNER JOIN with ON clause for join condition.Step 2: Validate alias usage and condition
Aliases 'a' and 'b' are used correctly; ON clause uses single '=' for comparison.Final Answer:
SELECT a.name, b.salary FROM employees a INNER JOIN salaries b ON a.id = b.emp_id; -> Option AQuick Check:
INNER JOIN + ON + aliases = correct syntax [OK]
Hint: Use ON with single = for join condition [OK]
Common Mistakes:
- Using WHERE instead of ON for join condition
- Using double equals '==' in SQL
- Incorrect USING clause syntax
3. Given tables
students (id, name) and grades (student_id, grade), what is the output of this query?
SELECT s.name, g.grade FROM students s INNER JOIN grades g ON s.id = g.student_id;
medium
Solution
Step 1: Understand the join condition
The query joins students and grades where students.id matches grades.student_id.Step 2: Predict output rows
Only students with matching grades appear, showing their name and grade as pairs.Final Answer:
[{"name": "Alice", "grade": "A"}, {"name": "Bob", "grade": "B"}] -> Option BQuick Check:
INNER JOIN returns matched student names with grades [OK]
Hint: INNER JOIN shows matched rows with selected columns [OK]
Common Mistakes:
- Expecting unmatched rows to appear
- Confusing alias names in SELECT
- Assuming syntax error without cause
4. Identify the error in this SQL query using INNER JOIN with aliases:
SELECT e.name, d.department FROM employees e INNER JOIN departments d ON e.dept_id = d.idd;
medium
Solution
Step 1: Check alias definitions
Aliases 'e' and 'd' are correctly defined for employees and departments.Step 2: Verify join condition columns
Column 'idd' in departments does not exist; likely a typo for 'id'.Final Answer:
Column 'idd' does not exist in departments table. -> Option CQuick Check:
Incorrect column name in ON clause causes error [OK]
Hint: Check column names in ON clause carefully [OK]
Common Mistakes:
- Assuming missing AS keyword causes error
- Confusing alias definition errors
- Changing join type unnecessarily
5. You have two tables:
orders (order_id, customer_id, amount) and customers (cust_id, name). Which query correctly uses INNER JOIN with aliases to list customer names and their total order amounts, grouping by customer name?hard
Solution
Step 1: Match join keys correctly
Join customers.cust_id with orders.customer_id to link orders to customers.Step 2: Use aliases and group by customer name
Use aliases 'c' and 'o' and group results by c.name to sum amounts per customer.Final Answer:
SELECT c.name, SUM(o.amount) FROM customers c INNER JOIN orders o ON c.cust_id = o.customer_id GROUP BY c.name; -> Option AQuick Check:
Correct join keys and grouping produce total per customer [OK]
Hint: Join on matching keys, group by customer name [OK]
Common Mistakes:
- Using wrong column names in ON clause
- Grouping by wrong column
- Mixing up alias names
