Subqueries vs Joins in SQL

What is the difference between Subqueries and Joins? Which is better and Why? Give an example of each.

Questions by t1tankster

Showing Answers 1 - 12 of 12 Answers

Join is a Methodology to get a data from more than one table, its work under the condition.

Example:

If we want to display the Employee no and his Department no

SELECT Empno,Deptno
FROM Emp,Dept
where Emp.Deptno=Dept.Deptno

subquery it has two select statement, the inner select statement will execute first and then it will be the value for outer select statement.

Example:

If we want to display the employees who are getting salary more than an employee TIM

SELECT Ename,Sal
FROM Emp
WHERE Sal>(SELECT Sal
FROM Emp
WHERE Ename='TIM');

  Was this answer useful?  Yes

abdullah

  • Sep 12th, 2011
 

I am software engineer and iam working on oracle mysql and sql so i have lot of experience on this ,basically joins are using there should be connection two or more than two tables ,joins are always occurs where each tables has relation with other tables and these are linear relation should be necessary while sub query and child query means query inside query no need to relation it works on columns and conditions like ,if you want to retrieve the name of employee who has salary more than two thousand then sub query will use very beneficial and sub quires ,A subquery is a query that is nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery. A subquery can be used anywhere an expression is allowed. In this example a subquery is used as a column expression named MaxUnitPrice in a SELECT statement.

  Was this answer useful?  Yes

Hiren

  • Nov 29th, 2011
 

Its true,
I would like to add some more information about it,

Subquery take separate temp table and checking condition …………..
Besides join is checks conditions first and then put it into table and displays as we mentioned.
If our tables has got big amount of data than, during the execution, subquery takes more load and hence more time as well.
So, its more convenient to use join instead of subquery.

Good luck.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions