Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi friends
i have table called emp which has 2 columns
1. emp_name
2. role


what i want is, i want to find employees with role A and B in single SQl statement

I tried
SELECT * FROM EMP WHERE ROLE LIKE 'A' AND ROLE LIKE 'B'


but its not working, any idea how to do it
i wana avoid use of cursor.

thanks in advance
Posted
Comments
bbirajdar 31-Oct-12 2:57am    
Table with 2 columns and no primary-foreign keys?
dhage.prashant01 31-Oct-12 3:04am    
Table has many columns
But i cut down to short as per my need :)
For the purpose of understanding

Use OR or IN in where statement
SQL
SELECT * FROM EMP WHERE ROLE LIKE 'A' OR ROLE LIKE 'B'

OR
SQL
SELECT * FROM EMP WHERE ROLE In ('A','B')


For finding employees who have both role then fire this query
SQL
SELECT EmpId FROM EMP WHERE ROLE LIKE 'A' OR ROLE LIKE 'B' Group by EMPId Having Count(EMPID)>1

Happy Coding!
:)
 
Share this answer
 
v2
Comments
damodara naidu betha 31-Oct-12 7:36am    
5+
Aarti Meswania 31-Oct-12 7:36am    
thank you! :)
SELECT * FROM EMP WHERE ROLE LIKE 'A' OR ROLE LIKE 'B'

OR

SELECT * FROM EMP WHERE ROLE LIKE 'A'
union
SELECT * FROM EMP WHERE ROLE LIKE 'B'
 
Share this answer
 
Comments
dhage.prashant01 31-Oct-12 3:06am    
I want to find employee with both role A and B
SELECT * FROM EMP WHERE ROLE LIKE 'A' OR ROLE LIKE 'B'
will get records for employee whose role is either A or B, which is not my requirment
SQL
SELECT * FROM EMP GROUP BY EMP_NAME HAVING COUNT(ROLE) >1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900