473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Subqueries in SQL

NeoPa
32,556 Expert Mod 16PB
To use a subquery (SQ) in SQL there are two ways :
  1. Treating the SQ as a record source (like a table).
  2. Treating the SQ as a single value.
In either case, the SQ is a simple SELECT query but surrounded by parentheses ().
In case 1 the SQ should either be in the FROM clause or, sometimes possible put within the In() command.
In case 2 the SQ can be used in place of any other item that returns a value (SELECT; WHERE; HAVING; GROUP BY; etc).

Assume the following structure :
Expand|Select|Wrap|Line Numbers
  1. Table Name=tblOKData
  2. ID; Autonumber; PK
  3. Name; String
with records :
Expand|Select|Wrap|Line Numbers
  1. 1   Bat
  2. 2   Ball
  3. 3   Racquet
Expand|Select|Wrap|Line Numbers
  1. Table Name=tblALLData
  2. ID; Autonumber; PK
  3. Name; String
with records :
Expand|Select|Wrap|Line Numbers
  1. 1   Glass
  2. 2   Cup
  3. 3   Plate
  4. 21  Bat
  5. 22  Ball
  6. 23  Racquet
Using a SQ within In().
Say that we wanted to show the tblAllData.ID for all items whose names match those found in the tblOKData table. We could use an INNER JOIN in this case, but alternatively (necessary to illustrate the point here) we could do it with a SQ.
The code would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT ID
  2. FROM tblAllData
  3. WHERE Name In(SELECT [Name]
  4.               FROM tblOKData)
Using a SQ within the FROM clause as a Recordset.
The simplest form of this is to surround a basic SELECT query in parentheses and rename (AS {Name}).
In this case we want the same effect as the SQL above.
The code would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT subQ.ID
  2. FROM (SELECT *
  3.       FROM tblAllData) AS subQ INNER JOIN tblOKData
  4.   ON subQ.Name = tblOKData.Name
Using a SQ as a Simple Value.
We want to select all tblAllData.IDs which are greater than the average value of these IDs.
The code would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2. FROM tblAllData
  3. WHERE [ID]>(SELECT Avg([ID])
  4.             FROM tblAllData)
Attached Files
File Type: zip SubQueries.Zip (8.6 KB, 751 views)
Jan 26 '07 #1
0 14662

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: pete | last post by:
Been banging my head against the wall with subqueries. Even simple stuff like this fails: SELECT CompanyName FROM tblcompanies WHERE CompanyName IN (SELECT HostName FROM tblhosts) Am I...
6
by: Daniel Elliott | last post by:
Hello, I was wondering if anyone would be able to help me with a problem I'm having. I'm trying to use the following query: SELECT Distinct c.site_id FROM campsite c WHERE c.site_id NOT IN...
5
by: Nick | last post by:
Im moving a development app (MySQL 5.0) to a different server which runs MySQL 4.0.20-standard. I am getting errors on queries that have subqueries such as... SELECT id FROM table1 WHERE id IN...
2
by: Kevin | last post by:
While converting SQL statements for a database change, I discovered a big performance hit in MYSQL with subqueries vices Sybase. I'm hoping that someone might be able to help me understand why? ...
2
by: CSN | last post by:
Is there much difference between using subqueries and separating out them into separate queries? __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free web site building...
2
by: orin | last post by:
Hi all, I've seen mention that you can use nested subqueries down to as many levels as you like but whenever I run the following: select * from table1 where tab1ID in (select tab1ID from...
2
by: psuaudi | last post by:
I have a main query that I would like to call two different subqueries. In MS Access, I usually just save the two subqueries as separate queries which are then called by a third separate and main...
4
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested subqueries form as well ? What are the major...
0
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The...
1
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should use subqueries. Much of what I read online says...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.