SQL Select Overview

SELECT Overview and Syntax.

Select is the real heart of SQL.

  • Used to answer questions based on your table data: how many, where, what kind of, even what if.
    Basic Select Syntax:
    MySQL, Oracle, SQL server, PostgreSQL:
    SELECT select_list
    FROM table_list
    WHERE search_conditions
  • The SELECT clause identifies the columns you want to retrieve.
  • The FROM clause specifies which tables these columns are in.
  • The WHERE clause qualifies the rows (you select the rows you want to see).
    Example:
    Select SHORT_DESC, PRICE from bookstore
      where publisher='Wrox';
    This select result should be:
    short_desc price
    Professional Ajax 32
    Professional C# 42
    Professional JavaScript for Web Developers 35
    Professional Visual Basic 6 Databases 38
    GDI+ Programming: Creating Custom Controls Using C# 29
  • Think of the SELECT and WHERE clauses as horizontal and vertical axes on a matrix.
  • Full SELECT Syntax:

    SELECT [ALL | DISTINCT] select_list
      FROM table/view_list
      [WHERE search_conditions]
      [GROUP BY group_by_list ]
       [HAVING search_conditions]
      [ORDER BY order_by_list ]
  1. Start with the SELECT statement and then the ALL statement, which is the default, or the DISTINCT statement, which will give you none-repeated group of rows in your select_list.
  2. Give then a list of columns from one or more tables/views and/or constant values as your "select_list" where all the included items are separated with comma.
  3. With the FROM statement you must specify needed tables separated with comma. You may also specify each table with an alias like "customer as c" where the character c is a short notation for the table customer.
  4. When you select columns from multiple tables, you may need to include the table name in your column choices. This is especially true when the same name of the column appears in several tables. The notation for this is when table_name.column_name.
  5. Use then the WHERE statement to select rows and to JOIN tables as well.
  6. After that, you can group select_list with GROUP BY clause for selected columns.
  7. The GROUP BY clause must come before an ORDER BY clause.
  8. The HAVING statement works on the ORDER BY clause result as the WHERE statement works on the SELECT statement result.
  9. The final result, whatever you have used of the WHERE , GROUP BY and HAVING statements or not, can be sorted by specifying columns as an order_by_list with the ORDER BY statement in front.
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.