Structured Query Language, or SQL, is the standard language for managing and querying data in relational databases. Whether you’re a novice looking to dive into the world of databases or an experienced professional wanting to test your basics, this SQL quiz is for you.
We’ve compiled 40 multiple-choice questions covering all the fundamental concepts of SQL. After each question, you’ll find the correct answer along with a brief explanation to help you understand the topic better. Ready? Let’s dive in!
1. Which SQL statement is used to extract data from a database?
Answer:
Explanation:
The SELECT statement is used to select data from a database. The result is stored in a result table.
2. Which SQL keyword is used to sort the result-set?
Answer:
Explanation:
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
3. Which SQL keyword is used to delete data from a database?
Answer:
Explanation:
The DELETE statement is used to delete existing records in a table.
4. What SQL clause is used to filter the results of a query?
Answer:
Explanation:
The WHERE clause is used to filter records and extract only those records that fulfill a specified condition.
5. Which SQL function returns the number of rows in a query?
Answer:
Explanation:
The COUNT function returns the number of rows that matches a specified criterion.
6. Which SQL keyword gives a nickname to a table (typically for the purpose of a join)?
Answer:
Explanation:
An alias only exists for the duration of the query. It can be used for column names and table names.
7. What type of SQL command is INSERT INTO?
Answer:
Explanation:
INSERT INTO is a command used to insert new records into a table, making it a Data Manipulation Language (DML) command.
8. Which command is used to remove a table from a database?
Answer:
Explanation:
The DROP command is used to delete an existing table (or database).
9. In SQL, which command is used to make changes made by a transaction permanent in a database?
Answer:
Explanation:
The COMMIT command saves all the transactions to the database since the last COMMIT or ROLLBACK command.
10. What will the SQL query SELECT * FROM Users WHERE FirstName = ‘John’ AND LastName = ‘Doe’; return?
Answer:
Explanation:
The query uses the AND operator, which means both conditions specified must be true for a row to be included in the result set.
11. What is the default sort order of the ORDER BY clause in SQL?
Answer:
Explanation:
By default, the ORDER BY clause sorts the records in ascending order.
12. Which SQL statement is used to update data in a database?
Answer:
Explanation:
The UPDATE statement is used to modify existing records in a table.
13. Which SQL function calculates the total sum of a numeric column?
Answer:
Explanation:
The SUM function is used to get the sum of a numeric column.
14. What does the SQL LIKE operator do?
Answer:
Explanation:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
15. Which SQL clause is used to group rows that have the same values in specified columns?
Answer:
Explanation:
The GROUP BY statement groups rows that have the same values in specified columns into aggregate data.
16. What does SQL stand for?
Answer:
Explanation:
SQL stands for Structured Query Language, a standard language for storing, manipulating, and retrieving data in databases.
17. In SQL, which operator is used to test for a value within a range of values?
Answer:
Explanation:
The BETWEEN operator selects values within a given range.
18. Which SQL statement is used to add, modify, or drop columns in a table?
Answer:
Explanation:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
19. What is the purpose of the SQL DISTINCT keyword?
Answer:
Explanation:
The DISTINCT keyword is used in SELECT statements to eliminate duplicate rows in the result set.
20. Which SQL command creates a new database?
Answer:
Explanation:
The CREATE DATABASE statement is used to create a new SQL database.
21. Which SQL statement is used to return only different values?
Answer:
Explanation:
The SELECT DISTINCT statement is used to return only distinct (different) values.
22. In SQL, which command is used to roll back a transaction?
Answer:
Explanation:
The ROLLBACK command restores the database to the last committed state.
23. Which SQL function returns the highest value in a column?
Answer:
Explanation:
The MAX function returns the highest value of the selected column.
24. What is the SQL keyword used to retrieve a maximum of 5 records from a table?
Answer:
Explanation:
The TOP clause is used to specify the number of records to return.
25. What does the SQL NULL value represent?
Answer:
Explanation:
A field with a NULL value is a field with no value, representing missing or unknown data.
26. Which SQL clause allows filtering AFTER the GROUP BY clause has been applied?
Answer:
Explanation:
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
27. What SQL keyword is used to introduce an alias?
Answer:
Explanation:
The "AS" keyword is used in SQL to rename a column or table using an alias.
28. Which SQL keyword combines the result of two or more SELECT statements?
Answer:
Explanation:
The UNION operator is used to combine the result-set of two or more SELECT statements.
29. What is the main difference between UNION and UNION ALL?
Answer:
Explanation:
UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.
30. Which SQL statement is used to create a stored procedure in a database?
Answer:
Explanation:
The CREATE PROCEDURE statement is used to create a stored procedure.
31. In SQL, how do you select all the records from a table named “Users” where the “FirstName” column is not empty?
Answer:
Explanation:
To ensure that the "FirstName" column is not empty, we need to check if it's not NULL.
32. Which SQL function returns the smallest value of the selected column?
Answer:
Explanation:
The MIN function returns the smallest value of the selected column.
33. What type of SQL command is GRANT?
Answer:
Explanation:
The GRANT command is a Data Control Language (DCL) command that provides specific privileges to users.
34. Which SQL statement is used to delete a table definition and all its data?
Answer:
Explanation:
The DROP TABLE statement deletes the table definition and all data, indexes, triggers, constraints, and permission specifications for that table.
35. What does the TRUNCATE TABLE statement do in SQL?
Answer:
Explanation:
The TRUNCATE TABLE statement deletes all rows from a table, but the table structure and its columns, constraints, indexes, and so on, remain.
36. Which SQL clause specifies a condition while fetching data from a single table or by joining multiple tables?
Answer:
Explanation:
The ON clause is used to specify conditions when joining tables. The conditions determine how the tables are related.
37. Which type of SQL join returns only the rows where there is a match in both the left and the right table?
Answer:
Explanation:
An INNER JOIN gets the records that have matching values in both tables.
38. If you use a LEFT JOIN between two tables, which of the following statements is true?
Answer:
Explanation:
A LEFT JOIN will fetch all records from the left table and only the matching ones from the right table. If there's no match, NULL values will be displayed for columns from the right table.
39. What does a FULL JOIN do in SQL?
Answer:
Explanation:
A FULL JOIN returns all records when there is a match in one of the tables. So, it will show all records from both tables and will display NULL for columns from the table that doesn’t have a match.
40. If you perform a CROSS JOIN between a table with 5 rows and a table with 8 rows, how many rows will the result set contain?
Answer:
Explanation:
A CROSS JOIN gives the Cartesian product of the two tables. It returns all possible combinations of all rows. Therefore, it'd be 5 * 8 = 40 rows.