Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Top Sales per Region | Introduction to Window Functions
Window Functions in SQL

bookChallenge: Top Sales per Region

To solve this task, you need to identify the highest sale amount and the corresponding employee for each region in the sales table. You will use the ROW_NUMBER() window function to achieve this efficiently.

Step-by-step approach

  1. Assign a row number within each region:

    • Use ROW_NUMBER() as a window function to assign a unique number to each sale within its region.
    • Partition the data by the region column so that numbering restarts for each region.
    • Order the rows within each region by the amount column in descending order; this places the highest sale at the top for each region.
  2. Select only the top sale per region:

    • After assigning row numbers, the row with ROW_NUMBER() = 1 in each region will represent the highest sale for that region.
    • Use a subquery or common table expression (CTE) to filter and keep only those rows where the row number is 1.
  3. Return the required columns:

    • Select the region, employee_id, and amount columns for the top sale in each region.
Oppgave

Swipe to start coding

Write a query to find the highest sale amount and corresponding employee for each region using window functions.

  • Assign a row number to each sale within its region, ordering by the sale amount in descending order.
  • Select the region, employee ID, and sale amount for the row with the highest sale in each region.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookChallenge: Top Sales per Region

Sveip for å vise menyen

To solve this task, you need to identify the highest sale amount and the corresponding employee for each region in the sales table. You will use the ROW_NUMBER() window function to achieve this efficiently.

Step-by-step approach

  1. Assign a row number within each region:

    • Use ROW_NUMBER() as a window function to assign a unique number to each sale within its region.
    • Partition the data by the region column so that numbering restarts for each region.
    • Order the rows within each region by the amount column in descending order; this places the highest sale at the top for each region.
  2. Select only the top sale per region:

    • After assigning row numbers, the row with ROW_NUMBER() = 1 in each region will represent the highest sale for that region.
    • Use a subquery or common table expression (CTE) to filter and keep only those rows where the row number is 1.
  3. Return the required columns:

    • Select the region, employee_id, and amount columns for the top sale in each region.
Oppgave

Swipe to start coding

Write a query to find the highest sale amount and corresponding employee for each region using window functions.

  • Assign a row number to each sale within its region, ordering by the sale amount in descending order.
  • Select the region, employee ID, and sale amount for the row with the highest sale in each region.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
single

single

some-alt