fbpx

Fetching Rows by Groups

Given the following Products table:

 

Fetching_Records_by_Sections_1

As you can see:

  • each row represents one product.
  • The table has three columns: ProductID, ProductName and ProuctPrice, which contain information about each product.
  • The ProductPrice values range between 10 and 300.

Based on the price range, we can say that there are three product groups : those with a price range of 1-100, those with a price range of 101-200, and those with a price range of 201-300.

Write a SELECT statement that retrieves the lowest two prices for each group.

 

fetching_groups_2

 

If you want to take this challenge one step forward:

The script below used a series of simple INSERT statements to populate Products table.

Use a TSQL loop to achieve the same goal. Then, try using recursive SELECT statement.

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…