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.INSERT QueriesThe 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:
Example:
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. SELECT FirstName, LastName FROM Addresss WHERE AddressID < 5 ORDER BY LastName
An INSERT Query allows you to add new records.
- Start with "INSERT INTO"
- Type name of the table
- 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.
- Type the word "VALUES"
- 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.
- Start with the word "UPDATE"
- Type name of the table
- Type the word "SET".
- After the word "SET" is a comma-separated list of field names and the values to change them to, in the form FieldName=NewValue..
- 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.
- Start with the words "DELETE FROM"
- Type the name of the table.
- 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:
If you do not have Microsoft Access: