One of the most frequently used SQL statements is the SELECT statement, which extracts specified columns from one or more database tables to build a recordset. Here’s the basic syntax for a SELECT statement:
SELECT ColumnName FROM TableName
You can add line breaks, tabs, and other white space to your statements to clarify the logic: SQL ignores all white space. The following example shows a valid statement:
SELECT PaidDues FROM Members
Note that a query inserted in a dynamic page does not need a semicolon at the end of it. However, a semicolon is required if you address the database directly, such as with the MySQL monitor.
The following keywords identify commonly used SQL commands:
| Keyword | Description |
|---|---|
| SELECT | Retrieves the specified records from a database |
| INSERT | Adds a new record in a database table |
| UPDATE | Changes values in specified database records |
| DELETE | Removes specified database records |
The following keywords are used to refine SQL statements:
| Keyword | Description |
|---|---|
| FROM | Names the data source for an operation |
| WHERE | Sets one or more conditions for the operation |
| ORDER BY | Sorts the recordset rows in a specified order |
| GROUP BY | Groups the recordset by the specified select list items |
The following operators specify conditions and perform logical and numeric functions:
| Operator | Meaning |
|---|---|
| = | Equal to |
| LIKE | Pattern matches (wildcards OK) |
| <> | Not equal to |
| NOT LIKE | Pattern does not match (wildcards OK) |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| AND | Both conditions must be met, such as Louisiana AND Texas |
| OR | At least one condition must be met, such as Smith OR Smyth |
| NOT | Exclude the condition following, such as Paris NOT France |
If the item being compared is text, place it in single quotes as in the following example:
...WHERE Country = 'Germany'
If the item being compared is a date and you’re working with a MySQL database, use the date format YYYY-MM-DD and place the date in single quotes:
...WHERE DateOfBirth < '1970-01-01'
If you're working with a Microsoft Access database, use the MM-DD-YYYY format and place the date in # signs:
...WHERE DateOfBirth < #01/01/1970#
Other databases may have their own date conventions. Consult the database system’s documentation. Some database systems may use non-standard SQL syntax in their products. Check your database system’s documentation.