Write a query to get the customer with the highest total order value for each year and month. Order and Customer tables are separate, with Order_ID and Customer_ID as primary keys. The Customer table's Oid is a foreign key referencing the Orders table's Order_ID. In case of a tie, return the customer with the lower Customer_ID.

AnswerBot
1y

Query to get the customer with the highest total order value for each year, month.

  • Join the Order and Customer tables on the foreign key

  • Group the results by year, month, and customer

  • Calculate the total...read more

Gaurav Rungta
2y

SELECT

subquery2.year,

subquery2.month,

subquery2.customerid,

subquery2.total_monthly_order_value

FROM

(

SELECT

YEAR(orderdate) AS year,

MONTH(orderdate) AS month,

customerid,

SUM(unitprice * quantity) AS total_monthly_order_value,

ROW_NUMBER() OVER (PARTITION BY YEAR(orderdate), MONTH(orderdate) ORDER BY SUM(unitprice * quantity) DESC, customerid ASC) AS rn

FROM

orders

JOIN

order_details ON orders.orderid = order_details.orderid

GROUP BY

YEAR(orderdate),

MONTH(orderdate),

customerid

) AS subquery2

WHERE

subquery2.rn = 1

ORDER BY

subquery2.year,

subquery2.month;

Help your peers!
Select
Add answer anonymously...

7 Eleven Senior Data Engineer interview questions & answers

A Senior Data Engineer was asked Q. How do you handle data pipelines when the schema information keeps changing at t...read more
A Senior Data Engineer was asked Q. What is the difference between a Broadcast variable and an accumulator variable?
A Senior Data Engineer was asked Q. Write a query to get the customer with the highest total order value for each ye...read more

Popular interview questions of Senior Data Engineer

A Senior Data Engineer was asked Q1. How do you handle data pipelines when the schema information keeps changing at t...read more
A Senior Data Engineer was asked Q2. What is the difference between a Broadcast variable and an accumulator variable?
A Senior Data Engineer was asked Q3. What are the different types of schema you know in Data Warehousing?
7 Eleven Senior Data Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits