Test your knowledge of SQL Joins, GROUP BY, HAVING, and writing practical reporting queries. This quiz covers core SQL concepts and basic skills needed to analyze and summarize data using joins and aggregations.
Which SQL clause returns only rows with matching values in both joined tables?
Explanation: An INNER JOIN only returns rows where there is a match in both tables, making it ideal for finding relationships shared by both. LEFT JOIN returns all records from the left table and matching rows from the right table, potentially including unmatched left-side rows. OUTER JOIN is not a standard SQL clause; it's usually specified as LEFT or RIGHT OUTER JOIN. FULL JOIN includes rows from both tables whether or not they match.
What is the primary function of the GROUP BY clause in SQL?
Explanation: GROUP BY groups together rows that have identical values in specified columns, allowing aggregate functions to summarize data within those groups. LIMIT (not listed) would restrict the number of records. Filtering is done with WHERE or HAVING, not GROUP BY. Sorting results is managed by ORDER BY, not GROUP BY.
If you perform a LEFT JOIN between Table A and Table B, what happens to rows from Table A with no matching rows in Table B?
Explanation: LEFT JOIN returns all records from Table A, and where there is no match in Table B, columns from Table B are filled with NULL. Excluding unmatched left rows is the behavior of INNER JOIN. Duplicating rows occurs only in special cases with multiple matches, not due to the lack of matches. This operation will not cause a query error.
Which clause would you use to filter aggregated group results, such as groups with a sum greater than 100?
Explanation: HAVING filters groups after aggregation, making it ideal to restrict results based on total values like a sum or count. ORDER BY only sorts results. WHERE is used to filter individual rows before grouping, not groups themselves. IN tests whether a value matches a set, not group aggregation.
What does the SQL function COUNT(*) do in a SELECT query?
Explanation: COUNT(*) counts the number of rows in a group or entire result, making it useful for summarizing records. Adding values is done with SUM(), averaging with AVG(), and finding minimum with MIN(). Each function provides a different type of aggregation.
Given tables 'sales' and 'employees', which JOIN would show all sales, including those without a matching employee?
Explanation: LEFT JOIN sales ON employees.id = sales.emp_id ensures all records from 'sales' are shown, with employee details where available. The INNER JOIN only displays rows with matches in both tables. LEFT JOIN employees ON sales.emp_id = employees.id prioritizes employees, not sales. FULL JOIN displays all matches from both sides, showing unmatched rows from both, not just from 'sales'.
If you run SELECT department, AVG(salary) FROM staff GROUP BY department, what does each row in the result represent?
Explanation: This query groups data by department, then computes the average salary for each group. Each row represents a unique department and its associated average. Individual staff records are not shown. Sorting is not performed here, as there's no ORDER BY. A summary of all salaries would use no grouping.
Which SQL construct allows you to join multiple tables to present a combined report view?
Explanation: The JOIN clause is used to combine rows from two or more tables based on related columns, enabling comprehensive reports. SELECT DISTINCT removes duplicates but does not join tables. CREATE DATABASE defines a new database and does not relate to querying. DROP TABLE deletes a table and is unrelated to combining data.
What is the result if you join Table X and Table Y using INNER JOIN and there are no matching rows in either table?
Explanation: An INNER JOIN returns only matches between both tables. If no matches exist, the output is empty. LEFT JOIN or RIGHT JOIN would include unmatched rows from one table. Including rows filled with NULL values is typical for OUTER or LEFT/RIGHT JOINs, not INNER JOINs. All rows from one table with unmatched rows is not the behavior of INNER JOIN.
When using SELECT department, SUM(amount) FROM payments GROUP BY department, what does SUM(amount) represent?
Explanation: SUM(amount) calculates the total of the 'amount' column for each department group. The highest payment would require MAX(), counting payments uses COUNT(), and the average payment uses AVG(). Each aggregation has a specific function for summarizing data.
In a SQL query joining customers and orders, which keyword specifies the matching columns?
Explanation: The ON keyword defines the joining condition, specifying which columns to match from each table. WHERE can be used for additional filtering but isn't meant specifically for join conditions. AS is used for aliases. LIMIT restricts the number of rows displayed and is unrelated to join conditions.
If you want each group to appear only once when aggregating data, which SQL keyword would you generally use with GROUP BY?
Explanation: GROUP BY collects rows into unique groups based on specified columns, ensuring one row per group in the result. UNION combines results of two queries but does not aggregate. ORDER BY sorts data, and DROP is used to delete objects like tables, not organize query output.
In which situation would you use WHERE instead of HAVING in a query with GROUP BY?
Explanation: WHERE filters rows before they are grouped, ensuring only matching rows participate in the grouping and aggregation. HAVING is used after data has been grouped, for filtering groups. ORDER BY is for sorting, and renaming columns is done with AS, not WHERE or HAVING.
What happens to records in the right table with no matching left-table row in a LEFT JOIN?
Explanation: LEFT JOIN includes all records from the left table, matching those from the right table where possible, and excludes any right table records without a left table match. The unmatched right-side rows are not included at all. Including NULLs for left-table columns is specific to RIGHT JOIN. Duplication and errors do not occur as distractions suggest.
Which JOIN returns all rows from both tables, including unmatched rows as NULLs for missing side values?
Explanation: FULL JOIN includes every row from both tables, adding NULLs for columns when there isn't a matching row on one side. INNER JOIN shows only matching records. LEFT JOIN returns all left-table rows but not all from the right. OUTER JOIN is a more generic term, and FULL JOIN is its specific implementation.
Which SQL query best reports the number of orders for each customer?
Explanation: This query groups orders by customer and counts them, reporting how many orders each customer made. Selecting only COUNT(*) from orders gives a total count, not per customer. Counting by order_id groups incorrectly. SELECT * with WHERE customer_id only filters, not groups or aggregates.