22 Apr Anatomy of an Sql Select Query
Once you have seen (and maybe tried) a simple SQL SELECT query pull things from a database, you may stop thinking of these as magical incantations and begin to see them as powerful tools that are usable and can be learned. The purpose of this article is to show how to build a simple SELECT query using a template. All you need to build a working query are
- The template and
- A database table
First, the template. Since SQL ignores spaces and newlines within a query, the template can have its main components on different lines. So the annotated template follows:
SQL Component | Comments | Examples |
SELECT | Identifies the type of query. Not case-sensitive. Required. | SELECT, Select |
fields | Field/column names in the table definition, comma-separated, case-sensitive. Required | * (for all fields) pID, FName, LName |
FROM table-name | Specifies the table from which the data is to be extracted. Table name is case-sensitive. Required. | FROM FHAbb, from tblNamesX |
WHERE cond | Specifies condition(s) required of each selected row to be requrned. Optional. | WHERE DueDate<NOW |
ORDER BY fld1 [DESC],fld2 [DESC] | Specifies ordering of the returned rows by their field/column names. DESC means descending order, default is ascending.Field names are case-sensitive. Optional. | ORDER BY PostDate DESC Order By LName, FName, MI |
LIMIT [start,] count | Specifies the number of records (‘count’) to be returned. Optionally specify the number of the starting row, e.g., 1501, 25 means return 25 rows starting with the 1501st. Optional. | LIMIT 25 Limit 1501, 50 |
Obviously there is more to SQL than a SELECT query, but this article should help to convince you that we aren’t doing rocket science when we query a database.
Please go to http://www.w3schools.com/sql/ and see what else youi can do with SQL!