0
0
SQLquery~10 mins

Subquery in WHERE clause in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all employees who work in the department with ID 5.

SQL
SELECT * FROM employees WHERE department_id = [1];
Drag options to blanks, or click blank then click option'
A5
B10
Cdepartment_id
D'5'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the number 5, which makes it a string.
Using the column name instead of a value.
2fill in blank
medium

Complete the code to select all employees whose department_id is in the list of department IDs from the departments table.

SQL
SELECT * FROM employees WHERE department_id IN (SELECT [1] FROM departments);
Drag options to blanks, or click blank then click option'
Asalary
Bemployee_id
Cname
Ddepartment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting employee_id instead of department_id in the subquery.
Selecting a column unrelated to departments.
3fill in blank
hard

Fix the error in the query to select employees who have a salary greater than the average salary in their department.

SQL
SELECT * FROM employees WHERE salary > (SELECT AVG([1]) FROM employees WHERE department_id = employees.department_id);
Drag options to blanks, or click blank then click option'
Aemployee_id
B*
Csalary
Ddepartment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using department_id inside AVG, which is not numeric for averaging.
Using * inside AVG, which is invalid.
4fill in blank
hard

Fill both blanks to select all products with a price higher than the average price in their category.

SQL
SELECT * FROM products WHERE price > (SELECT [1] FROM products WHERE category_id = [2]);
Drag options to blanks, or click blank then click option'
AAVG(price)
Bproducts.category_id
Cdepartment_id
DMAX(price)
Attempts:
3 left
💡 Hint
Common Mistakes
Using MAX(price) instead of AVG(price).
Using department_id instead of products.category_id.
5fill in blank
hard

Fill all three blanks to select employees whose salary is greater than the average salary in their department and whose department name is 'Sales'.

SQL
SELECT * FROM employees WHERE salary > (SELECT AVG([1]) FROM employees WHERE department_id = [2]) AND department_id = (SELECT id FROM departments WHERE name = [3]);
Drag options to blanks, or click blank then click option'
Asalary
Bemployees.department_id
C'Sales'
Ddepartment_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using department_id without table prefix in the subquery, causing ambiguity.
Using Sales without quotes, causing syntax error.
Using wrong column names in AVG or subqueries.