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
This gives you the table that you’re looking for (I notice that the other answers don’t), in SQL Server 2005