...

/

Practice Sequence and Hierarchy

Practice Sequence and Hierarchy

Practice writing queries to deepen your understanding of sequencing and hierarchical patterns.

Sequence and hierarchy patterns questions

Let’s practice writing queries to strengthen your grasp of sequence and hierarchy patterns.

Top product per category with ties allowed

Given the following structure of the Products table:

Field

Type

ProductID

int

ProductName

varchar(50)

CategoryID

int

Price

decimal(10,2)

Stock

int

LastRestockDate

date

MonthlySales

int

InventoryTurnoverRate

decimal(5,2)

Where ProductID is the primary key and CategoryID is the foreign key from the Categories table. The table contains information about products.

Write an SQL query to return the top-selling product(s) in each category. Return ProductName, CategoryID, MonthlySales, and sales_rank.

Expected output:

ProductName CategoryID MonthlySales sales_rank
Wireless Earbuds 1 20
...