Test your understanding of SQL joins and aggregation techniques with this quiz. Questions focus on concepts, syntax, and usage of different join types and aggregation functions to help you master key SQL skills.
Which type of JOIN returns only the rows where there is a match in both tables for a given condition?
Explanation: An INNER JOIN returns records that have matching values in both tables according to the specified condition, so only overlapping data is included. LEFT JOIN includes all records from the left table regardless of matches in the right, and FULL JOIN returns all records when there is a match in either table. CROSS JOIN simply combines each row from one table with every row from another, which does not require any matching.
In SQL, which JOIN should you use if you want to include all records from the first (left) table and only the matching rows from the second (right) table?
Explanation: LEFT JOIN returns all rows from the left table, and the matched rows from the right table; rows from the right with no match result in NULLs. RIGHT JOIN works in the reverse order, while SELF JOIN joins a table with itself, and OUTER JOIN is not a standard SQL keyword (FULL OUTER JOIN is different).
Given two tables, if you want all records from the second (right) table regardless of matching, which SQL JOIN should you use?
Explanation: RIGHT JOIN includes all rows from the right table along with matching rows from the left table; unmatched left rows result in NULLs. INNER JOIN only includes matched rows, LEFT JOIN prioritizes the left table, and UNION JOIN is not a valid SQL operation.
Which keyword is typically used in SQL to specify the columns used to JOIN two tables together?
Explanation: ON is the standard keyword required to define the condition on which two tables are joined, such as ON table1.id = table2.id. USING is only valid for joining on columns with the same name, WHERE is used for filtering data, and ORDER is used for sorting, not joining.
Which SQL aggregate function should you use to determine the number of rows returned by a query?
Explanation: COUNT calculates the number of rows that meet the query conditions. SUM adds up values, AVG calculates the arithmetic mean, and MIN finds the minimum value. Only COUNT is intended for counting rows.
When writing a query to show the total sales per customer, which SQL clause groups the results by customer?
Explanation: GROUP BY organizes rows into groups sharing equivalent values, allowing aggregate functions across those groups. ORDER BY sorts results, WHERE filters rows before grouping, and HAVING is used to filter groups after aggregation.
If you want to calculate the total revenue from a 'sales_amount' column, which aggregate function should you use?
Explanation: SUM returns the total value of a numeric column, ideal for calculating total revenue. COUNT counts rows, MAX finds the highest value, and FIRST, while it might sound logical, is not a standard aggregate function in SQL.
In which scenario would you use the DISTINCT keyword with an aggregation?
Explanation: DISTINCT is used in combination with aggregate functions like COUNT to count only unique values in a column. It does not display every row, perform joins, or order results (ORDER BY is for sorting).
Which option correctly describes joining two tables using more than one column from each table?
Explanation: You can join tables on multiple columns by specifying several conditions within the ON clause separated by AND. HAVING filters after aggregation, ORDER BY only sorts, and WHERE filters rows but is not directly for joining tables.
What does a FULL JOIN (also called FULL OUTER JOIN) do in SQL?
Explanation: FULL JOIN includes every row from both tables, placing NULL where there is no match. Left JOIN limits to left table rows, Inter JOIN shows only matches, and Right JOIN focuses on right table entries.
Which SQL clause is used to filter the results of a GROUP BY after aggregation?
Explanation: HAVING filters the results after aggregation, allowing conditions on aggregate values (such as COUNT u003E 10). WHERE filters rows before aggregation, ORDER BY sorts the output, and JOIN is used to combine tables, not filter aggregate groups.
Which aggregate function should you use to calculate the average value in the 'score' column?
Explanation: AVG finds the mean, which is the total of the values divided by the number of items. SUM adds up the values, COUNT counts them, and TOP is not an aggregate function in SQL.
Given two tables, which type of JOIN returns every possible combination of rows from both tables?
Explanation: A CROSS JOIN produces the Cartesian product of the tables, giving all possible row combinations. INNER JOIN combines only matching rows, LEFT JOIN only left table rows with matches, and NATURAL JOIN automatically joins columns with the same name (not every combination).
Which function will return the largest value from a column named 'salary'?
Explanation: MAX picks the highest value in a set. MIN does the opposite, COUNT tallies rows, and MEDIAN—which sounds plausible—is not a standard SQL aggregate function.
What does SELF JOIN mean in the context of SQL tables?
Explanation: A SELF JOIN references the same table twice in a query using aliases, usually to compare rows within that table. Joining unrelated tables is not specific, primary key joins are a technique but not unique to SELF JOIN, and circular references describe a data design issue rather than a join type.
How does the SUM() aggregate function handle NULL values?
Explanation: SUM ignores NULL values and only adds up non-null entries, not counting them as zeros or causing errors. If all values are NULL, the result is NULL. COUNT can exclude or include NULLs based on usage, but SUM does not.