Essential SQL Commands Quiz Quiz

Test your knowledge of basic SQL commands, such as SELECT, WHERE, JOIN, UPDATE, and more. This quiz covers key SQL concepts, syntax, and common use cases for effective database querying and management.

  1. Selecting Data

    Which SQL command is used to retrieve specific columns from a table named Employees, such as only the 'name' and 'salary' columns?

    1. EXTRACT name, salary FROM Employees
    2. SHOW name, salary Employees
    3. SELECT name, salary FROM Employees
    4. FIND name, salary IN Employees

    Explanation: The SELECT statement is correctly used for retrieving specific columns from a table. FIND, EXTRACT, and SHOW are not valid SQL commands in this context. While they may sound similar or hint at data retrieval, only SELECT follows SQL syntax and semantics.

  2. Filtering Results

    If you want to get all products with a price greater than 50, which SQL clause should you use to specify this condition?

    1. FILTER BY price u003E 50
    2. WHERE price u003E 50
    3. HAVING price u003E 50
    4. ORDER BY price u003E 50

    Explanation: The WHERE clause is used to filter rows based on the specified condition. ORDER BY determines the sorting, not filtering. HAVING is for filtering groups, not individual rows. FILTER BY is not standard SQL syntax.

  3. Renaming Columns

    Which SQL keyword allows you to temporarily rename a column in your result set, such as displaying 'first_name' as 'Name'?

    1. IS
    2. IN
    3. LIKE
    4. AS

    Explanation: The AS keyword is used as an alias for columns or tables to rename them in the query output. LIKE is for pattern matching, IN is used for specifying multiple criteria, and IS is used for comparisons like IS NULL.

  4. Combining Tables

    How would you combine rows from two tables, for example 'Customers' and 'Orders', to view related information?

    1. MERGE
    2. LINK
    3. JOIN
    4. APPEND

    Explanation: JOIN is the correct SQL command for combining rows from two or more tables based on a related column. MERGE is not standard in basic SQL for table combination, APPEND is not valid SQL syntax, and LINK is a non-SQL term.

  5. Conditional Multiple Criteria

    If you want to select users whose age is 18 or 21, which SQL clause helps list multiple possible values in a condition?

    1. LIKE
    2. IN
    3. CASE
    4. BETWEEN

    Explanation: The IN clause is used to specify multiple possible values in a WHERE condition. CASE is for conditional outputs, BETWEEN is for range queries, and LIKE is for pattern matching, not multiple explicit values.

  6. Pattern Matching

    Which SQL operator should be used to find all usernames that begin with 'admin'?

    1. LIKE
    2. BEGIN
    3. STARTS
    4. MATCHES

    Explanation: LIKE operator is used in SQL for pattern matching in string comparisons. BEGIN, MATCHES, and STARTS are not valid SQL keywords for this purpose. LIKE allows the use of wildcards such as '%' for flexible searches.

  7. Limiting Output

    Which SQL command limits the number of rows returned in a query to, for example, only 10 results?

    1. ROWNUM 10
    2. FETCH 10
    3. TOP 10
    4. LIMIT 10

    Explanation: LIMIT is the standard SQL clause for restricting the number of records returned, especially in many modern databases. FETCH and TOP can be used in some systems but are less standardized, and ROWNUM is specific to certain database engines.

  8. Null Value Checks

    Which SQL condition should you use to retrieve all rows where the 'email' column is missing a value?

    1. LIKE NULL
    2. EQUALS NULL
    3. IS NULL
    4. IS EMPTY

    Explanation: IS NULL checks for missing or undefined values in SQL. EQUALS NULL and LIKE NULL are incorrect syntaxes, and IS EMPTY is not standard in SQL for checking null values.

  9. Updating Data

    What SQL command updates a table named 'Inventory' to set the 'stock' column to 100 for all rows?

    1. UPDATE Inventory SET stock = 100
    2. ALTER Inventory SET stock = 100
    3. CHANGE Inventory SET stock = 100
    4. MODIFY Inventory stock = 100

    Explanation: UPDATE is used to modify existing data in a table. CHANGE, ALTER (when used alone), and MODIFY are not used in this context in SQL syntax. ALTER is for schema changes, not row updates.

  10. Grouping Data

    How do you group records in a table by a specific column, such as 'country', to aggregate data like total sales for each country?

    1. COLLECT BY country
    2. GROUP BY country
    3. COMBINE BY country
    4. ORDER BY country

    Explanation: GROUP BY is designed for grouping rows based on a specific column before applying aggregate functions. ORDER BY merely sorts results, while COLLECT BY and COMBINE BY are not valid SQL commands.

  11. Deleting Data

    Which SQL statement correctly removes all rows from the 'Logs' table where 'status' equals 'inactive'?

    1. ERASE FROM Logs WHERE status = 'inactive'
    2. DELETE FROM Logs WHERE status = 'inactive'
    3. DROP Logs WHERE status = 'inactive'
    4. REMOVE FROM Logs status = 'inactive'

    Explanation: DELETE FROM with a WHERE clause is the proper SQL statement for deleting specific rows. REMOVE and ERASE are not SQL commands. DROP deletes the table structure itself, not just the rows.

  12. Rolling Back Transactions

    If you have made changes in a transaction and want to undo them, which SQL command should be used?

    1. ROLLBACK
    2. UNDO
    3. REVERSE
    4. CANCEL

    Explanation: ROLLBACK undoes all changes made in the current transaction. UNDO, CANCEL, and REVERSE are not standard SQL commands for this purpose, even though their names imply some sort of reversal.

  13. Aggregate Functions

    Which SQL function would you use to calculate the average of the 'score' column in a 'Games' table?

    1. MIN(score)
    2. AVG(score)
    3. COUNT(score)
    4. SUM(score)

    Explanation: AVG stands for average and is the correct aggregate function for this calculation. SUM returns the total, COUNT counts the rows, and MIN finds the smallest value, none of which produce the average.

  14. Ordering Results

    How do you write a SQL statement to list products from the 'Products' table in descending order of their 'price'?

    1. SORT BY price DESCEND
    2. ORDER BY price DESC
    3. ORDER price DOWN
    4. GROUP BY price REVERSE

    Explanation: ORDER BY with the DESC keyword lists the results in descending order. ORDER price DOWN and SORT BY price DESCEND are not valid syntaxes. GROUP BY is used for aggregation, not for sorting.

  15. Table Structure Modification

    Which SQL statement is used to add a column called 'birthdate' to an existing table named 'Employees'?

    1. MODIFY Employees COLUMNS birthdate
    2. UPDATE Employees ADD birthdate
    3. ALTER TABLE Employees ADD birthdate
    4. CHANGE Employees INSERT birthdate

    Explanation: ALTER TABLE with the ADD clause is the correct SQL way to add new columns to an existing table. UPDATE is for modifying data, not structure, and neither MODIFY nor CHANGE are valid commands in this context.

  16. Conditional Output

    Which SQL construct would you use to return different results in a SELECT query based on a column's value, such as labeling grades as 'pass' or 'fail'?

    1. SWITCH
    2. WHEN
    3. IF
    4. CASE

    Explanation: CASE allows you to implement conditional logic in SQL queries to return different values based on certain conditions. SWITCH and IF are not supported keywords in SQL for this scenario, and WHEN is part of the CASE expression rather than being a standalone keyword.