0
0
SQLquery~20 mins

Why subqueries are needed in SQL - Challenge Your Understanding

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
Why use subqueries in SQL?
Which of the following best explains why subqueries are needed in SQL?
ATo perform a query inside another query to filter or calculate based on intermediate results.
BTo speed up the database server by running multiple queries at once.
CTo replace the need for joins between tables.
DTo store data permanently in the database.
Attempts:
2 left
💡 Hint
Think about how you can use one query's result inside another query.
query_result
intermediate
2:00remaining
Output of a subquery filtering example
Given the tables Employees (id, name, salary) and Departments (id, name, manager_id), what is the output of this query?
SELECT name FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees);
AAll employee names regardless of salary.
BNames of employees whose salary is above the average salary of all employees.
CNames of employees whose salary is below the average salary of all employees.
DAn error because subqueries cannot be used in WHERE clause.
Attempts:
2 left
💡 Hint
Look at what the subquery calculates and how it is used in the WHERE clause.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for a subquery in SELECT
Which option shows the correct syntax to use a subquery inside a SELECT statement to get the department name for each employee?
SQL
Tables: Employees(id, name, department_id), Departments(id, name)
ASELECT name, (SELECT name FROM Departments WHERE id = Employees.id) AS dept_name FROM Employees;
BSELECT name, SELECT name FROM Departments WHERE id = Employees.department_id AS dept_name FROM Employees;
CSELECT name, (SELECT name FROM Departments WHERE id = department_id) dept_name FROM Employees;
DSELECT name, (SELECT name FROM Departments WHERE id = Employees.department_id) AS dept_name FROM Employees;
Attempts:
2 left
💡 Hint
Check the placement of the subquery and alias syntax.
optimization
advanced
2:00remaining
Why might subqueries cause performance issues?
Which reason best explains why subqueries can sometimes slow down SQL queries?
ABecause subqueries store temporary data on disk.
BBecause subqueries prevent the use of indexes.
CBecause the subquery runs once for each row of the outer query, increasing execution time.
DBecause subqueries always lock the entire table.
Attempts:
2 left
💡 Hint
Think about how many times the subquery might execute.
🔧 Debug
expert
2:00remaining
Find the error in this subquery usage
What error will this query produce?
SELECT name FROM Employees WHERE salary > (SELECT salary FROM Employees WHERE id = 10);
AError: Subquery returns zero rows if no employee with id=10 exists.
BNo error, query runs correctly.
CError: Subquery returns more than one row.
DError: Subquery cannot reference the same table as outer query.
Attempts:
2 left
💡 Hint
What happens if the subquery finds no matching row?