Anatomy of an Sql Select Query

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

  1. The template and
  2. 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 ComponentComments Examples
SELECT Identifies the type of query. Not case-sensitive. Required.SELECT, Select
   fieldsField/column names in the table definition, comma-separated, case-sensitive. Required* (for all fields)
pID, FName, LName
FROM table-nameSpecifies the table from which the data is to be extracted. Table name is case-sensitive. Required.FROM FHAbb, from tblNamesX
WHERE condSpecifies condition(s) required of each selected row to be requrned. Optional.

WHERE DueDate<NOW
WHERE Age>18

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,] countSpecifies 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!

Tags:
,