SQL MCQ

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?

A. GET
B. EXTRACT
C. FETCH
D. SELECT

Answer:

D. SELECT

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?

A. SORT BY
B. ORDER BY
C. ARRANGE BY
D. ALIGN BY

Answer:

B. ORDER BY

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?

A. REMOVE
B. DELETE
C. DESTROY
D. ERASE

Answer:

B. DELETE

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?

A. FILTER BY
B. SELECT BY
C. WHERE
D. LOCATE

Answer:

C. WHERE

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?

A. NUM
B. COUNT
C. SUM
D. TOTAL

Answer:

B. COUNT

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)?

A. NICKNAME
B. AS
C. ALIAS
D. CALL

Answer:

C. ALIAS

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?

A. Data Definition Language
B. Data Manipulation Language
C. Data Control Language
D. Transaction Control Language

Answer:

B. Data Manipulation Language

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?

A. REMOVE
B. ERASE
C. DROP
D. DELETE

Answer:

C. DROP

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?

A. SAVE
B. SAVEPOINT
C. COMMIT
D. HOLD

Answer:

C. COMMIT

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?

A. All users with the first name 'John'.
B. All users with the last name 'Doe'.
C. All users with the first name 'John' or the last name 'Doe'.
D. All users with the first name 'John' and the last name 'Doe'.

Answer:

D. All users with the first name 'John' and the last name 'Doe'.

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?

A. Descending
B. Ascending
C. Random
D. Hierarchical

Answer:

B. Ascending

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?

A. MODIFY
B. REVISE
C. CHANGE
D. UPDATE

Answer:

D. UPDATE

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?

A. SUMMARIZE
B. TOTAL
C. COUNT
D. SUM

Answer:

D. SUM

Explanation:

The SUM function is used to get the sum of a numeric column.

14. What does the SQL LIKE operator do?

A. Compares two values for equality
B. Finds rows that match a specified pattern
C. Calculates the sum of values
D. Sorts result in ascending or descending order

Answer:

B. Finds rows that match a specified pattern

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?

A. GROUP BY
B. ORDER BY
C. PARTITION BY
D. SORT BY

Answer:

A. GROUP BY

Explanation:

The GROUP BY statement groups rows that have the same values in specified columns into aggregate data.

16. What does SQL stand for?

A. Structured Question Language
B. Simple Query Language
C. Structured Query Language
D. System Query Language

Answer:

C. Structured Query Language

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?

A. IN
B. BETWEEN
C. LIKE
D. CONTAINS

Answer:

B. BETWEEN

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?

A. MODIFY TABLE
B. CHANGE TABLE
C. ALTER TABLE
D. UPDATE TABLE

Answer:

C. ALTER TABLE

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?

A. To sort results
B. To remove duplicates
C. To update records
D. To join tables

Answer:

B. To remove duplicates

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?

A. NEW DATABASE
B. ADD DATABASE
C. CREATE DATABASE
D. INIT DATABASE

Answer:

C. CREATE DATABASE

Explanation:

The CREATE DATABASE statement is used to create a new SQL database.

21. Which SQL statement is used to return only different values?

A. SELECT UNIQUE
B. SELECT DIFFERENT
C. SELECT DISTINCT
D. SELECT SEPARATE

Answer:

C. SELECT DISTINCT

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?

A. REVERT
B. UNDO
C. ROLLBACK
D. RESET

Answer:

C. ROLLBACK

Explanation:

The ROLLBACK command restores the database to the last committed state.

23. Which SQL function returns the highest value in a column?

A. MAX
B. HIGH
C. UP
D. TOP

Answer:

A. MAX

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?

A. TOP 5
B. FIRST 5
C. BEST 5
D. LEADING 5

Answer:

A. TOP 5

Explanation:

The TOP clause is used to specify the number of records to return.

25. What does the SQL NULL value represent?

A. Zero
B. Empty string
C. Undefined or missing data
D. False

Answer:

C. Undefined or missing data

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?

A. FILTER BY
B. WHERE
C. HAVING
D. AFTER

Answer:

C. HAVING

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?

A. INTRODUCE
B. AS
C. ALIAS FOR
D. NAME

Answer:

B. AS

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?

A. COMBINE
B. UNION
C. JOIN
D. MERGE

Answer:

B. UNION

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?

A. UNION ALL allows duplicate records.
B. UNION requires the same number of columns in each SELECT statement.
C. UNION ALL can only be used with two SELECT statements.
D. UNION allows different data types in the SELECT statements.

Answer:

A. UNION ALL allows duplicate records.

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?

A. CREATE STORED PROCEDURE
B. ADD PROCEDURE
C. DEFINE PROCEDURE
D. MAKE PROCEDURE

Answer:

A. CREATE STORED PROCEDURE

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?

A. SELECT * FROM Users WHERE FirstName = ''
B. SELECT * FROM Users WHERE FirstName != ''
C. SELECT * FROM Users WHERE FirstName IS NOT NULL
D. SELECT * FROM Users WHERE FirstName EXISTS

Answer:

C. SELECT * FROM Users WHERE FirstName IS NOT NULL

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?

A. LEAST
B. MINIMUM
C. MIN
D. SMALL

Answer:

C. MIN

Explanation:

The MIN function returns the smallest value of the selected column.

33. What type of SQL command is GRANT?

A. Data Definition Language
B. Data Manipulation Language
C. Data Control Language
D. Transaction Control Language

Answer:

C. Data Control Language

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?

A. DELETE TABLE
B. REMOVE TABLE
C. DROP TABLE
D. TRUNCATE TABLE

Answer:

C. DROP TABLE

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?

A. Deletes the table definition
B. Deletes only the table data but not its schema
C. Modifies the table data type
D. Makes a copy of the table

Answer:

B. Deletes only the table data but not its schema

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?

A. AND
B. ALIAS
C. ON
D. WHERE

Answer:

C. ON

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?

A. INNER JOIN
B. LEFT JOIN
C. RIGHT JOIN
D. FULL JOIN

Answer:

A. INNER JOIN

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?

A. It retrieves only the matched records from both tables.
B. It retrieves all the records from the right table and the matched records from the left table.
C. It retrieves all the records from the left table and the matched records from the right table.
D. It retrieves all the records from both tables regardless of a match.

Answer:

C. It retrieves all the records from the left table and the matched records from the right table.

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?

A. Returns only the rows where there is a match in one of the tables.
B. Returns all the rows when there is a match in one of the tables.
C. Returns only the rows where there is a match in both tables.
D. Returns all rows from both tables and puts NULL in the columns from the table that doesn’t have a match.

Answer:

D. Returns all rows from both tables and puts NULL in the columns from the table that doesn’t have a match.

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?

A. 13
B. 40
C. 3
D. 8

Answer:

B. 40

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.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top