SQL Basics: Essential Interview Questions Quiz

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.

  1. Purpose of GROUP BY

    What is the main purpose of the GROUP BY clause in SQL?

    1. To sort the data in ascending or descending order
    2. To filter rows before retrieving data
    3. To delete duplicate records from the table
    4. To summarize data by grouping rows with identical column values

    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.

  2. Using ORDER BY

    Which statement about the ORDER BY clause in SQL is correct?

    1. ORDER BY sorts query results based on specified columns
    2. ORDER BY combines multiple tables into one result set
    3. ORDER BY groups data into summary rows
    4. ORDER BY filters data before aggregation

    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.

  3. Function of WHERE Clause

    What does the WHERE clause do in an SQL SELECT statement?

    1. It filters rows based on given conditions
    2. It sorts the result set in descending order
    3. It summarizes rows using aggregate functions
    4. It removes duplicate values from the result

    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.

  4. WHERE vs. HAVING

    What is the primary difference between the WHERE and HAVING clauses in SQL?

    1. WHERE can only be used with INNER JOINs
    2. HAVING filters individual records, WHERE does grouping
    3. WHERE is only used for sorting results
    4. WHERE filters individual rows before grouping, while HAVING filters groups after aggregation

    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.

  5. Using LIKE Operator

    Which SQL clause would you use to find customers whose names begin with 'A'?

    1. WHERE name LIKE 'A%'
    2. WHERE name IN 'A%'
    3. WHERE name = 'A%'
    4. WHERE name == '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.

  6. INNER JOIN vs. LEFT JOIN

    What best describes the difference between an INNER JOIN and a LEFT JOIN in SQL?

    1. INNER JOIN returns matching rows from both tables, while LEFT JOIN returns all rows from the left table and matching rows from the right table
    2. LEFT JOIN always produces fewer rows than INNER JOIN
    3. LEFT JOIN only returns rows with no matches
    4. INNER JOIN returns all rows from both tables

    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.

  7. Purpose of Self-Join

    In what scenario would a self-join be useful in SQL?

    1. When you need to compare rows within the same table, such as matching employees to their managers
    2. When filtering out null values in a column
    3. When joining two unrelated tables
    4. When performing aggregate calculations

    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.

  8. Using CASE Statement

    What is the primary function of the CASE statement in SQL?

    1. To group rows into summary rows
    2. To join two tables based on a key
    3. To remove duplicates from query results
    4. To perform conditional logic, similar to if-then-else statements

    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.

  9. Selecting All Columns

    Which SQL statement retrieves all columns from a table named 'products'?

    1. SELECT ALL FROM products
    2. SELECT products FROM *
    3. SELECT FROM products *
    4. SELECT * FROM products

    Explanation: 'SELECT * FROM products' is the standard syntax to fetch all columns. The other options are either incorrect or not recognized in SQL.

  10. Filtering With Multiple Conditions

    To select all orders with amounts greater than 100 and status as 'delivered', which clause is correct?

    1. WHERE amount u003E 100 u0026u0026 status = 'delivered'
    2. WHERE amount u003E 100; status = 'delivered'
    3. WHERE amount =u003E 100 OR status = 'delivered'
    4. WHERE amount u003E 100 AND status = 'delivered'

    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.

  11. Using Wildcards

    Which wildcard is used with LIKE to represent any number of characters in an SQL pattern?

    1. #
    2. !
    3. %
    4. *

    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.

  12. Default Sorting Order

    What is the default sorting order in SQL when using the ORDER BY clause?

    1. Grouped
    2. Descending (DESC)
    3. Ascending (ASC)
    4. Random

    Explanation: ORDER BY sorts in ascending order by default unless DESC is specified. 'Random' or 'Grouped' are not valid sorting orders.

  13. Aggregate Functions

    Which of the following is an aggregate function in SQL?

    1. SUM()
    2. REPLACE()
    3. CONCAT()
    4. GROUP()

    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.

  14. Filtering Null Values

    How can you select only the records where the 'email' column is not null?

    1. WHERE email != NULL
    2. WHERE email IS NOT NONE
    3. WHERE email IS NOT NULL
    4. WHERE email u003Cu003E 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.

  15. Identifying Unique Values

    Which keyword retrieves only unique, non-duplicate values from a column?

    1. ONLY
    2. DISTINCT
    3. UNIQUE()
    4. COUNT

    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.

  16. Combining Conditions

    What is the purpose of the OR keyword in a WHERE clause?

    1. To return rows where at least one of multiple conditions is true
    2. To sort by original record order
    3. To return only rows where all conditions are true
    4. To perform a mathematical addition

    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.