Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 881695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:18:15+00:00 2026-05-15T12:18:15+00:00

In this sample database there are two tables, products and prices. The goal is

  • 0

In this sample database there are two tables, products and prices.
The goal is to find the highest and the lowest price for each product.

The price table can have either zero, one or two rows per product.

create table products(
    id int,
    name nvarchar(50)
)

create table prices(
    productId int,
    price int
)

insert into products (id, name) values (33,'bike')
insert into products (id, name) values (44,'car')
insert into products (id, name) values (55,'bus')

insert into prices (productId, price) values (33, 10)
insert into prices (productId, price) values (33, 40)
insert into prices (productId, price) values (44, 300)

The sql query should result in this:

productId  highPrice  lowPrice
33         40         10
44         300        NULL
55         NULL       NULL
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:18:15+00:00Added an answer on May 15, 2026 at 12:18 pm

    This gives you the table that you’re looking for (I notice that the other answers don’t), in SQL Server 2005

    select P.ID as ProductID, 
    nullif(sum(case when idx=1 then price else 0 end), 0) as highPrice,
    nullif(sum(case when idx=2 then price else 0 end), 0) as lowPrice  from
    (
        select productid, price, row_number() over(partition by productID order by price desc) as idx from prices
    ) T
    right join products P on T.productID = P.ID
    group by P.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a database with tables Products and Employees. There is a new requirement to
This is a sample scenario. I have an existing database consisting of tables as
I have a simple database with two tables: Photo and Tag . There is
I have two tables Product having following structure: ProductID,ProductName, IsSaleTypeA, IsSaleTypeB, IsSaleTypeC 1, AAA,
I have two tables. One contains a list of products with the primary key
My database (sqlite3), has two tables with the following schemas: CREATE TABLE log(d date,
For the sake of simplicity, let's say my database has two tables, videos and
There are 3 tables in my database: Projects, Users and UsersInProjects. In UsersInProjects table
I'm using EF 4.1 and a database I didn't design. I have two tables
In my Android application; I have a single database with multiple tables. Each table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.