Test your knowledge of fundamental SQL concepts, clauses, and query techniques with this easy, beginner-friendly quiz. Improve your understanding of SQL syntax, filtering, data grouping, and join operations in preparation for interviews or academic assessments.
What is the main purpose of the GROUP BY clause in SQL?
Explanation: The GROUP BY clause is used to arrange rows with the same values in specified columns into summary rows, often paired with aggregate functions like COUNT, SUM, or AVG. Filtering is typically handled by the WHERE or HAVING clauses, not GROUP BY. Sorting is done using ORDER BY, and removing duplicates is managed with DISTINCT.
Which statement about the ORDER BY clause in SQL is correct?
Explanation: ORDER BY arranges the returned data in a specified order, such as alphabetically or by highest value. Combining tables is performed by JOIN clauses, not ORDER BY. Filtering data is done by WHERE or HAVING, and grouping is achieved with GROUP BY.
What does the WHERE clause do in an SQL SELECT statement?
Explanation: The WHERE clause restricts returned records to those matching specific criteria. Sorting is accomplished by ORDER BY, duplicates are removed using DISTINCT, and summarizing happens through aggregate functions with GROUP BY.
What is the primary difference between the WHERE and HAVING clauses in SQL?
Explanation: The WHERE clause applies conditions to rows before any grouping, while the HAVING clause filters groups after aggregation. WHERE is not used for sorting, grouping is managed by GROUP BY, and WHERE is not exclusive to JOIN operations.
Which SQL clause would you use to find customers whose names begin with 'A'?
Explanation: The LIKE operator with 'A%' finds all names starting with 'A'. Using '=' does not support pattern matching, 'IN' is used for a set of values, and '==' and '*' are not standard SQL for this purpose.
What best describes the difference between an INNER JOIN and a LEFT JOIN in SQL?
Explanation: INNER JOIN retrieves only the rows with matches in both tables. LEFT JOIN returns all rows from the left table, plus matching rows from the right or NULLs when there's no match. LEFT JOIN does not always produce fewer rows, and neither JOIN type returns just unmatched rows.
In what scenario would a self-join be useful in SQL?
Explanation: A self-join is used to compare rows within the same table, for example, linking employees to their managers in a hierarchical setup. Filtering nulls is handled by WHERE or IS NOT NULL. Regular joins connect different tables, and aggregate calculations don’t require joining a table with itself.
What is the primary function of the CASE statement in SQL?
Explanation: CASE allows SQL queries to branch logic based on conditions, returning different values depending on which is met. Grouping uses GROUP BY, removing duplicates is done via DISTINCT, and joining tables is managed by JOIN clauses.
Which SQL statement retrieves all columns from a table named 'products'?
Explanation: 'SELECT * FROM products' is the standard syntax to fetch all columns. The other options are either incorrect or not recognized in SQL.
To select all orders with amounts greater than 100 and status as 'delivered', which clause is correct?
Explanation: Using 'AND' ensures both conditions must be true. 'OR' allows either, 'u0026u0026' is not standard SQL, and separating conditions with a semicolon is not valid syntax.
Which wildcard is used with LIKE to represent any number of characters in an SQL pattern?
Explanation: The percent sign (%) in LIKE patterns represents zero or more characters. Asterisk (*) and other symbols shown here are not used for this purpose in standard SQL.
What is the default sorting order in SQL when using the ORDER BY clause?
Explanation: ORDER BY sorts in ascending order by default unless DESC is specified. 'Random' or 'Grouped' are not valid sorting orders.
Which of the following is an aggregate function in SQL?
Explanation: SUM() totals numerical values in a column and is a classic aggregate function. CONCAT() joins strings, GROUP() is not an SQL function, and REPLACE() modifies string values.
How can you select only the records where the 'email' column is not null?
Explanation: 'IS NOT NULL' is correct for checking non-null values. The other variations are incorrect or not supported by SQL for null checks.
Which keyword retrieves only unique, non-duplicate values from a column?
Explanation: DISTINCT ensures each value appears only once. COUNT returns the number of results, ONLY and UNIQUE() are not valid SQL keywords for this operation.
What is the purpose of the OR keyword in a WHERE clause?
Explanation: OR in WHERE allows retrieval of any rows matching at least one condition. AND is needed for requiring all conditions. Plus signs and sorting functions are not relevant to this keyword.