Challenge - 5 Problems
Table Alias Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of query using table aliases
Given two tables employees and departments, what is the output of this query?
SELECT e.name, d.name FROM employees AS e JOIN departments AS d ON e.dept_id = d.id;
MySQL
CREATE TABLE employees (id INT, name VARCHAR(20), dept_id INT); INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20); CREATE TABLE departments (id INT, name VARCHAR(20)); INSERT INTO departments VALUES (10, 'HR'), (20, 'Sales'); SELECT e.name, d.name FROM employees AS e JOIN departments AS d ON e.dept_id = d.id;
Attempts:
2 left
💡 Hint
Look at how the aliases e and d refer to employees and departments tables.
✗ Incorrect
The query joins employees (aliased as e) with departments (aliased as d) on matching dept_id and id. So Alice is in HR and Bob is in Sales.
📝 Syntax
intermediate2:00remaining
Identify the syntax error with table alias
Which option contains a syntax error when using table aliases in MySQL?
MySQL
SELECT e.name FROM employees e JOIN departments d ON e.dept_id = d.id;
Attempts:
2 left
💡 Hint
Check if the JOIN keyword is missing or misplaced.
✗ Incorrect
Option A misses the JOIN keyword between the two table aliases, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing query with table aliases
Which option uses table aliases to make the query more readable and efficient?
MySQL
SELECT employees.name, departments.name FROM employees JOIN departments ON employees.dept_id = departments.id;
Attempts:
2 left
💡 Hint
Using aliases shortens table names and clarifies columns.
✗ Incorrect
Option A uses aliases e and d consistently, making the query shorter and clearer.
🔧 Debug
advanced2:00remaining
Debug the ambiguous column error
What error will this query raise?
SELECT name FROM employees e JOIN departments d ON e.dept_id = d.id;
MySQL
CREATE TABLE employees (id INT, name VARCHAR(20), dept_id INT); CREATE TABLE departments (id INT, name VARCHAR(20)); SELECT name FROM employees e JOIN departments d ON e.dept_id = d.id;
Attempts:
2 left
💡 Hint
Both tables have a column named 'name'.
✗ Incorrect
The query does not specify which 'name' column to select, causing ambiguity error.
🧠 Conceptual
expert2:00remaining
Purpose of table aliases in complex queries
Why are table aliases especially useful in queries involving the same table multiple times?
Attempts:
2 left
💡 Hint
Think about self-joins and comparing rows within the same table.
✗ Incorrect
Aliases let you treat the same table as if it were two different tables, useful for self-joins.