Database Articles
  Home arrow Database Articles arrow Page 2 - The From Clause and SQL Queries
IBM developerWorks
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

The From Clause and SQL Queries
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-11-01

    Table of Contents:
  • The From Clause and SQL Queries
  • The from Clause
  • Views
  • Table Links

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The From Clause and SQL Queries - The from Clause


    (Page 2 of 4 )

    Thus far, you have seen queries whose from clauses contain a single table. Although most SQL books will define the from clause as simply a list of one or more tables, I would like to broaden the definition as follows:

    Thefromclause defines the tables used by a query, along with the means of linking the tables together.

    This definition is composed of two separate but related concepts, which will be explored in the following sections.

    Tables

    When confronted with the term table, most people think of a set of related rows stored in a database. While this does describe one type of table, I would like to use the word in a more general way by removing any notion of how the data might be stored and concentrating on just the set of related rows. There are three different types of tables that meet this relaxed definition:

    1. Permanent tables (i.e., created using thecreate table statement)
    2. Temporary tables (i.e., rows returned by a subquery)
    3. Virtual tables (i.e., created using thecreate viewstatement)

    Each of these table types may be included in a query’sfromclause. By now, you should be comfortable with including a permanent table in afromclause, so I will briefly describe the other types of tables that can be referenced in afromclause.

    Subquery-generated tables

    A subquery is a query contained within another query. Subqueries are surrounded by parentheses and can be found in various parts of a select statement; within thefromclause, however, a subquery serves the role of generating a temporary table that is visible from all other query clauses and can interact with other tables named in thefromclause. Here’s a simple example:

      mysql> SELECT e.emp_id, e.fname, e.lname
         
    -> FROM (SELECT emp_id, fname, lname, start_date, title
         
    ->   FROM employee) e;

      +--------+----------+------------+
      | emp_id | fname    | lname      |
      +--------+----------+------------+
      |      1 | Michael  | Smith      |
      |      2 | Susan    | Barker     |
      |      3 | Robert   | Tyler      |
      |      4 | Susan    | Hawthorne  |
      |      5 | John     | Gooding    |
      |      6 | Helen    | Fleming    |
      |      7 | Chris    | Tucker     |
      |      8 | Sarah    | Parker     |
      |      9 | Jane     | Grossman   |
      |     10 | Paula    | Roberts    |
      |     11 | Thomas   | Ziegler    |
      |     12 | Samantha | Jameson    |
      |     13 | John     | Blake      |
      |     14 | Cindy    | Mason      |
      |     15 | Frank    | Portman    |
      |     16 | Theresa  | Markham    |
      |     17 | Beth     | Fowler     |
      |     18 | Rick     | Tulman     |
      +--------+----------+------------+
      18 rows in set (0.00 sec)

    In this example, a subquery against theemployee table returns five columns, and the containing query references three of the five available columns. The subquery is referenced by the containing query via its alias, which, in this case, ise. This is a simplistic and not-particularly-useful example of a subquery in afromclause; you will find complete coverage of subqueries in Chapter 9.

    More Database Articles Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning SQL," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter three of the book Learning SQL, written by Alan Beaulieu (O'Reilly; ISBN: 0596007272). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway