Consider two tables: Employees and Departments.
Employees has columns: emp_id, name, dept_id.
Departments has columns: dept_id, dept_name.
What will be the result of this query?
SELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id = d.dept_id ORDER BY e.emp_id;
Remember INNER JOIN returns rows where the join condition matches.
The query joins employees with their matching department by dept_id. Only employees with a matching department appear.
Choose the query that correctly uses INNER JOIN with table aliases a and b for tables TableA and TableB.
Check the JOIN syntax and ON clause format.
Option A uses correct INNER JOIN syntax with aliases and ON condition. Option A uses WHERE instead of ON, which is incorrect here. Option A uses '==' which is invalid in SQL. Option A has an incomplete WHERE clause.
Given large tables Orders (alias o) and Customers (alias c), which query is best optimized?
Filtering early in the JOIN can improve performance.
Option A filters customers by status during the JOIN, reducing rows processed. Option A joins all customers, including inactive. Option A uses implicit join syntax which is less clear and may be less optimized. Option A uses LEFT JOIN but filters on customer status, effectively acting like INNER JOIN but less efficient.
Given tables Products (alias p) and Categories (alias c), why does this query cause an error?
SELECT p.product_name, c.category_name FROM Products p INNER JOIN Categories c ON p.category_id = category_id;
Check the ON clause for proper column references.
The ON clause uses 'category_id' without specifying which table it belongs to. This causes ambiguity since both tables may have a column named 'category_id'. Aliases must prefix columns in JOIN conditions.
Consider the query:
SELECT a.col1, b.col2 FROM Table1 a INNER JOIN Table2 b ON a.id = b.id;
What is the main purpose of using aliases a and b here?
Think about why we use short names in writing queries.
Aliases are used to give tables shorter names to simplify query writing and improve readability, especially with long table names or multiple joins.