Code:
/*
Notes:
1. @Cars is a table variable, I have used to simulate your Cars table
2. I have inserted records for KIA and BENZ so that we have multiple brands
*/
declare @Cars table
(id int identity
,Brand varchar(100)
,Model int
,photo varchar(100)
)
insert into @Cars(Brand,Model,photo)
(select 'KIA',2,'' union all
select 'KIA',2,'' union all
select 'KIA',3,'' union all
select 'KIA',4,'' union all
select 'Benz',21,'' union all
select 'Benz',21,'' union all
select 'Benz',31,'' union all
select 'Benz',41,''
)
--All Brands
select
cars1.*
from
@Cars Cars1
join ( select ID=min(ID)
,brand
,model
from @Cars
group by brand,model
) Cars2
on cars1.ID=Cars2.ID
--KIA Only
select
cars1.*
from
@Cars Cars1
join ( select ID=min(ID)
,brand
,model
from @Cars
group by brand,model
) Cars2
on cars1.ID=Cars2.ID
where
Cars1.Brand='KIA'