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 row by row processing ?

2 Answers  


What is difference between index and primary key?

0 Answers  


What is the difference Between Sql-server 2000 & 2005

3 Answers   Value Labs, Wipro,


What does it mean to be in union?

0 Answers  


how you can move data or databases between servers and databases in sql server? : Sql server administration

0 Answers  






what's new in sql server 2016?

0 Answers  


Write a SQL Query to find first Week Day of month?

2 Answers  


What causes index fragmentation?

0 Answers  


How to use the inserted and deleted pseudo tables?

0 Answers  


Is sql server free?

0 Answers  


What according to you is the difference between mysql and sql server performance?

0 Answers  


What is optimization and its types?

0 Answers  


Categories