wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

SQL Subqueries and Functions Quiz

Total questions: 60

Worksheet time: 30mins

Name
Class
Date
1.

Which of the following best describes a scalar subquery?

a)

A subquery that returns multiple rows and columns

b)

A subquery that returns only one row but multiple columns

c)

A subquery that returns a single value (one row, one column)

d)

A subquery used only in the FROM clause

2.

What is the key difference between correlated and non-correlated subqueries?

a)

Correlated subqueries can return multiple columns, while non-correlated cannot

b)

Correlated subqueries reference columns from the outer query, while non-correlated do not

c)

Non-correlated subqueries always use the FULL JOINs, while correlated subqueries never do

d)

Non-correlated subqueries are slower than correlated subqueries

3.

Which of the following scenarios is best suited for a self-join?

a)

Comparing rows within the same table

b)

Combining results of multiple queries

c)

Traversing hierarchical data recursively

d)

Removing duplicates in a table

4.

What is the result of a CROSS JOIN between two tables?

a)

Rows matched by common columns

b)

All combinations of rows from both tables (Cartesian product)

c)

Only rows with NULL values from both tables

d)

A join that eliminates duplicate rows

5.

Which JOIN type returns all rows from both tables, whether they match or not?

a)

INNER JOIN

b)

LEFT JOIN

c)

RIGHT JOIN

d)

FULL OUTER JOIN

6.

The SQL operation UNION ALL differs from UNION because:

a)

UNION ALL requires identical column names, while UNION does not

b)

UNION ALL includes duplicates, while UNION removes duplicates

c)

UNION ALL is slower than UNION

d)

UNION ALL can combine tables with different data types

7.

Which set operation would you use to find rows that are present in the first query but not in the second?

a)

UNION

b)

INTERSECT

c)

EXCEPT (or MINUS)

d)

CROSS JOIN

8.

What is the main advantage of using a Common Table Expression (CTE)?

a)

It improves execution speed in all cases

b)

It allows defining temporary result sets for better readability and modularity

c)

It automatically indexes the query result to worsen readability and modularity

d)

It permanently stores query results in the database

9.

Recursive CTEs are commonly used for which type of problem?

a)

Eliminating duplicates in a table such as table or graph traversal

b)

Joining multiple unrelated tables such as organizational charts or pie traversal

c)

Solving hierarchical queries such as organizational charts or graph traversal

d)

Performing aggregate calculations

10.

Which clause is necessary in a recursive CTE to prevent infinite loops?

a)

LIMIT

b)

WHERE

c)

UNION ALL

d)

ORDER BY

11.

Which of the following functions assigns a unique sequential number to each row, regardless of ties?

a)

RANK()

b)

DENSE_RANK()

c)

ROW_NUMBER()

d)

NTILE()

12.

What is the key difference between RANK() and DENSE_RANK()?

a)

RANK() skips numbers for ties, while DENSE_RANK() does not

b)

DENSE_RANK() is faster than RANK()

c)

RANK() works only with numeric columns, DENSE_RANK() works with all columns

d)

DENSE_RANK() ignores NULL values, RANK() does not

13.

Which analytic function allows you to access the value of a column from the next row in the result set?

a)

LAG()

b)

LEAD()

c)

FIRST_VALUE()

d)

NTH_VALUE()

14.

The function FIRST_VALUE() is typically used to:

a)

Return the earliest value in an ordered partition

b)

Return the value of the first column in the table

c)

Return the first row inserted into a table

d)

Return the first non-null value in the database

15.

Which clause is required when using aggregate window functions like SUM() OVER() or AVG() OVER()?

a)

HAVING

b)

OVER()

c)

GROUP BY

d)

PARTITION BY must always be included

16.

What is the main purpose of a stored procedure?

a)

To store data permanently in the database

b)

To save a precompiled set of SQL statements that can accept parameters and include control flow logic

c)

To create temporary tables in memory

d)

To optimize execution plans automatically

17.

Which statement about User-Defined Functions (UDFs) is correct?

a)

Scalar UDFs return tables, while Table-Valued UDFs return single values

b)

Scalar UDFs return a single value, while Table-Valued UDFs return a set of rows

c)

UDFs cannot accept parameters

d)

UDFs can be used only inside stored procedures

18.

A BEFORE trigger is most often used to:

a)

Enforce business rules before a row is inserted or updated

b)

Automatically back up the database before a transaction

c)

Execute instead of a stored procedure

d)

Prevent deadlocks in transaction processing

19.

Which of the following is a potential pitfall of triggers?

a)

They cannot enforce referential integrity

b)

They may cause hidden performance issues and make debugging harder

c)

They cannot interact with multiple tables

d)

They always require materialized views to function

20.

What is the difference between an updatable view and a materialized view?

a)

Updatable views store data, while materialized views do not

b)

Materialized views store the query result physically, while updatable views are virtual and reflect base table changes in real-time

c)

Updatable views are faster than materialized views, and the materialized views catch the query result logically and periodically in a scheduled time

d)

Materialized views can only be created with UNION ALL

21.

What is the primary role of the system catalog (data dictionary) in a DBMS?

a)

To store user data and transactions

b)

To maintain metadata about the database, such as schema, users, and constraints

c)

To optimize hardware utilization

d)

To act as a backup system for disaster recovery

22.

Which of the following is NOT typically stored in the system catalog?

a)

Table definitions and schema information

b)

User privileges and security roles

c)

System statistics for optimization

d)

Actual row-level data of user tables

23.

How does a DBMS ensure that the metadata in the system catalog is always up-to-date?

a)

By requiring manual updates from database administrators

b)

By automatically updating metadata whenever schema changes occur

c)

By rebuilding indexes periodically

d)

By copying metadata into log files during transactions

24.

The Information Schema in SQL provides standardized views of metadata. Which view lists all the tables in a database?

a)

INFORMATION_SCHEMA.COLUMNS

b)

INFORMATION_SCHEMA.TABLES

c)

INFORMATION_SCHEMA.KEY_COLUMN_USAGE

d)

INFORMATION_SCHEMA.ROUTINES

25.

Which Information Schema view would you query to find details about stored procedures and functions?

a)

ROUTINES

b)

COLUMNS

c)

INDEXES

d)

KEY_COLUMN_USAGE

26.

In PostgreSQL, which catalog schema stores system-level metadata?

a)

sys

b)

pg_catalog

c)

dba_

d)

information_schema

27.

In SQL Server, which schema provides access to system metadata views?

a)

pg_catalog

b)

sys

c)

dba_

d)

db_catalog

28.

In Oracle, metadata views often begin with which prefix?

a)

sys_

b)

pg_

c)

dba_

d)

meta_

29.

Why is the system catalog important for database administration?

a)

It stores raw business data for decision-making

b)

It provides metadata needed for monitoring, auditing, performance tuning, and backup/recovery

c)

It acts as a temporary storage for transactions

d)

It is used only during database installation

30.

How does the system catalog help in query optimization?

a)

By storing execution plans permanently

b)

By providing metadata statistics such as table size, index selectivity, and column distributions

c)

By rewriting queries into procedural SQL

d)

By preventing deadlocks in transaction processing

31.

Which of the following is the correct order of query processing steps in a DBMS?

a)

Optimization → Execution → Parsing → Translation

b)

Parsing → Translation → Optimization → Execution

c)

Translation → Parsing → Execution → Optimization

d)

Parsing → Execution → Optimization → Translation

32.

During query parsing, what is the main task performed by the DBMS?

a)

Converting SQL into relational algebra expressions

b)

Checking syntax and validating the query structure

c)

Optimizing the query for execution

d)

Translating the query into a physical execution plan

33.

During query parsing, what is the main task performed by the DBMS?

a)

Converting SQL into relational algebra expressions

b)

Checking query syntax and validating against schema definitions

c)

Estimating costs of query execution plans

d)

Rewriting queries for optimization

34.

Heuristic-based optimization primarily focuses on:

a)

Applying cost models to choose the cheapest execution plan

b)

Using fixed rules, such as pushing down selections and projections early

c)

Using machine learning models for query predictions

d)

Reducing transaction deadlocks during execution

35.

In cost-based optimization, the DBMS relies heavily on:

a)

User-defined query hints only

b)

Statistics such as table size, index selectivity, and cardinality

c)

Random selection of execution plans

d)

Only I/O costs, ignoring CPU and memory

36.

Why is cardinality estimation critical in query optimization?

a)

It determines the logical schema of the database

b)

It helps estimate the number of rows processed at each step, which affects cost and plan selection

c)

It defines access privileges for query execution

d)

It is only used for distributed databases

37.

Which SQL command is commonly used to analyze and view the execution plan of a query?

a)

SHOW PLAN

b)

DESCRIBE PLAN

c)

EXPLAIN

d)

ANALYZE EXECUTION

38.

Which join algorithm is most efficient when one table is small and the other is large, especially with indexes?

a)

Hash Join

b)

Sort-Merge Join

c)

Nested Loop Join

d)

Merge Join

39.

Which join algorithm works best when both input tables are already sorted?

a)

Nested Loop Join

b)

Hash Join

c)

Sort-Merge Join

d)

Cross Join

40.

Query rewriting techniques, such as predicate pushdown and subquery unnesting, aim to:

a)

Change the database schema for faster queries

b)

Simplify queries and reduce the amount of data processed

c)

Replace SQL with procedural code

d)

Avoid the use of indexes during query execution

41.

How do database statistics influence query optimization?

a)

They force the optimizer to use only heuristic rules

b)

They provide information about data distribution, table size, and index selectivity, guiding the optimizer in plan selection

c)

They are used only for transaction management

d)

They have no effect on execution plans

42.

Which of the following scenarios violates the Atomicity property of a transaction?

a)

A bank transfer deducts money from one account but fails to credit the other

b)

A query returns stale data because another transaction updated it concurrently

c)

A transaction reads uncommitted changes from another transaction

d)

A database crash causes data loss after commit

43.

The Durability property in ACID ensures that:

a)

All transactions must be executed sequentially

b)

Once a transaction is committed, its effects persist even if the system crashes

c)

Transactions will always roll back after a system failure

d)

No two transactions can run at the same time

44.

In the Two-Phase Locking (2PL) protocol, which of the following is true?

a)

A transaction can release locks before acquiring new ones

b)

Once a transaction releases a lock, it cannot acquire any new locks

c)

Transactions never hold multiple locks at the same time

d)

Locks are only applied at the row level

45.

Strict Two-Phase Locking (Strict 2PL) differs from Basic 2PL by:

a)

Allowing non-conflicting transactions to bypass locks

b)

Releasing all locks only after the transaction commits

c)

Using timestamps instead of locks

d)

Applying locks only to read operations

46.

In timestamp-based concurrency control, each data item is associated with:

a)

Only a read timestamp

b)

Only a write timestamp

c)

Both a read timestamp and a write timestamp

d)

A lock table entry

47.

Optimistic Concurrency Control works best in which type of environment?

a)

High-contention environments with frequent conflicts

b)

Environments with few conflicts, where validation at commit time is cheaper than locking

c)

Systems where only read operations occur

d)

Real-time systems requiring strict deadlines

48.

Multi-Version Concurrency Control (MVCC) improves concurrency by:

a)

Allowing multiple versions of data items so readers don't block writers and vice versa

b)

Assigning unique timestamps to each transaction to prevent deadlocks

c)

Allowing updates only at the page level

d)

Forcing transactions into a strict serial order

49.

Which isolation level allows dirty reads?

a)

Read Uncommitted

b)

Read Committed

c)

Repeatable Read

d)

Serializable

50.

A phantom read occurs when:

a)

A transaction reads the same row twice and gets different values

b)

A transaction reads uncommitted data from another transaction

c)

A transaction re-executes a query and finds new rows that were inserted by another transaction

d)

A transaction is rolled back due to deadlock

51.

Which of the following is NOT a common deadlock handling technique?

a)

Detection and resolution

b)

Prevention through resource ordering

c)

Avoidance using wait-die or wound-wait schemes

d)

Automatic transaction commit without locks

52.

Which of the following is an example of a transaction failure?

a)

A hard disk crash causes data loss

b)

A system power outage occurs during execution

c)

A transaction is aborted due to a logical error (e.g., divide by zero)

d)

Corruption of database files due to bad sectors

53.

A media failure refers to:

a)

Sudden termination of a transaction due to concurrency conflicts

b)

Software bugs in the DBMS engine

c)

Physical damage to storage devices, such as disk crashes

d)

Memory overflow during query execution

54.

The Write-Ahead Logging (WAL) principle requires that:

a)

Log records are written only after the transaction commits

b)

Changes to the database are written first, followed by log records

c)

Log records are written to stable storage before the actual database changes are applied

d)

Log records are optional if checkpoints exist

55.

What is the purpose of a checkpoint in recovery management?

a)

To remove old transactions from the system catalog

b)

To speed up recovery by reducing the amount of log data that must be processed

c)

To delete all transaction logs after a commit

d)

To prevent deadlocks during system failure

56.

In fuzzy checkpointing, the system:

a)

Suspends all transactions until the checkpoint is complete

b)

Allows ongoing transactions while marking a checkpoint in the log

c)

Deletes uncommitted transactions from the buffer

d)

Performs a full backup of the database automatically

57.

The ARIES recovery algorithm performs which sequence of phases?

a)

Undo → Redo → Analysis

b)

Analysis → Redo → Undo

c)

Redo → Undo → Analysis

d)

Backup → Restore → Redo

58.

Which backup strategy involves saving the entire database every time?

a)

Full Backup

b)

Incremental Backup

c)

Differential Backup

d)

Transaction Log Backup

59.

An incremental backup stores:

a)

Only the data changed since the last full backup

b)

The entire database plus all transaction logs

c)

Data that has changed since the last checkpoint

d)

Data that has changed since the last backup (full or incremental)

60.

To restore a database after failure, the general recovery procedure is:

a)

Apply logs first, then restore from backup

b)

Restore from backup, then apply logs to bring the database to a consistent state

c)

Run checkpoints only

d)

Re-execute all user transactions manually