SQL Guide
SQL Guide
00
• Select two fields and sort in descending order by the products name
SELECT fldAmount, fldName FROM tblProducts
ORDER BY fldName DESC WHERE CONDITION(S)
Returns: All records in descending order
Usage: Use the WHERE statement to selectively query a table in a database and only
• Return only unique products affect the records that meet your conditions.
SELECT DISTINCT fldName FROM tblProducts
Returns: Examples: (reference Sample Table)
• Affect only products named Chair
fldName WHERE fldName = ‘Chair’
Chair • Affect only products named Chair and amount is zero
Desk WHERE fldName = ‘Chair’ AND fldAmount = 0
Speakers • Affect only product names that start with C*
Computer Desk WHERE fldName Like ‘C%’
• Return the total of items with a stock quantity greater than zero • Affect only products that have desk somewhere in the name
SELECT COUNT(*) FROM tblProducts WHERE fldQty > 0 WHERE fldName Like ‘%Desk%’
Returns: 4 • Affect only products where the amount is null
WHERE fldAmount Is Null
• Return the total amount of each product • Affect only chairs and desks
SELECT fldName, SUM(fldAmount) WHERE fldName IN (‘Chair’,’Desk’)
FROM tblProducts GROUP BY fldName • Affect products with an amount between 0 and 50
Returns: WHERE fldAmount BETWEEN 0 And 50
* - It should be noted that Access uses the perctange (%) symbol for a wildcard
fldName fldAmount
while other databases may use an asterisk (*).
Chair 56
Desk 102
Speakers 0
Computer Desk 1
UPDATE TABLE_NAME
• Return the total amount of each product having an amount greater than 100
SELECT fldName, SUM(fldAmount) SET COLUMN NAME = VALUE
FROM tblProducts [ WHERE CONDITION(S) ]
GROUP BY fldName HAVING SUM(fldAmount) > 100
Returns: Usage: Updates any or all fields in a table. Used to change the value of a single
field, multiple fields, or all fields where a condition (if supplied) has been satisfied.
* - Any columns that are not defined will be given either a Null value or the default value as set
in the columns default value property. Autonumber columns should not be passed as the
database will automatically assign a value when the record is added. CREATE TABLE TABLE_NAME
( COLUMN_NAME DATA_TYPE [(SIZE)] [NOT NULL]
COLUMN_CONSTRAINT,
[, other column definitions,…]
[, primary key constraint ]
SELECT COLUMN_NAME(S) INTO NEW_TABLE_NAME
)
[ IN EXTERNAL_DATABASE ]
FROM SOURCE_TABLE_NAME
Usage: Creates a new table in an existing database. When creating the table field
[ WHERE CONDITION(S) ] names and the data types must be specified.
Usage: Used to create a make-table query. The most common use for this statement Examples:
is for making backup copies of tables. The SELECT...INTO statement doesn't
define a primary key for the new table.
• Create the products table from code
CREATE TABLE tblProducts (fldName varchar(50),
Examples: fldAmount Integer)
• Make a complete backup of a table* • Create a table and force a field to require a value
SELECT * INTO BackupTable FROM SourceTable CREATE TABLE tblMyTable (TableID Long NOT NULL,
fldName varchar(25))
• Make a backup of select columns inside of a table
SELECT fldOne, fldTwo INTO BackupTable FROM SourceTable
• Make a complete backup of a table into a different database*
SELECT fldOne, fldTwo INTO BackupTable
IN ‘Backup.mdb’ Operate against a collection of values, but return a single, summarizing value.
FROM SourceTable
AVG ( COLUMN_NAME ) - Returns the average value of a column
* - The asterisk should not be used and the field names should be explicitly listed out. This was
done for demonstration purposes only. COUNT ( COLUMN_NAME ) - Returns the row number for any row not
containing a null value for the column
COUNT ( * ) - Returns the number of selected rows
FIRST ( COLUMN_NAME ) - Returns the value of the first record for the
specified field
DELETE FROM TABLE_NAME LAST ( COLUMN_NAME ) - Returns the value of the last record for the
[ WHERE CONDITION(S) ] specified field
MAX ( COLUMN_NAME ) - Returns the maximum value of a column
Usage: Deletes a single or multiple records from a table. Can be used to delete a MIN ( COLUMN_NAME ) - Returns the minimum value of a column
single record, multiple records (using the WHERE clause), or all records. SUM ( COLUMN_NAME ) - Return the total sum of a column
Examples: (reference Sample Table)
COUNT ( DISTINCT COLUMN_NAME ) - Returns the count for all
• Delete all chairs from the products table unique column values*
DELETE FROM tblProducts WHERE fldName = ‘Chair’
• Delete all products * - Access does not support this aggregate function
DELETE FROM tblProducts
Examples: (reference Sample Table)
• Determine the greatest quantity of any product
SELECT MAX(fldAmount) FROM tblProducts
Returns: 60
DROP TABLE TABLE_NAME(s)
• Determine how many unique products (Access)
SELECT DISTINCT COUNT(fldName) FROM tblProducts
Usage: Deletes an entire table from a database. To delete multiple tables separate
Returns: 4
table names by commas
Operate against a single value, and return a single value based on the input value.