Query Optimization Techniques - Advanced Guided Assignment - MySQL #30
akash-coded
started this conversation in
Tasks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Absolutely, here are more complex exercises which involve a variety of SQL constructs.
Exercise 1: CTE and Views
Given the following query that calculates the total sales per product for each year, create a CTE and a corresponding view for this query.
Exercise 2: Stored Procedures and Triggers
Create a stored procedure to add a new product to the
productstable. After adding the new product, the stored procedure should calculate and update the average product price in theproductlinestable for the product line of the new product.Now, write a trigger that calls this stored procedure whenever a new product is inserted into the
productstable.Exercise 3: Conditional Statements and Cursors
Write a stored procedure that uses a cursor to iterate over all orders that have a status of 'In Process'. For each such order, if the total quantity of products ordered exceeds the quantity in stock for any product, set the status of the order to 'On Hold'. Use conditional statements to implement this logic.
Exercise 4: Optimizing Complex Joins
Given the following query that joins four tables, optimize the query by choosing the correct order of joins and other optimizations.
Sure, let's continue with more assignments using advanced SQL constructs and optimization techniques.
Exercise 5: Handling NULLs
Given the following query, optimize it and handle the NULL values in a way that doesn't disrupt the calculations or results. Use the COALESCE function or any other relevant method for this.
Exercise 6: Subqueries and HAVING Clause
Write a query that shows the customers who have made total purchases of more than the average purchase amount. Use a subquery to calculate the average purchase amount, and a HAVING clause to filter the customers.
Exercise 7: UNION and Window Functions
Create a query that shows the total sales for each product line for the year 2003 and the year 2004. Use the UNION operator to combine the results for the two years into a single result set. Then, over this result set, use a window function to calculate the year-on-year growth rate in sales for each product line.
Exercise 8: Case Statements and Complex Aggregates
Write a query that categorizes customers into 'High Value', 'Medium Value', and 'Low Value' based on their total purchases. Use a CASE statement to create the categories, and complex aggregates to define the thresholds for each category.
Exercise 9: Optimizing Joins with Derived Tables
In the following query that joins five tables, optimize the query by using derived tables.
Consider the sizes of the tables, the number of rows that will be returned after each join, and the use of indexes when optimizing this query.
Make sure you explain the optimization steps you took and why they improve the performance of the query. Also, consider the trade-offs of using these advanced SQL constructs and optimization techniques. Remember to explain your reasoning and the impact of your optimizations for each exercise.
Beta Was this translation helpful? Give feedback.
All reactions