Challenge your understanding of complex SQL join techniques and aggregation scenarios. This quiz is designed to test deep knowledge of joins, groupings, subqueries, and aggregate functions.
Understanding FULL OUTER JOIN
If Table1 has 10 rows and Table2 has 8 rows with 2 matching IDs, how many rows will the result of a FULL OUTER JOIN on ID contain?
- 10
- 16
- 12
- 18
- 8
Aggregating with GROUP BY and HAVING
Given a table Sales with columns: id, product, quantity, and price, which query correctly returns products with a total quantity sold greater than 100?
- SELECT product, SUM(quantity) FROM Sales HAVING SUM(quantity) u003E 100;
- SELECT product FROM Sales GROUP BY product WHERE SUM(quantity) u003E 100;
- SELECT product, SUM(quantity) FROM Sales WHERE quantity u003E 100 GROUP BY product;
- SELECT product, SUM(quantity) FROM Sales GROUP BY product HAVING SUM(quantity) u003E 100;
- SELECT product FROM Sales WHERE SUM(quantity) u003E 100 GROUP BY product;
Identifying Correct JOIN Types
Which join should you use to return all records from Table A and only the matching records from Table B, filling with NULLs when no match is found?
- OUTER JOIN
- LEFT JOIN
- CROSS JOIN
- RIGHT JOIN
- INNER JOIN
Ambiguous Columns in Joins
When joining Table Employee and Table Department, both containing a column named 'id', how should you refer to 'id' to avoid ambiguity?
- Employee-id
- id
- Empid
- Employee.id
- Department-id
Aggregating After JOINs
After joining Orders and Customers, how do you count the number of orders for each customer using SQL aggregation functions?
- SELECT Customers.id, COUNT(Orders.id) FROM Customers LEFT JOIN Orders ON Customers.id = Orders.customer_id GROUP BY Customers.id;
- SELECT Customers.id, SUM(Orders.id) FROM Orders JOIN Customers ON Orders.customer_id = Customers.id;
- SELECT COUNT(*) FROM Customers GROUP BY Orders.id;
- SELECT Customers.id, COUNT(*) FROM Orders INNER JOIN Customers GROUP BY Orders.id;
- SELECT Customers.id, COUNT(Orders.id) FROM Customers GROUP BY Customers.id;
Joins vs. Subqueries in Aggregation
Which approach is generally more efficient for finding the maximum salary per department in a large Employee table?
- Nesting subqueries for each department
- Self CROSS JOIN with WHERE filters
- Joins with UNION ALL
- Multiple correlated subqueries in the SELECT clause
- Using GROUP BY with aggregation in a single scan
Set Operations vs. Joins
What distinguishes a UNION from an INNER JOIN in SQL?
- INNER JOIN produces more rows than UNION always
- UNION only works with numeric data, INNER JOIN is data type agnostic
- UNION always requires an ON clause
- UNION combines rows from result sets, INNER JOIN merges columns from matching rows
- UNION removes duplicate rows, INNER JOIN keeps all duplicates
JOINs and NULL Values
If a LEFT JOIN is performed between Students and Scores on student_id, what will be returned for students with no scores?
- Only student names are returned
- Only students with at least one score
- All student details with NULLs in the Scores columns
- No students are shown at all
- All scores with NULLs in the Students columns
GROUP BY with Multiple Columns
Given a table Orders with columns (order_id, customer_id, status), which SQL snippet correctly counts the number of orders per customer and status combination?
- SELECT order_id, COUNT(*) FROM Orders GROUP BY order_id, status;
- SELECT customer_id, status, COUNT(*) FROM Orders GROUP BY customer_id, status;
- SELECT customer_id, status, SUM(order_id) FROM Orders GROUP BY status;
- SELECT customer_id, COUNT(status) FROM Orders GROUP BY customer_id;
- SELECT COUNT(*), status FROM Orders GROUP BY customer_id;
Self-JOIN Scenario
To find employees who have the same manager in an Employee table (employee_id, manager_id), which approach is correct?
- JOIN on employee_id = manager_id without condition
- LEFT JOIN Employee to itself on employee_id = manager_id
- Use an INNER JOIN with a UNION on manager_id
- Aggregate by employee_id and count manager_id
- Perform a self-join on Employee.manager_id, filtering for different employee_ids