SQL MCQs – Advanced SQL Concepts

11.) What is the purpose of the EXISTS clause in SQL?

A) To check if a table exists
B) To optimize queries
C) To create a temporary table
D) To verify the presence of rows in a subquery

Answer: Option D

Explanation: EXISTS checks whether a subquery returns any rows.

12.) Which of the following best describes a self-join?

A) A join between two different tables
B) A join that excludes null values
C) A join of a table with itself
D) A join that merges columns

Answer: Option C

Explanation: A self-join joins a table to itself using aliases to differentiate table instances.

13.) What is the primary use of a FULL OUTER JOIN?

A) Combines rows with matching values only
B) Includes all rows from both tables, matching where possible
C) Joins rows based on primary keys
D) Joins rows with null values only

Answer: Option B

Explanation: FULL OUTER JOIN returns all rows from both tables, with nulls for unmatched rows.

14.) How does the LAG() function work in SQL?

A) Returns the previous value in a set
B) Returns the next value in a set
C) Computes a running total
D) Returns the average value of a set

Answer: Option A

Explanation: LAG() retrieves the value of a column from a previous row within the same partition.

15.) What is the purpose of the ROW_NUMBER() function?

A) Assigns unique numbers to rows within a partition
B) Calculates the total number of rows in a table
C) Assigns ranks with gaps for duplicates
D) Creates a unique index

Answer: Option A

Explanation: ROW_NUMBER() assigns a unique sequential number to rows in a result set.

16.) Which SQL keyword is used to combine results from multiple queries?

A) JOIN
B) UNION
C) INTERSECT
D) EXCEPT

Answer: Option B

Explanation: UNION combines the results of two queries into a single result set.

17.) What is the difference between DELETE and TRUNCATE?

A) DELETE removes all rows; TRUNCATE removes specific rows
B) DELETE is faster than TRUNCATE
C) DELETE resets indexes; TRUNCATE does not
D) DELETE removes specific rows; TRUNCATE removes all rows

Answer: Option D

Explanation: DELETE removes specific rows and logs changes, whereas TRUNCATE removes all rows without logging individual deletions.

18.) What is a materialized view?

A) A dynamically updated query result
B) A schema for table design
C) A temporary table for joins
D) A stored query result that is periodically updated

Answer: Option D

Explanation: A materialized view stores the result of a query and updates it periodically.

19.) What is a scalar subquery?

A) A subquery that returns a single value
B) A subquery that returns multiple rows
C) A subquery used in joins
D) A subquery that updates data

Answer: Option A

Explanation: A scalar subquery returns a single value and is often used in SELECT or WHERE clauses.

20.) How does PARTITION BY enhance window functions?

A) Joins rows with matching values
B) Aggregates data within a table
C) Divides data into subsets for analysis
D) Eliminates null values

Answer: Option C

Explanation: PARTITION BY divides data into subsets for window functions, allowing calculations within each partition.

Leave a Reply

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