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

How to Display, Amount of Disk Activity Generated by Transact-SQL Statements?

1 Answers  


How To Make password Protected SQL Server 2005 Database i.e when i open SQL Server database then ask for password.

1 Answers  


How many ways to create table-valued functions?

0 Answers  


Can two tables share the same primary key?

0 Answers  


Why I am getting "the microsoft .net framework 2.0 in not installed" message?

0 Answers  






Can two tables share a primary key?

0 Answers  


Hi Can any one tell me the Good institute to learn SQL esp for Data Base Testing or SQL from scratch to the proficiency level in Hyederabad and facult also. Thankyou well in advance

6 Answers  


What different steps will a sql server developer take to secure sql server?

0 Answers  


How can sql injection be stopped? : sql server security

0 Answers  


How to maintain a fill factor in existing indexes?

0 Answers  


How to determine the service pack currently installed on SQL Server?

4 Answers   HCL,


What's the maximum size of a row?

4 Answers   IBM,


Categories