SQL Practice Queries - 1
Written by Devanshu Agarwal /
SQL stands for Structured Query Language, and it is a language used to manage and manipulate data in relational databases. SQL is a widely used language in the world of data and is essential for anyone who wants to work with databases.
Today, I have crafted an intricate SQL question that can prove to be beneficial in enhancing your SQL query formulation skills, as well as equipping you with the necessary expertise to tackle SQL interview questions. This question is designed to challenge your understanding of SQL concepts and command structures, enabling you to gain a more in-depth understanding of this powerful database management language. By attempting this question, you can refine your SQL proficiency and discover new strategies to solve complex problems. This advanced level SQL question will help you to build confidence in your abilities and improve your overall proficiency in SQL.
Assume you have a table called "orders" with the following columns:
- order_id (int)
- customer_id (int)
- order_date (date)
- order_total (decimal)
- order_status (varchar)
Here is the data for based on the above table structure.
order_id | customer_id | order_date | order_total | order_status |
---|---|---|---|---|
1 | 101 | 2022-02-15 | 25.99 | Shipped |
2 | 102 | 2022-02-16 | 50.00 | Processing |
3 | 103 | 2022-02-17 | 10.00 | Completed |
4 | 104 | 2022-02-18 | 30.00 | Shipped |
5 | 102 | 2022-02-19 | 15.00 | Processing |
6 | 101 | 2022-02-20 | 20.00 | Completed |
7 | 105 | 2022-02-21 | 45.00 | Shipped |
8 | 101 | 2022-02-22 | 35.00 | Processing |
9 | 106 | 2022-02-23 | 60.00 | Completed |
10 | 104 | 2022-02-24 | 40.00 | Shipped |
Write a query to find the top 5 customers who have made the largest total amount of orders (i.e., sum of all their order_total values) in the year 2022. The query should return the following columns:
- customer_id
- total_order_amount (sum of order_total values for the customer)
- rank (the rank of the customer based on total_order_amount, where the customer with the largest total_order_amount has a rank of 1)
NOTE: Prior to reading the solution, it is suggested that you attempt to solve the problem independently.
Here's the query that would solve this problem:
SELECT
customer_id,
SUM(order_total) AS total_order_amount,
RANK() OVER (ORDER BY SUM(order_total) DESC) AS rank
FROM
orders
WHERE
YEAR(order_date) = 2022
GROUP BY
customer_id
ORDER BY
total_order_amount DESC
LIMIT 5;
In this query, we use the SUM() function to calculate the total order amount for each customer, and the RANK() function to assign a rank to each customer based on their total order amount. We also use the WHERE clause to filter the results to only include orders from the year 2022, and the GROUP BY clause to group the results by customer. Finally, we use the ORDER BY clause to sort the results by total_order_amount in descending order, and the LIMIT clause to limit the results to the top 5 customers.
Output
customer_id | total_order_amount | rank |
---|---|---|
101 | 80.99 | 1 |
104 | 70.00 | 2 |
102 | 65.00 | 3 |
106 | 60.00 | 4 |
105 | 45.00 | 5 |
I hope this example helps you practice and develop your SQL skills!
Read More
SQL Intro: The Beginner's Guide to SQL Basics
Discover SQL syntax and learn the fundamentals of SQL for beginners. Start coding SQL databases in minutes with this comprehensive guide.
Sorting SQL data in ascending and descending order
Learn how to sort your SQL data in ascending order with the ORDER BY ASC clause and improve your query results.
SQL Practice Queries - 2
Looking to enhance your database management skills and take your SQL knowledge to the next level? With our advanced SQL functions and queries training, you'll learn the syntax and techniques to improv ...
Practical Examples of Using SQL Distinct, WHERE Clause and AND, OR, and NOT operators
See practical examples of how to use the SQL Distinct, WHERE Clause and AND, OR, and NOT operators