What is the difference between in and exists.
Ex: select * from emp where empno in(....) and
select * from emp where empno exists(....)

What is the difference between a Join and Union and Union
and UnionAll.

Answers were Sorted based on User's Feedback



What is the difference between in and exists. Ex: select * from emp where empno in(....) and sele..

Answer / gopi muluka

IN:Returns true if the column matches to any value in a
subquery or a list.
Exists:Returns true if a subquery contains any row.

Generally EXISTS is faster than IN, because it stops
processing once it finds a row in resultset

Join : Returns only rows which are matching on Joining
Column

Union: Combines two or more resultsets with same number of
columns and displays it as a single resultset by
eliminating duplicate records

Union All: Combines two or more resultsets with same number
of columns,displays it as a single resultset including
duplicate records if any exists

Is This Answer Correct ?    16 Yes 0 No

What is the difference between in and exists. Ex: select * from emp where empno in(....) and sele..

Answer / shafiq...

When you use IN operator it will scan all the row in table
to check and Exists means when its find the data stop
execute to check further row.

Join- to join matching data from two or more table.
Union- Retrive matching data from both table.
UnionAll- Retrive matching as well as unmatched data from
both data.

hope this is clear

Is This Answer Correct ?    19 Yes 6 No

What is the difference between in and exists. Ex: select * from emp where empno in(....) and sele..

Answer / saradhi

Just like JOINS, UNION combines data into a single record-
set but vertically by adding rows from another table. JOINS
combine data horizontally by adding columns from another
table.

UNION insures you get DISTINCT records from both the tables.
UNION ALL pulls out all records from both the tables with
duplicates.

Is This Answer Correct ?    2 Yes 0 No

What is the difference between in and exists. Ex: select * from emp where empno in(....) and sele..

Answer / nandkumar karlekar

Answer:-

1. Union is heavy as compare to Union All

2. result of Union Query is Set where as result of
Union ALL is not a Set it’s only concatenation of two
results

3. Suppose table1 and table2 have T1 ,T2 records
respectively then the number of comparison require to
produce result is as follows



Union All requires O (1) time complexity

Union Requires O ((T1*(T1+1)/2) + (T2*(T2+1)/2) + (T1 *T2))





Example for UNION: - Table1 has 10 records

Table2 has 20 records

Suppose: table1 and table2 has all record distinct.



Total comparisons require = 465 comparisons



Example for UNION ALL: -

Table1 has 10 records

Table2 has 20 records



Suppose: table1 and table2 has all record distinct or not,
no problem.



Total comparisons require = Constant or may be 0

Is This Answer Correct ?    1 Yes 0 No

What is the difference between in and exists. Ex: select * from emp where empno in(....) and sele..

Answer / manoj pandey

Check below links to know the reason with examples from my blog posts:

IN vs EXISTS vs JOIN: http://sqlwithmanoj.wordpress.com/2011/02/15/not-in-not-exists-joins-with-null-values/

UNION vs UNION ALL: http://sqlwithmanoj.wordpress.com/2010/12/30/why-union-all-is-faster-than-union/

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More SQL Server Interview Questions

What is difference in performance between insert top (n) into table and using top with insert?

0 Answers  


What is a result set object returned by odbc_exec()?

0 Answers  


How many types of subqueries are there in sql server?

0 Answers  


What happens if the update subquery returns no rows in ms sql server?

0 Answers  


Explain what is the purpose of sql profiler in sql server?

0 Answers  






Please explain go command in sql server?

0 Answers  


What do you mean by 'normalization'?

0 Answers   Ernst Young, Thomson Reuters,


Explain the database you used in your final year project?

0 Answers   Wipro,


Can we rollback records deleted by a truncate statement?

3 Answers   CarrizalSoft Technologies, United Healthcare,


What is meant by Active-Passive and Active-Active clustering setup?

0 Answers  


What is the name of the system variable that returns the number of rows affected by a SQL statement?

0 Answers   HCL,


Ms sql server index?

0 Answers  


Categories