Subquery in WHERE in SQL
Level 4109 keystrokes
A subquery in WHERE compares each row against a calculated value.
SQL code
-- Return the name and salary of employees1SELECT2 name,3 salary4FROM5 employees-- Keep only employees who earn more than the average6WHERE7 salary > ( -- The subquery returns one value: the average salary of all employees -- It does not depend on the outer row, so it only needs to be computed once8 SELECT AVG(salary)9 FROM employees10 )-- Highest salary first11ORDER BY12 salary DESC;Output
+-------+--------+ | name | salary | +-------+--------+ | Carol | 85000 | | Dave | 72000 | | Alice | 68000 | +-------+--------+ 3 rows in set
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this SQL code and how often they appear.
(× 2)× 2,× 1;× 1>× 1