Database Articles
  Home arrow Database Articles arrow Page 3 - Query Primer
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

Query Primer
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-10-25

    Table of Contents:
  • Query Primer
  • Query Clauses
  • The select Clause
  • Column Aliases

  • 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

    Query Primer - The select Clause


    (Page 3 of 4 )

    Even though the select clause is the first clause of a selectstatement, it is one of the last clauses to be evaluated by the database server. The reason for this is that before you can determine what to include in the final result set, you need to know all of the possible columns that could be included in the final result set. In order to fully understand the role of theselectclause, therefore, you will need to understand a bit about thefromclause. Here’s a query to get started:

      mysql> SELECT *
          -> FROM department;

      +---------+----------------+
      | dept_id | name           |
      +---------+----------------+
      |       1 | Operations     |
      |       2 | Loans          |
      |       3 | Administration |
      +---------+----------------+
      3 rows in set (0.04 sec)

    In this query, thefromclause lists a single table (department), and theselectclause indicates that all columns (designated by “*”) in thedepartmenttable should be included in the result set. This query could be described in English as follows:

    Show me all the columns in thedepartment table.

    In addition to specifying all of the columns via the asterisk character, you can explicitly name the columns you are interested in, such as:

      mysql> SELECT dept_id, name
          -> FROM department;

      +---------+----------------+
      | dept_id | name           |
      +---------+----------------+
     
    |       1 | Operations     |
      |       2 | Loans          |
      |       3 | Administration |
     
    +---------+----------------+
      3 rows in set (0.01 sec)

    The results are identical to the first query, since all of the columns in thedepartmenttable (dept_idandname) are named in theselect clause. You can choose to include only a subset of the columns in thedepartmenttable as well:

      mysql> SELECT name
          -> FROM department;

      +----------------+
      | name           |
      +----------------+
      | Operations     |
      | Loans          |
      | Administration |
      +----------------+
      3 rows in set (0.00 sec)

    The job of theselectclause, therefore, is the following:

    Theselectclause determines which of all possible columns should be included in the query’s result set.

    If you were limited to including only columns from the table or tables named in thefromclause, things would be rather dull. However, you can spice things up by including in yourselectclause such things as:

    • Literals, such as numbers or strings
    • Expressions, such astransaction.amount *
      -1
       
    • Built-in function calls, such asROUND(transaction.amount, 2)

    The next query demonstrates the use of a table column, a literal, an expression, and a built-in function call in a single query against theemployeetable:

      mysql> SELECT emp_id,
          ->  'ACTIVE',
         
    ->  emp_id * 3.14159,
         
    ->  UPPER(lname)
         
    -> FROM employee;

     

    emp_id  ACTIVE  emp_id * 3.14159  UPPER(lname) 

     

     

    1  ACTIVE

    3.14159 SMITH

     

     

    2 ACTIVE

    6.28318 BARKER

     

     

    3 ACTIVE

    9.42477  TYLER

     

     

    4 ACTIVE

    12.56636 HAWTHORNE

     

     

    5 ACTIVE

    15.70795  GOODING

     

     

    6 ACTIVE

    18.84954FLEMING

     

     

    7  ACTIVE

    21.99113  TUCKER

     

     

    8ACTIVE

    25.13272 PARKER

     

     

    9 ACTIVE

    28.27431 GROSSMAN

     

     

    10 ACTIVE

    31.41590  ROBERTS

     

     

    11 ACTIVE

    34.55749 ZIEGLER

     

     

    12  ACTIVE 

    37.69908  JAMESON

     

     

    13  ACTIVE

    40.84067  BLAKE

     

     

    14 ACTIVE

    43.98226 MASON

     

     

    15 ACTIVE

    47.12385 PORTMAN

     

     

    16 ACTIVE

    50.26544MARKHAM

     

     

    17 ACTIVE

    53.40703 FOWLER

     

     

    18  ACTIVE

    56.54862 TULMAN

     

     

    18 rows in set (0.05 sec)

    Expressions and built-in functions will be covered in detail later, but I wanted to give you a feel for what kinds of things can be included in theselectclause. If you only need to execute a built-in function or evaluate a simple expression, you can skip thefromclause entirely. Here’s an example:

         mysql> SELECT VERSION(),
            
    ->   USER(),
            ->   DATABASE();

     

    VERSION()  USER()  DATABASE() 

     

     4.1.11-nt  lrngsql@localhost  bank 

     

    1 row in set (0.02 sec)

    Since this query simply calls three built-in functions and doesn’t retrieve data from any tables, there is no need for afromclause.

    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 1 hosted by Hostway