sqlbeginner10 minutes
Refactor SQL Query to Remove Redundant Subquery
Optimize a basic SQL query by refactoring it to remove an unnecessary subquery, improving readability and performance without changing the output.
Challenge prompt
You are given a SQL query that selects employees from the 'employees' table with a salary greater than 50000. However, the query uses a redundant subquery structure. Refactor the query to achieve the same results but without the unnecessary subquery, making it cleaner and more efficient.
Guidance
- • Analyze the query to identify where the subquery is not needed.
- • Rewrite the query using a straightforward WHERE clause.
- • Ensure the output remains the same before and after refactoring.
Hints
- • Is the subquery filtering data in a way that can be done directly in the WHERE clause?
- • Consider if flattening the query improves readability without changing results.
Starter code
SELECT * FROM (SELECT * FROM employees) AS emp WHERE salary > 50000;Expected output
All rows from 'employees' where salary is greater than 50000, the same as the original query but retrieved using a simpler query structure.
Core concepts
simple SELECTWHERE clausesubqueriesquery optimization
Challenge a Friend
Send this duel to someone else and see if they can solve it.