No, it will not work 100%.What happens if you have these four students?123456 Bill Smith 852147 Andy Noname900005 Andy Smith 152144 Andy Smith 900003 Bill Noname115478 Bill NonameYou query above will fetch Bill Smith and Andy Noname as well,because there is at least one other Bill with ID beginning with 9 AND one other Smith with ID beginning with 9.You have to test for both firstname and lastname simultaneously, like my suggestion.declare @students table (id int, fname varchar(10), lname varchar(10))insert @studentsselect 123456, 'Bill', 'Smith' union allselect 852147, 'Andy', 'Noname' union allselect 900005, 'Andy', 'Smith' union allselect 152144, 'Andy', 'Smith' union allselect 900003, 'Bill', 'Noname' union allselect 115478, 'Bill', 'Noname'-- davidmalSelect *from @studentsWhere fname in (Select fname from @students where id like '9%') and lname in (Select lname from @students where id like '9%')-- pesoselect s1.id, s1.fname, s1.lnamefrom @students as s1where exists (select null from @students as s2 where s2.fname = s1.fname and s2.lname = s1.lname and id like '9%')
Peter LarssonHelsingborg, Sweden