Test your knowledge of SQL joins and aggregation techniques with beginner-friendly questions on INNER JOIN, LEFT JOIN, GROUP BY, and HAVING. This quiz is perfect for anyone seeking to review or strengthen their SQL query skills, especially with joins and group-based calculations.
Which SQL clause combines rows from two tables only when there is a matching value in both tables?
Explanation: INNER JOIN returns only the rows where there is a match in both tables, making it the standard join for intersecting data. LEFT JOIN includes unmatched rows from the left table, FULL JOIN returns all rows when there is a match in one of the tables, and OUTER MATCH is not a valid SQL clause.
When using a LEFT JOIN between tables 'Customers' and 'Orders', which describes the returned data?
Explanation: LEFT JOIN returns all records from the left table—in this case, Customers—even if there is no matching row in Orders. 'Only customers with orders' would describe INNER JOIN, 'All orders, with customer data if available' is a RIGHT JOIN scenario, and 'Only orders placed on Mondays' is irrelevant here.
In a SQL query, what does the GROUP BY clause accomplish?
Explanation: The GROUP BY clause organizes rows into groups based on the values of specified columns, allowing for aggregate calculations per group. Filtering after aggregation is handled by HAVING, not GROUP BY. Joining tables is done by JOIN clauses, and duplicates are removed with DISTINCT.
Which SQL clause should you use to filter the results of a GROUP BY operation based on aggregated values?
Explanation: The HAVING clause filters groups created by GROUP BY, typically using aggregate functions like COUNT or SUM. WHERE filters individual rows before aggregation, ORDER BY sorts the results, and WITH defines common table expressions.
How can you combine three tables with INNER JOIN so that only rows with matching keys in all tables are shown?
Explanation: Chaining INNER JOIN clauses connects all three tables, ensuring only rows present in all are included. LEFT JOIN would retain all rows from the first table, GROUP BY does not perform joining, and listing tables with commas is obsolete, error-prone syntax.
Which SQL function would you use to count the number of rows in each group after a GROUP BY?
Explanation: COUNT returns the number of rows in each group, making it ideal for quantifying group size. AVG computes averages, MAX returns the highest value, and RANK is not an aggregate function.
What value is returned for the right table's columns in a LEFT JOIN when there is no matching row?
Explanation: When there is no match in the right table, LEFT JOIN produces NULL for those columns. Zero and empty string are valid values but not defaults, and N/A is not a standard SQL value.
If you want to filter sales rows before grouping by region, which SQL clause should you use?
Explanation: WHERE filters records before the GROUP BY groups them. HAVING is for filtering after aggregation. WHEN and GROUP FILTER are not valid SQL filtering clauses in this context.
Which is a correct usage of GROUP BY if you want to aggregate data by 'department'?
Explanation: GROUP BY department is the correct syntax. GROUP BY SUM(department) is invalid as SUM should only be in the SELECT or HAVING clauses, 'EACH' is not used in SQL for groups, and 'GROUP department BY' is reversed.
How would you display the average salary in a 'salary' column by each job title along with the job title?
Explanation: The correct format is SELECT job_title, AVG(salary), grouping by job_title. The second option places GROUP BY incorrectly, the third option omits the aggregate column, and the fourth tries to average job_title, which is a string.
Why is the ON clause important in JOIN operations?
Explanation: The ON clause defines how the tables are linked by setting join conditions. It doesn't filter groups (that's HAVING), add columns directly, or perform sorting (which is handled by ORDER BY).
How can you find customers who have placed no orders using 'Customers' and 'Orders' tables?
Explanation: LEFT JOIN returns all customers; filtering where Orders.id IS NULL shows those without a matching order. INNER JOIN would not include unmatched customers, RIGHT JOIN is the reverse, and ON TRUE joins everything indiscriminately.
Which query filters to show only products with more than 50 sales, grouped by product?
Explanation: HAVING must follow GROUP BY to filter on aggregates; the first option uses correct order and logic. The second has HAVING before GROUP BY, which is invalid. The others misuse WHERE or do not group properly.
Which type of JOIN would you use to include all rows from the left table even if there are no matching rows in the right table?
Explanation: LEFT JOIN ensures all rows appear from the left table, filling with NULLs where needed. INNER JOIN would omit unmatched rows. OUTER JOIN typically refers to FULL OUTER JOIN, which includes all rows from both tables. CROSS JOIN creates combinations, not matched results.
After grouping by 'category', which aggregate function tells you how many groups there are?
Explanation: COUNT is the standard function to determine the number of resulting groups or rows. SUM accumulates values, GROUPS is not a standard function, and DIVIDE is not a SQL aggregate.
If a column contains NULL values, which join type will NOT return rows where the join key is NULL?
Explanation: INNER JOIN requires matching values in both tables, so NULLs in the join column will prevent a match. LEFT JOIN, FULL JOIN, and RIGHT JOIN can include rows with NULLs if present in either side, depending on which table has the NULL.