Here's an example of one way to return all columns for both tables (only image for minimum image_id)use northwindset nocount ongocreate table tblVacation (vacation_id int primary key ,land int ,destination int)gocreate table tblImage (image_Id int primary key ,vacation_id int references tblVacation(vacation_id) ,folder int ,name int)goinsert tblVacation select 1,1,1 unionselect 2,2,2 unionselect 3,3,3insert tblImageselect 1,1,1,1 unionselect 2,1,1,1 unionselect 3,1,1,1 unionselect 4,2,2,2 unionselect 5,2,2,2 unionselect 6,3,3,3 unionselect 7,3,3,3goselect a.vacation_id ,a.land ,a.destination ,c.image_Id ,c.folder ,c.[name]from tblvacation ajoin (--derived table returns minimum image_id for each vacation_id select vacation_id ,min(image_id) image_id from tblimage group by vacation_id ) b on b.vacation_id = a.vacation_idjoin tblimage c on c.image_id = b.image_idgodrop table tblimagegodrop table tblvacation
Be One with the OptimizerTG