0
0
PostgreSQLquery~10 mins

Scalar subqueries in PostgreSQL - 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 the average salary from the employees table.

PostgreSQL
SELECT (SELECT [1](salary) FROM employees) AS avg_salary;
Drag options to blanks, or click blank then click option'
AAVG
BCOUNT
CSUM
DMAX
Attempts:
3 left
💡 Hint
Common Mistakes
Using SUM instead of AVG returns total salary, not average.
Using COUNT returns number of rows, not average salary.
2fill in blank
medium

Complete the code to find employees whose salary is greater than the average salary.

PostgreSQL
SELECT name FROM employees WHERE salary > (SELECT [1](salary) FROM employees);
Drag options to blanks, or click blank then click option'
ACOUNT
BSUM
CMIN
DAVG
Attempts:
3 left
💡 Hint
Common Mistakes
Using SUM returns total salary, causing incorrect comparison.
Using MIN returns lowest salary, not average.
3fill in blank
hard

Fix the error in the scalar subquery to correctly get the maximum salary.

PostgreSQL
SELECT name, salary FROM employees WHERE salary = (SELECT [1](salary) FROM employees);
Drag options to blanks, or click blank then click option'
AMAX()
BMAX(
CMAX
DMAX)
Attempts:
3 left
💡 Hint
Common Mistakes
Writing MAX without parentheses causes syntax error.
Adding parentheses incorrectly breaks the query.
4fill in blank
hard

Fill both blanks to select employees whose salary is between the minimum and maximum salaries.

PostgreSQL
SELECT name FROM employees WHERE salary BETWEEN (SELECT [1](salary) FROM employees) AND (SELECT [2](salary) FROM employees);
Drag options to blanks, or click blank then click option'
AMIN
BAVG
CMAX
DSUM
Attempts:
3 left
💡 Hint
Common Mistakes
Using AVG instead of MIN or MAX changes the range incorrectly.
Using SUM does not make sense for range boundaries.
5fill in blank
hard

Fill all three blanks to select employees whose salary is greater than the average salary and less than the maximum salary.

PostgreSQL
SELECT name FROM employees WHERE salary > (SELECT [1](salary) FROM employees) AND salary < (SELECT [2](salary) FROM employees) ORDER BY [3];
Drag options to blanks, or click blank then click option'
AMIN
BAVG
CMAX
Dsalary
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up MIN and AVG in comparisons.
Forgetting to order by a valid column.