Geeks Talk

Prepare for your Next Interview




SubQuery

This is a discussion on SubQuery within the SQL Server forums, part of the Databases category; How is subquery used in sqlserver?I mean the command used for subquery?...


Go Back   Geeks Talk > Databases > SQL Server

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-16-2007
Junior Member
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 2 Times in 2 Posts
chitrab is on a distinguished road
SubQuery

How is subquery used in sqlserver?I mean the command used for subquery?
Reply With Quote
The Following User Says Thank You to chitrab For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 05-24-2007
Expert Member
 
Join Date: Sep 2006
Location: India
Posts: 131
Thanks: 1
Thanked 20 Times in 20 Posts
Innila is on a distinguished road
Re: SubQuery

Hi Chitra,

A subquery is a select statement that is embedded in a clause of another select statement.
A subquery is a SELECT query that returns a single value and is nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery.

Here is a simple subquery example where the employee details of maximum salary holder is retrieved,
Code:
SELECT ename, empno, sal from Northwind.emp_new a
where sal = (SELECT MAX(sal) FROM Northwind.emp_new b)
__________________
*** Innila ***
Reply With Quote
  #3 (permalink)  
Old 05-24-2007
Expert Member
 
Join Date: Jun 2006
Location: India
Posts: 411
Thanks: 15
Thanked 33 Times in 25 Posts
jamesravid is on a distinguished road
Re: SubQuery

A correction,

It is not true that A subquery is a SELECT query that returns a single value. Subquery can also return set of values... Here is an example,

SELECT ename, empno, sal from Northwind.emp_new a
where sal in (SELECT MAX(sal) FROM Northwind.emp_new b group by dept)
__________________
Cheers,
:) James:)
Reply With Quote
  #4 (permalink)  
Old 05-24-2007
Expert Member
 
Join Date: Sep 2006
Location: India
Posts: 131
Thanks: 1
Thanked 20 Times in 20 Posts
Innila is on a distinguished road
Re: SubQuery

Yeah true.
Nice that u have mentioned the correction, James.
__________________
*** Innila ***
Reply With Quote
Reply

  Geeks Talk > Databases > SQL Server


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Join Query without using NOT IN subquery Geek_Guest SQL 1 03-07-2007 04:24 PM


All times are GMT -4. The time now is 01:31 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved