0
0
DBMS Theoryknowledge~20 mins

Subqueries and nested queries in DBMS Theory - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of subqueries
What is the main purpose of using a subquery in an SQL statement?
ATo create a new table from existing tables
BTo perform a query inside another query to filter or calculate values
CTo permanently store data in the database
DTo delete rows from multiple tables simultaneously
Attempts:
2 left
💡 Hint
Think about how one query can depend on the result of another.
📋 Factual
intermediate
2:00remaining
Identifying valid subquery placement
Which of the following SQL clauses can contain a subquery?
AWHERE, FROM, and SELECT clauses
BINSERT and DELETE clauses only
CCREATE and DROP clauses only
DALTER and GRANT clauses only
Attempts:
2 left
💡 Hint
Subqueries are often used to filter rows or generate data sets.
🚀 Application
advanced
2:00remaining
Output of nested subquery with aggregation
What will be the output of this query?
SELECT employee_id FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = 10);
AList of employee IDs with salary greater than the average salary in department 10
BList of employee IDs only from department 10
CList of employee IDs with salary greater than the overall average salary of all employees
DSyntax error due to incorrect subquery
Attempts:
2 left
💡 Hint
Look at the subquery filtering by department_id = 10 and calculating average salary.
🔍 Analysis
advanced
2:00remaining
Analyzing error in correlated subquery
Consider this query:
SELECT e1.employee_id FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department_id = e2.department_id);

What error or issue will this query produce?
AIt will cause a runtime error because the subquery returns multiple rows
BIt will cause a syntax error due to alias misuse
CIt will run correctly and return employees with salary above their department average
DIt will return no rows because the condition is always false
Attempts:
2 left
💡 Hint
Check if the subquery is correlated properly and returns a single value per outer row.
Reasoning
expert
2:00remaining
Determining the number of rows returned by nested queries
Given two tables:
  • departments(department_id, department_name)
  • employees(employee_id, department_id, salary)

What is the number of rows returned by this query?
SELECT department_id FROM departments WHERE department_id IN (SELECT department_id FROM employees WHERE salary > 50000);
ANumber of employees in departments with salary less than or equal to 50000
BNumber of employees with salary greater than 50000
CTotal number of departments regardless of employee salaries
DNumber of departments that have at least one employee with salary greater than 50000
Attempts:
2 left
💡 Hint
The subquery finds departments with employees earning more than 50000; the outer query filters departments accordingly.