5

how can i write a select query by combining IN and LIKE i have a subquery which returns some records , with threse records i have to select records from another table. the problem is i have to use LIKE clause on the resultant recors of the subquery below is an example of what im trying to do

select * from salary where employeename like (select name from employee)

from salary table i need records which matches the name of employe table. i need to use LIKE . can someone help me please

Girish K G
  • 407
  • 5
  • 11
  • 19
  • 1
    http://stackoverflow.com/questions/3014940/is-there-a-combination-of-like-and-in-in-sql – Romeo Feb 09 '12 at 04:37

2 Answers2

23

I'd go with a join instead of an in... although with the wildcards it will be a full table-scan anyways:

select distinct s.* 
from salary s
join employee e on s.employeename like '%' + e.name + '%'
Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109
1

check this out.

select * from salary where emp_name like '%aj'
Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109
  • This does not parse correctly (you need string delimiters and shouldn't have the trailing .), the table is wrong (and you're missing the other table), and I'm unclear why you would have a leading wildcard but not a trailing one. – Aaron Bertrand Feb 09 '12 at 04:43
  • 2
    @AaronBertrand Partly my fault - the trailing `.` was my mistake when I wrapped the query in code tags. I won't take credit for the rest though... – Michael Fredrickson Feb 09 '12 at 04:48
  • 3
    However, this will work great if you need Nicki Minaj's salary. – Michael Fredrickson Feb 09 '12 at 04:55