Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SubQuery

Author  Topic 

asif372
Posting Yak Master

100 Posts

Posted - 2013-04-25 : 02:57:26

Can any one explain me sub query in easy words ? i am new in sql

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-25 : 03:09:17
If you want to get results based on the output of another query, then sub queries will be used.
You can use sub queries in the SELECT, FROM, WHERE.... clauses
Ex:
SELECT EMPLOYEE_ID, SALARY, FIRST_NAME || ' ' || LAST_NAME "Highest Paid Employee"
FROM EMPLOYEES
WHERE SALARY = (SELECT MAX (SALARY) FROM EMPLOYEES) -- this is the sub query for getting highest salary of employees

refer these links for further details:
https://www.simple-talk.com/sql/sql-training/subqueries-in-sql-server/
http://msdn.microsoft.com/en-IN/library/ms189575(v=sql.105).aspx
http://blog.sqlauthority.com/2010/06/06/sql-server-subquery-or-join-various-options-sql-server-engine-knows-the-best/

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-25 : 04:16:01
another type of subqueries are correlated subqueries where you pass column(s) from main query to it and retrieve related data from one or more tables based on passed column values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -