Explore fundamental concepts of SQL JOINs and best practices for query optimization in PostgreSQL. This quiz covers join types, execution order, index usage, and efficient querying methods to enhance your relational database skills.
Given two tables 'employees' and 'departments', which JOIN clause would return only the rows where there is a matching department for each employee?
Explanation: The INNER JOIN returns only rows that have matching values in both tables, which means only employees with corresponding departments are listed. LEFT JOIN includes all employees, even those without a matching department. RIGHT JOIN would include all departments, even if no employee belongs to them. FULL OUTER JOIN returns all records with matches where available, including unmatched rows from both tables.
If you write 'SELECT * FROM products LEFT JOIN sales ON products.id = sales.product_id', what will the resulting dataset include?
Explanation: A LEFT JOIN returns all records from the left table (products), and matched records from the right table (sales); when there is no match, the sales fields will contain NULL values. Only returning products with sales would require an INNER JOIN. A result of only sales records is incorrect, as the base here is products. Including all sales records and matching products describes a RIGHT JOIN rather than a LEFT JOIN.
When optimizing JOIN queries in PostgreSQL, which indexing strategy is generally most helpful if columns are frequently used in join conditions?
Explanation: Indexing join columns speeds up lookups when joining tables, making queries more efficient. Indexing only non-join columns does not help with join performance. Avoiding indexes on join columns or relying solely on sequence scans can severely degrade performance, especially as table sizes grow.
What is the outcome of a FULL OUTER JOIN between 'students' and 'enrollments' tables where not every student has an enrollment and some enrollments have no matching student?
Explanation: A FULL OUTER JOIN includes all records from both tables, showing matches where they exist, and NULLs where there is no match. Including only matching records describes INNER JOIN. Showing only students with enrollments or only enrollments with matching students misses unmatched rows, which FULL OUTER JOIN does not exclude.
For retrieving all customer names and their orders, with customers who have placed no orders also included, which SQL statement is most efficient?
Explanation: A LEFT JOIN from customers ensures every customer is in the result, whether they have orders or not. Using LEFT JOIN starting from orders misses customers with no orders. INNER JOIN only shows customers with at least one order. RIGHT JOIN starting from orders also doesn't include customers without orders.
What does the 'USING' keyword accomplish in a SQL JOIN, as in 'SELECT * FROM A JOIN B USING (id)'?
Explanation: USING specifies the column(s) to perform the join and automatically removes duplicate columns from the result. Joining on all columns requires a NATURAL JOIN. Creating an alias is done with 'AS'. Comparing by data type only is not valid SQL JOIN behavior; USING explicitly matches by specified column.
What is typically processed first in the SQL query 'SELECT * FROM a INNER JOIN b ON a.id = b.id WHERE b.value u003E 50'?
Explanation: SQL query execution generally joins tables before applying WHERE conditions, so the JOIN occurs first. The SELECT is evaluated last, after filtering. The WHERE clause filters the result after the JOIN, not before. Aggregations, if any, are handled after WHERE—not before JOIN or filtering.
Which SQL feature allows you to define a subquery with a name for improved readability and potential query optimization?
Explanation: A Common Table Expression, introduced by the WITH clause, lets you name subqueries, making complex queries clearer and sometimes enabling optimization. The HAVING clause filters grouped results, GROUP BY organizes records for aggregation, and ORDER BY sorts output; none of these define named subqueries.
When using 'SELECT *' in a JOIN between two tables sharing a column name, what typically happens in the resulting output?
Explanation: With SELECT *, all columns from both tables are displayed, even if they have the same name, usually distinguished by their table names to avoid confusion. Only hiding duplicates happens when using USING or NATURAL JOIN. An error does not occur solely due to duplicate names. Automatic merging would only happen in specific join syntax, not with SELECT *.
What is the primary function of the EXPLAIN statement in PostgreSQL when analyzing a join query?
Explanation: EXPLAIN provides detailed information on how a query will be executed, including the steps involved and their estimated costs. It does not add indexes or rewrite queries on its own. While errors may occur if the syntax is wrong, this is not the statement's primary purpose.