Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Subqueries

September 13, 1998

As we have already discussed, relational databases are table based. That is, all SQL commands are applied to the contents of tables and then those results are displayed as tables themselves. One great consequence of that is that it is fairly easy to perform queries upon the virtual tables created by other queries. Or, in jargonese, to perform subqueries.

Consider this example in which we want to get a listing of all the clients who made a transaction of greater than $150.00.

    SELECT DISTINCT C_NAME, C_PHONE
    FROM CLIENTS
    WHERE EMP_NUM IN
	(
	SELECT C_NUM
	FROM SALES
	WHERE S_AMOUNT > 150.00
	);

We would expect the following results:

    C_NAME		C_PHONE
    --------------------------------
    Jason Lim		456-7890
    Stephen Petersen	167-3333
    --------------------------------

As you can see, the subquery in the WHERE clause would return a set containing "101" and "103". Next, the top-level query would return the two clients. In this case, the DISTINCT operator was not necessary since we don't have many sales in our SALES table. However, for most subqueries, it is useful to include the distinct so that you do not get a client repeated for multiple sales. Of course, you might want that. :)

Joins
Introduction to Databases for the Web | Table of Contents
Adding Data


Up to => Home / Authoring / DB / Intro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers