Database Articles
  Home arrow Database Articles arrow Query Primer
IBM Rational Software Development Conference
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 Rational Software Development Conference
 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Query Primer
    (Page 1 of 4 )

    If you're learning SQL, it's important to understand the Select statement. This article, the first of three parts, dissects its different parts. It is excerpted from chapter three of the book Learning SQL, written by Alan Beaulieu (O'Reilly; ISBN: 0596007272). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    So far, you have seen a few examples of database queries (a.k.a. select statements) sprinkled throughout the first two chapters. Now it’s time to take a closer look at the different parts of the select statement and how they interact.

    Query Mechanics

    Before dissecting the selectstatement, it might be interesting to look at how queries are executed by the MySQL server (or, for that matter, any database server). If you are using the mysql command-line tool (which I assume you are), then you have already logged in to the MySQL server by providing your username and password (and possibly a hostname if the MySQL server is running on a different computer). Once the server has verified that your username and password are correct, a database connection is generated for you to use. This connection is held by the application that requested it (which, in this case, is the mysql tool) until either the application releases the connection (i.e., as a result of your typing quit) or the server closes the connection (i.e., when the server is shut down). Each connection to the MySQL server is assigned an identifier, which is shown to you when you first log in:

      Welcome to the MySQL monitor. Commands end with ; or \g.
      Your MySQL connection id is 2 to server version: 4.1.11-nt

      Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

    In this case, my connection ID is2. This information might be useful to your database administrator if something goes awry, such as a malformed query that runs for hours, so you might want to jot it down.

    Once the server has verified your username and password and issued you a connection, you are ready to execute queries (along with other SQL statements). Each time a query is sent to the server, the server checks the following things prior to statement execution:

    1. Do you have permission to execute the statement?
    2. Do you have permission to access the desired data?
    3. Is your statement syntax correct?

    If your statement passes these three tests, then your query is handed to the query optimizer, whose job it is to determine the most efficient way to execute your query. The optimizer will look at such things as the order in which to join the tables named in the query and what indexes are available, and then picks an execution plan, which is used by the server to execute your query.

    Understanding and influencing how your database server chooses execution plans is a fascinating topic that many of you will wish to explore. For those readers using MySQL, you might consider reading High Performance MySQL (O’Reilly). Among other things, you will learn how to generate indexes, analyze execution plans, influence the optimizer via query hints, and tune your server’s startup parameters. If you are using Oracle Database or SQL Server, there are dozens of tuning books available.

    Once the server has finished executing your query, the result set is returned to the calling application (which is, once again, the mysql tool). As was mentioned in Chapter 1, a result set is just another table containing rows and columns. If your query fails to yield any results, the mysql tool will show you the message found at the end of the following example:

      mysql> SELECT emp_id, fname, lname
          -> FROM employee
         
    -> WHERE lname = 'Bkadfl';
      Empty set (0.00 sec)

    If the query returns one or more rows, the mysql tool will format the results by adding column headers and by constructing boxes around the columns using the -, |, and + symbols, as shown in the next example:

      mysql> SELECT fname, lname
          -> FROM employee;

      +----------+-----------+
      | fname    | lname     |
      +----------+-----------+
      | Michael  | Smith     |
      | Susan    | Barker    |
      | Robert   | Tyler     |
      | Susan    | Hawthorne |
      | John     | Gooding   |
      | Helen    | Fleming   |
      | Chris    | Tucker    |
      | Sarah    | Parker    |
     
    | Jane     | Grossman  |
      | Paula    | Roberts   |
      | Thomas   | Ziegler   |
      | Samantha | Jameson   |
      | John     | Blake     |
      | Cindy    | Mason     |
      | Frank    | Portman   |
      | Theresa  | Markham   |
      | Beth     | Fowler    |
      | Rick     | Tulman    |
      +----------+-----------+
      18 rows in set (0.00 sec)

    This query returns the first and last names of all of the employees in theemployeetable. After the last row of data is displayed, the mysql tool displays a message telling you how many rows were returned, which, in this case, is 18.

    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