Challenge: Query Optimization
Assume we want to retrieve only the account numbers that have performed at least one transaction.
In this task, we have a query that is not optimized. Your task is to rewrite it in a more optimal way.
Here is a list of the main query rewriting techniques to enhance optimization:
-
Explicitly Specify Columns: instead of using the asterisk (
*) wildcard, explicitly mention column names in queries for better performance, readability, and maintainability; -
Minimize Subqueries: reduce the use of subqueries to optimize query performance. Consider alternatives like joins or derived tables to avoid complexity and overhead;
-
Avoid Repeated IN Operators: limit the use of the
INoperator in queries to prevent performance impact. Instead, consider usingJOINorEXISTSclauses for more efficient execution plans; -
Organize Joins Logically: start SQL joins with the main table and then join with related tables to optimize query organization and database engine optimization;
-
Use Restrictive WHERE Conditions: improve query performance by including restrictive conditions in the
WHEREclause to filter rows and enhance execution speed; -
Refactor Code into Stored Procedures or Functions: encapsulate repetitive code segments into stored procedures or user-defined functions for code reusability, modularity, and easier maintenance. These can reduce redundancy and optimize SQL queries.
Swipe to start coding
Your task is to write a query that returns a list of all account numbers that have performed at least one transaction (transferred money to another account). The results should be sorted in ascending order by account number.
To make your query more efficient and readable, follow these steps:
- Select only the
account_numbercolumn; - Use an
INNER JOINbetween theBankAccountsandTransactionstables; - Do not use the
INoperator or subqueries; - Sort the result by
account_numberin ascending order.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.55
Challenge: Query Optimization
Swipe to show menu
Assume we want to retrieve only the account numbers that have performed at least one transaction.
In this task, we have a query that is not optimized. Your task is to rewrite it in a more optimal way.
Here is a list of the main query rewriting techniques to enhance optimization:
-
Explicitly Specify Columns: instead of using the asterisk (
*) wildcard, explicitly mention column names in queries for better performance, readability, and maintainability; -
Minimize Subqueries: reduce the use of subqueries to optimize query performance. Consider alternatives like joins or derived tables to avoid complexity and overhead;
-
Avoid Repeated IN Operators: limit the use of the
INoperator in queries to prevent performance impact. Instead, consider usingJOINorEXISTSclauses for more efficient execution plans; -
Organize Joins Logically: start SQL joins with the main table and then join with related tables to optimize query organization and database engine optimization;
-
Use Restrictive WHERE Conditions: improve query performance by including restrictive conditions in the
WHEREclause to filter rows and enhance execution speed; -
Refactor Code into Stored Procedures or Functions: encapsulate repetitive code segments into stored procedures or user-defined functions for code reusability, modularity, and easier maintenance. These can reduce redundancy and optimize SQL queries.
Swipe to start coding
Your task is to write a query that returns a list of all account numbers that have performed at least one transaction (transferred money to another account). The results should be sorted in ascending order by account number.
To make your query more efficient and readable, follow these steps:
- Select only the
account_numbercolumn; - Use an
INNER JOINbetween theBankAccountsandTransactionstables; - Do not use the
INoperator or subqueries; - Sort the result by
account_numberin ascending order.
Solution
Thanks for your feedback!
single