Test your knowledge of SQL joins, GROUP BY, HAVING clauses, and basic window functions with this easy-level quiz. Perfect for beginners looking to reinforce their understanding of data relationships and aggregation techniques in SQL.
Which SQL clause returns only the rows that have matching values in both tables when joining them?
Explanation: INNER JOIN returns rows where there is a match in both tables, ensuring only matching data is selected. LEFT JOIN returns all records from the left table and the matched records from the right table, so unmatched rows from the right are not included. OUTER JOIN is a general term, but it is not a valid SQL keyword by itself. CROSS JOIN combines all records from both tables, regardless of matches.
When using LEFT JOIN in SQL, what happens to the rows in the left table that do not have a matching row in the right table?
Explanation: LEFT JOIN keeps all rows from the left table, even if there is no match in the right table, and fills the right table's columns with NULL values where matches do not exist. Rows are not excluded; that's the behavior of INNER JOIN. Rows are not duplicated or included twice; each matches at most one in the right table.
What is the primary use of the GROUP BY clause in an SQL SELECT statement?
Explanation: GROUP BY groups rows sharing the same value of specified columns into summary rows, which enables aggregating data such as sums or counts. LIMIT is used to restrict the row count, not GROUP BY. ORDER BY sorts results, and JOIN clauses are for combining columns from multiple tables.
Why would you use the HAVING clause instead of WHERE after a GROUP BY in SQL?
Explanation: HAVING is used to filter groups after aggregation, making it possible to filter results based on aggregate values like SUM or COUNT. WHERE filters rows before they are grouped, not after aggregation. ORDER BY sorts results, and JOIN is unrelated to HAVING.
What does the COUNT(*) function return when used with GROUP BY on the 'department' column in an 'employees' table?
Explanation: COUNT(*) with GROUP BY 'department' counts the rows (employees) in each department, giving per-group totals. It does not count the number of departments, which could be found with COUNT(DISTINCT department). It does not sum salaries or retrieve the first record from each group.
If you want a list of all customers, including those who have not placed any orders, which SQL join should you use between 'customers' and 'orders'?
Explanation: LEFT JOIN includes all records from the left table (customers), even if there are no matching rows in the right table (orders). INNER JOIN would exclude customers without orders. RIGHT JOIN would keep all orders, not all customers. FULL JOIN includes all records from both tables but may add unnecessary unmatched records from both sides.
If you use 'SELECT SUM(sales) FROM store_sales GROUP BY region', what does each row in the result represent?
Explanation: SUM(sales) with GROUP BY region calculates the total sales within each region, outputting one row per region. It does not group by store, average sales, or give the overall sum unless GROUP BY is removed or modified.
Which keyword specifies the condition used to match rows from two tables in an SQL JOIN?
Explanation: The ON keyword is used in JOIN clauses to set the joining condition between columns. BY is used in GROUP BY and ORDER BY. WITH is for common table expressions, and INTO is for inserting or creating tables.
What does the ROW_NUMBER() window function do in an SQL query?
Explanation: ROW_NUMBER() assigns a sequential integer to rows within each partition of result set, often used for ranking. COUNT counts rows, not row numbers. SUM computes running totals with SUM() OVER, while primary keys are table-level constraints, not functions.
In basic window functions like 'ROW_NUMBER() OVER (PARTITION BY category)', what does PARTITION BY do?
Explanation: PARTITION BY splits the data into subsets (partitions), so the window function like ROW_NUMBER() starts over in each group. It does not restrict row count, sort order, or perform filtering before aggregation.
When used with ROW_NUMBER(), what does the ORDER BY clause inside the OVER() window specify?
Explanation: ORDER BY inside the OVER() clause determines the sequence in which ROW_NUMBER() (or similar functions) assigns numbers to rows. It does not group, filter, or join rows. Grouping is set by PARTITION BY, and joins happen earlier in the query.
If two tables have NULL values in the join columns, what is the result of an INNER JOIN on those columns?
Explanation: In INNER JOIN, rows with NULL in the join columns are not considered equal and do not match, so those rows are excluded. Matching NULLs together does not happened, and NULLs don’t generate extra matches. The query does not fail because of NULL, but the rows are simply not joined.
What is the correct syntax to calculate the average salary in an 'employees' table?
Explanation: AVG() is the standard SQL function for calculating the average. AVERAGE and MEAN are not valid SQL aggregate functions. While SUM divided by COUNT may provide the same result, it is less direct and could give misleading results if NULLs are present.
Which keyword would you add to a GROUP BY query to eliminate duplicate results in the output?
Explanation: DISTINCT removes duplicate rows from the final result set of a query, and can be combined with GROUP BY. UNIQUE and REMOVE are not valid SQL keywords in this context, while EXCLUDE does not remove duplicates in SQL.
How can you count the number of unique customers who placed an order in an 'orders' table?
Explanation: COUNT(DISTINCT column) counts the number of unique non-NULL values in a column, ideal for unique customers. COUNT with GROUP BY order_id would not count unique customers. SUM is for adding numeric values, not counting. DISTINCT by itself with SELECT returns unique customer IDs, not a count.
If you write 'SELECT SUM(price) FROM sales;' without GROUP BY, what value is returned?
Explanation: Without GROUP BY, SUM aggregates over the entire table and gives one value: the total sum. It does not give grouped totals, nor does it count records. Multiple sums or sums by region require GROUP BY.