SQL stands for Structured Query Language.

It is the language used for communicating storing/retrieving data from a database. Each query is divided in to parts, called clauses. The clauses you use depends on the type of query you write.

A query will either select records from the database, or it will change the records in a database. For the first type, there is only one kind: a SELECT query. For the second type, there are three kinds: INSERT, UPDATE, DELETE.

SELECT Queries

A SELECT query starts with the SELECT clause. This clause is a comma-separated list of the fields you want. If the fields are from differing tables that are joined by the query, then it is common to name the field using dot notation: TableName.FieldName.

The next part of a SELECT query is the FROM clause. This clause is a comma-separated list of the tables in which the fields belong. The clause can get more complicated if you choose to join tables together here. We will not be getting into the nature of these types of joins.

Optional clauses for a SELECT query include:

WHERE: This is a comma-separated list of criteria for the query. Criteria restricts the records based on values. You can create joins between tables by specifying that the field from one must equal the field from another
ORDER BY: This is a comma-separated list of fields to alphabetize by.
Example:
SELECT FirstName, LastName FROM Addresss WHERE AddressID < 5 ORDER BY LastName
INSERT Queries

An INSERT Query allows you to add new records.

  1. Start with "INSERT INTO"
  2. Type name of the table
  3. In parentheses, a comma separated list of of the fields you want to insert data into. Note: The auto-number field does not need to have a value inserted into it. It will automatically be filled by the MS Access database.
  4. Type the word "VALUES"
  5. In parentheses, a comma-separated list of the values to insert. Note that text values must be enclosed in quotes, while numeric values are not.

Example: 

INSERT INTO Addresses(FirstName, LastName, YearsKnown) VALUES ("Joe", "Sweeney", 0)

UPDATE Queries

An UPDATE Query allows you to change field values for an existing record or records.

  1. Start with the word "UPDATE"
  2. Type name of the table
  3. Type the word "SET".
  4. After the word "SET" is a comma-separated list of field names and the values to change them to, in the form FieldName=NewValue..
  5. You may optionally include a WHERE clause to restrict the records that are affected by the query.

Example:

UPDATE Addresses SET YearsKnown = 1 WHERE YearsKnown = 0

DELETE Queries

A DELETE Query allows you to remove records from a table.

  1. Start with the words "DELETE FROM"
  2. Type the name of the table.
  3. You may optionally include a WHERE clause to restrict the records that are affected by the query. This is usually a good idea. If you do not include a WHERE clause, then all records will be deleted from the table.

Example:

DELETE FROM Addresses WHERE AddressID = 5

I learned SQL from Microsoft Access by using the query designer and viewing the SQL.

If you have Microsoft Access:

  1. Open the database.
  2. Click on the "Queries" Tab.
  3. Click on the "New" icon in the toolbar
  4. Click on "Design View"
  5. Click on "OK"
  6. A window titled "Show Table" will appear.
  7. Select tables to include in the query
  8. Click in close.
  9. Play around with adding fields and criteria.
  10. Click on "View" in the menu
  11. Click on "SQL View"
  12. You will see the SQL generated by Access

If you do not have Microsoft Access:

  1. Open the table in Visual Data Designer.
  2. Right-click on "Properties" in the tree.
  3. Select "New Query"
  4. A window titled "Query Builder" will appear.
  5. Play around with creating a query.
  6. Click on "Save"
  7. You will be prompted to enter a name for the query
  8. The query will show in the tree of the Database Window.
  9. Click on the plus sign (+) next to the query
  10. Click on the plus sign (+) next to properties
  11. Look down the list to the one that starts "SQL="