SQL Practice Queries - 1

Devanshu Agarwal

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_idcustomer_idorder_dateorder_totalorder_status
11012022-02-1525.99Shipped
21022022-02-1650.00Processing
31032022-02-1710.00Completed
41042022-02-1830.00Shipped
51022022-02-1915.00Processing
61012022-02-2020.00Completed
71052022-02-2145.00Shipped
81012022-02-2235.00Processing
91062022-02-2360.00Completed
101042022-02-2440.00Shipped

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:

SQL
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_idtotal_order_amountrank
10180.991
10470.002
10265.003
10660.004
10545.005

I hope this example helps you practice and develop your SQL skills!