Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Query Optimization | Query optimization.Indexes
course content

Course Content

Advanced Techniques in SQL

Challenge: Query OptimizationChallenge: Query Optimization

Let's discover two tables: bank_accounts and one additional table named transactions.

BankAccounts table

account_number account_holder balance
789 Alice Johnson 3500.00
101 Bob Brown 4500.00
202 Emily Jones 6000.00
303 Michael Davis 800.00
404 Sarah Wilson 12000.00
505 David Taylor 300.00
606 Emma Thomas 7500.00
707 Daniel Martinez 1000.00
808 Olivia Rodriguez 900.00
909 Sophia Lee 2500.00
123 John Doe 5000.00
456 Jane Smith 2000.00

Transactions table

account_from account_to amount timestamp
789 101 1000.00 2024-05-27 12:00:00
101 202 2000.00 2024-05-27 12:05:00
202 303 500.00 2024-05-27 12:10:00
303 404 800.00 2024-05-27 12:15:00
404 505 1200.00 2024-05-27 12:20:00

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:

  1. Explicitly Specify Columns: Instead of using the asterisk (*) wildcard, explicitly mention column names in queries for better performance, readability, and maintainability;
  2. Minimize Subqueries: Reduce the use of subqueries to optimize query performance. Consider alternatives like joins or derived tables to avoid complexity and overhead;
  3. Avoid Repeated IN Operators: Limit the use of the IN operator in queries to prevent performance impact. Instead, consider using JOIN or EXISTS clauses for more efficient execution plans;
  4. Organize Joins Logically: Start SQL joins with the main table and then join with related tables to optimize query organization and database engine optimization;
  5. Use Restrictive WHERE Conditions: Improve query performance by including restrictive conditions in the WHERE clause to filter rows and enhance execution speed;
  6. 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.

Task

Please rewrite the query more optimally. Pay attention to the fact that we need to get only account numbers that have performed at least one transaction (transferred money to another account) sorted in ascending order.

Note

Use LEFT or INNER join type to complete this task.

Everything was clear?

Section 2. Chapter 7
toggle bottom row
course content

Course Content

Advanced Techniques in SQL

Challenge: Query OptimizationChallenge: Query Optimization

Let's discover two tables: bank_accounts and one additional table named transactions.

BankAccounts table

account_number account_holder balance
789 Alice Johnson 3500.00
101 Bob Brown 4500.00
202 Emily Jones 6000.00
303 Michael Davis 800.00
404 Sarah Wilson 12000.00
505 David Taylor 300.00
606 Emma Thomas 7500.00
707 Daniel Martinez 1000.00
808 Olivia Rodriguez 900.00
909 Sophia Lee 2500.00
123 John Doe 5000.00
456 Jane Smith 2000.00

Transactions table

account_from account_to amount timestamp
789 101 1000.00 2024-05-27 12:00:00
101 202 2000.00 2024-05-27 12:05:00
202 303 500.00 2024-05-27 12:10:00
303 404 800.00 2024-05-27 12:15:00
404 505 1200.00 2024-05-27 12:20:00

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:

  1. Explicitly Specify Columns: Instead of using the asterisk (*) wildcard, explicitly mention column names in queries for better performance, readability, and maintainability;
  2. Minimize Subqueries: Reduce the use of subqueries to optimize query performance. Consider alternatives like joins or derived tables to avoid complexity and overhead;
  3. Avoid Repeated IN Operators: Limit the use of the IN operator in queries to prevent performance impact. Instead, consider using JOIN or EXISTS clauses for more efficient execution plans;
  4. Organize Joins Logically: Start SQL joins with the main table and then join with related tables to optimize query organization and database engine optimization;
  5. Use Restrictive WHERE Conditions: Improve query performance by including restrictive conditions in the WHERE clause to filter rows and enhance execution speed;
  6. 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.

Task

Please rewrite the query more optimally. Pay attention to the fact that we need to get only account numbers that have performed at least one transaction (transferred money to another account) sorted in ascending order.

Note

Use LEFT or INNER join type to complete this task.

Everything was clear?

Section 2. Chapter 7
toggle bottom row
some-alt