SQL MCQs – Advanced SQL Concepts

Advanced SQL concepts enhance the ability to solve complex database problems and improve performance. These include topics like subqueries, common table expressions (CTEs), window functions, indexes, and advanced join operations. Understanding advanced SQL equips you to handle large datasets efficiently, write optimized queries, and manage database systems effectively. Practicing multiple-choice questions (MCQs) on these topics will strengthen your problem-solving skills and boost confidence for technical interviews.

1.) What is the purpose of a Common Table Expression (CTE)?

A) To create temporary tables
B) To optimize indexing
C) To perform transactions
D) To simplify complex queries by defining a temporary result set

Answer: Option D

Explanation: A CTE provides a way to define a temporary result set that can be referred to within a query.

2.) Which SQL clause is used to filter grouped data?

A) WHERE
B) HAVING
C) GROUP BY
D) ORDER BY

Answer: Option B

Explanation: HAVING filters aggregated data after grouping, whereas WHERE filters rows before grouping.

3.) What is a window function in SQL?

A) A function that operates on a subset of rows
B) A function that partitions data for analysis
C) A function that performs calculations across a window of rows
D) All of the above

Answer: Option D

Explanation: Window functions allow calculations across a set of table rows related to the current row, often used with OVER() and PARTITION BY.

4.) What is the main difference between a UNION and a UNION ALL?

A) UNION removes duplicates; UNION ALL does not
B) UNION is faster than UNION ALL
C) UNION ALL removes duplicates; UNION does not
D) UNION merges columns; UNION ALL merges rows

Answer: Option A

Explanation: UNION removes duplicate rows, whereas UNION ALL includes all rows, including duplicates.

5.) What is the purpose of an index in SQL?

A) To store duplicate data
B) To sort data
C) To speed up data retrieval
D) To ensure data integrity

Answer: Option C

Explanation: Indexes improve the speed of data retrieval operations by creating a sorted structure of table data.

6.) What does the NVL() function do in SQL?

A) Calculates the sum of a column
B) Converts a null value to a specified value
C) Removes duplicate rows from a query
D) Joins two tables based on a condition

Answer: Option B

Explanation: NVL() replaces null values with a specified default value, commonly used in Oracle databases.

7.) How does a CROSS JOIN behave?

A) Matches rows from two tables based on a condition
B) Joins rows with matching primary keys
C) Produces a Cartesian product of two tables
D) Joins rows with null values

Answer: Option C

Explanation: CROSS JOIN produces all possible combinations of rows from two tables.

8.) What is the primary advantage of a clustered index?

A) Data is stored in order based on the index
B) Faster insertions
C) Supports multiple indexes per table
D) Reduces storage requirements

Answer: Option A

Explanation: A clustered index determines the physical order of data in a table, speeding up range-based queries.

9.) Which statement about stored procedures is true?

A) They can be used to encapsulate complex SQL queries and business logic
B) They are automatically optimized for performance by the database
C) They cannot accept input parameters
D) They are executed automatically on database startup

Answer: Option A

Explanation: Stored procedures allow encapsulating logic and queries, making database operations reusable and efficient.

10.) How does the SQL MERGE statement work?

A) Merges two tables into one new table
B) Performs insert, update, or delete operations based on a condition
C) Combines columns from two queries into one result set
D) Optimizes indexes for faster retrieval

Answer: Option B

Explanation: MERGE allows performing insert, update, or delete actions depending on whether a match is found in the specified condition.

Leave a Reply

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