Special Operator Any Or Some In SQL Server

SOME and ANY operators are interchangeable – wherever we use the terms ANY, SOME would work just the same.

Let’s see examples,
  1. CREATE TABLE #TEMP (EMPID INT IDENTITY (1,1),NAME VARCHAR(20),CITY VARCHAR(20))  
  2.   
  3. CREATE TABLE #TEMPCITY( CITYNAME VARCHAR(20))  
  4.   
  5. INSERT INTO #TEMP (NAME,CITY)  
  6. SELECT 'ROHAN','ALLHABAD'  
  7. UNION  
  8. SELECT 'SUMIT','JAIPUR'  
  9. UNION   
  10. SELECT 'RAHL','DELHI'  
  11. UNION  
  12. SELECT 'RAJU''NOIDA'  
  13.  
  14. INSERT INTO #TEMPCITY(CITYNAME)  
  15. SELECT  'JAUNPUR'  
  16. UNION   
  17. SELECT'ALLHABAD'  
  18.   
  19. UNION   
  20.  SELECT'NOIDA'  
  21.   
  22. SELECT * FROM #TEMP  
  23. WHERE CITY=ANY(SELECT CITYNAME FROM #TEMPCITY)  
  24.   
  25. DROP TABLE  #TEMP  
  26. DROP TABLE #TEMPCITY  
 
The ANY operator takes all values produced by the subquery. In this case all city values in the #tempcity table, and evaluate to TRUE if ANY of them equal the city value of the current row of the outer query. Of course, this means that the subquery must select values whose datatype is comparable to what they are being compared to in the main predicates, just as IN and relational operator must. This is contract to EXISTS, which simply determine whether or not subquery produce result and does not actually use the result, so that the datatype of the subquery output column do not matter.

Let's use Of SOME Operator.
  1. CREATE TABLE #TEMP (EMPID INT IDENTITY (1,1),NAME VARCHAR(20),CITY VARCHAR(20))  
  2.   
  3. CREATE TABLE #TEMPCITY( CITYNAME VARCHAR(20))  
  4.   
  5. INSERT INTO #TEMP (NAME,CITY)  
  6. SELECT 'ROHAN','ALLHABAD'  
  7. UNION  
  8. SELECT 'SUMIT','JAIPUR'  
  9. UNION   
  10. SELECT 'RAHL','DELHI'  
  11. UNION  
  12. SELECT 'RAJU''NOIDA'  
  13.   
  14.   
  15. INSERT INTO #TEMPCITY(CITYNAME)  
  16. SELECT  'JAUNPUR'  
  17. UNION   
  18. SELECT'ALLHABAD'  
  19.   
  20. UNION   
  21.  SELECT'DELHI'  
  22.   
  23. SELECT * FROM #TEMP  
  24. WHERE CITY=SOME(SELECT CITYNAME FROM #TEMPCITY)  
  25.   
  26. DROP TABLE  #TEMP  
  27. DROP TABLE #TEMPCITY  

Inthe  first example it returns two row and the same as example 2.