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).
Select SHORT_DESC, PRICE from bookstore where publisher='Wrox';This select result should be:
- 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 ]
- 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.
- 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.
- 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.
- 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.
- Use then the WHERE statement to select rows and to JOIN tables as well.
- After that, you can group select_list with GROUP BY clause for selected columns.
- The GROUP BY clause must come before an ORDER BY clause.
- The HAVING statement works on the ORDER BY clause result as the WHERE statement works on the SELECT statement result.
- 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.