Test your understanding of SQL joins, GROUP BY, and HAVING clauses with these beginner-friendly questions. Learn to identify correct join types and aggregation usage in common SQL scenarios.
Which SQL keyword pair combines rows from two tables only when there is a match in both tables, such as linking 'students' and 'enrollments' by 'student_id'?
Explanation: INNER JOIN returns rows that have matching values in both tables, making it perfect for combining students and enrollments that share a student_id. LEFT JOIN and RIGHT JOIN also combine rows but may include non-matching rows from one side. OUTER JOIN is not a specific SQL keyword; rather, it's a broad term sometimes informally used for left and right joins.
If you want to select all customers and their orders, showing customers even if they have no orders, which join type should you use?
Explanation: LEFT JOIN includes all rows from the left table (customers) and the matching rows from the right table (orders), filling NULLs where there’s no match. INNER JOIN would exclude customers without orders. RIGHT JOIN would include all orders, which is not the focus. FULL JOIN is not standard in all databases and returns all rows from both tables.
What is the primary purpose of the GROUP BY clause in a SQL SELECT statement using a 'sales' table?
Explanation: GROUP BY is used to collect rows that have the same values in specified columns into summary rows, such as sums or counts. It doesn't filter rows before joins; that's done with WHERE. Ordering rows is done with ORDER BY. UPDATE is a separate SQL command not related to aggregation.
Which query counts the number of orders placed by each customer in an 'orders' table?
Explanation: The correct query groups orders by customer_id and counts them, showing how many each customer made. SELECT COUNT(customer_id) would return only a total count, not broken by customer. SUM is not suitable for counting rows. The last option lacks GROUP BY, so it wouldn't aggregate per customer.
When filtering aggregate results such as totals after a GROUP BY, which clause is appropriate to use in SQL?
Explanation: HAVING filters groups created by GROUP BY and is evaluated after aggregation, making it suitable for aggregate filters. WHERE filters rows before grouping but cannot reference aggregate functions. ORDER BY is for sorting results, and JOIN is for combining tables.
If you JOIN two tables and some rows have no matches, what value does SQL show for columns from the non-matching table?
Explanation: SQL displays NULL for columns from the non-matching table in JOINs like LEFT or RIGHT JOIN when there's no corresponding row to join. ZERO is not automatically used unless specified. BLANK is not a data type; it’s a visual description. DEFAULT values are only used if defined for missing values, but not in JOINs by default.
To find the highest salary in an 'employees' table, which aggregation function should you use?
Explanation: The MAX function returns the highest value in a column, such as the highest salary. SUM adds up all values, AVG calculates the mean, and COUNT tallies the number of rows, so these do not identify the single highest value.
If you GROUP BY 'department' in a 'staff' table, which other column(s) can you select directly without aggregation?
Explanation: In SQL, after GROUP BY, only grouped columns and columns with aggregation (like SUM or AVG) can be selected directly. Selecting any column, or 'salary' without aggregation, would cause an error. Only 'department' is too restrictive because aggregates are also allowed.
If you write SELECT * FROM tableA INNER JOIN tableB ON tableA.id = tableB.id, what do you get in the result?
Explanation: SELECT * with INNER JOIN returns all columns from both tables but only for rows where the join condition matches. You do not get non-matching rows; that's the job of OUTER or FULL joins. Restricting to one table’s columns is not what SELECT * achieves.
If you use GROUP BY on two columns, like 'city' and 'store_type', what is the effect in a 'stores' table?
Explanation: GROUP BY with multiple columns groups data for every unique combination of the listed columns, like each city and each type of store. Grouping by only one column would not combine both. Not grouping would show all duplicates. Selecting only unique cities or store_types is more restrictive and misses the paired combinations.