Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

SQL Guide

This document is a Quick Reference Guide for Structured Query Language (SQL) that outlines various SQL commands and their usage, including SELECT, UPDATE, INSERT, DELETE, and CREATE TABLE statements. It provides examples for querying data, updating records, inserting new entries, and deleting tables, along with formatting rules for different data types. The guide also covers aggregate functions and scalar functions commonly used in SQL operations.

Uploaded by

Hiroshi Takey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL Guide

This document is a Quick Reference Guide for Structured Query Language (SQL) that outlines various SQL commands and their usage, including SELECT, UPDATE, INSERT, DELETE, and CREATE TABLE statements. It provides examples for querying data, updating records, inserting new entries, and deleting tables, along with formatting rules for different data types. The guide also covers aggregate functions and scalar functions commonly used in SQL operations.

Uploaded by

Hiroshi Takey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Page 1 of 1 Quick Reference Guide $5.

00

Structured Query Language


Table: tblProducts
fldName fldAmount
SELECT [ DISTINCT ] * | LIST OF COLUMNS, FUNCTIONS,
CONSTANTS Chair 0
FROM LIST OF TABLES OR VIEWS Chair 56
[ WHERE CONDITION(S) ] Desk 42
[ ORDER BY ORDERING COLUMN(S) [ ASC | DESC ] ] Desk 60
[ GROUP BY GROUPING COLUMN(S) ] Speakers 0
[ HAVING CONDITION(S) ] Computer Desk 1
Usage: Returns records from a database. The SELECT statement can be written to;
return records where conditions have been met, return records that are grouped Information
(GROUPED BY) or sorted (ORDER BY), return aggregate information about the
records (Count, Min, Max, Sum, Avg), or return unique records (Add the ‘Distinct’ When passing values these formatting rules may need to be applied
keyword to the statement).
Numerical: No special formatting required
Examples: (reference Sample Table) String: Requires a single quote ( ‘ ) on each side of the value
• Select all records from the Products table (tblProducts) Dates: Requires a pound sign ( # ) on each side of the value (Access). MySQL uses a
SELECT * FROM tblProducts single quote ( ‘ ).
Returns: All Records

• 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.

fldName fldAmount Examples: (reference Sample Table)


Desk 102 • Modify all records in the Products table (tblProducts)
UPDATE tblProducts
SET fldName = ‘Pencil’, fldAmount = 6
* - Any field name with special characters (spaces) or
reserved words will need to be enclosed in square brackets [ ] • Change all chairs to a zero amount
UPDATE tblProducts
SET fldAmount = 0
WHERE fldName = ‘Chair

http://www.veign.com Part Number: QRG0003 ©2005 Veign, All Rights Reserved


Page 2 of 2 Quick Reference Guide $5.00

INSERT INTO TABLE_NAME ALTER TABLE TABLE_NAME


[ COLUMN LIST ] {ADD
VALUE (VALUE LIST) {COLUMN field type[(size)] [NOT NULL] [CONSTRAINT index] |
CONSTRAINT multifieldindex} |
Usage: Adds a new record into an existing table. Used to add a record into a table DROP {COLUMN field | CONSTRAINT indexname} }
by explicitly defining the columns or by passing values in the order the columns
appear in the table Usage: Modifies an existing table in a database.

Examples: (reference Sample Table) Examples:


• Add a record into the table (values added in the same order as the columns) • Delete the fldAmount column from the tblProducts table
INSERT INTO tblProducts ALTER TABLE tblProducts DROP COLUMN fldAmount
VALUES (‘Book’,10)
• Add a new Date column to the tblProducts table
• Change all chairs to a zero amount ALTER TABLE tblProducts ADD COLUMN fldDate DateTime
INSERT INTO tblProducts (fldName, fldAmount)
VALUES (‘Book’,10)

* - 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

Examples: (reference Sample Table)


• Delete the products table from the database
DROP TABLE tblProducts

http://www.veign.com Part Number: QRG0003 ©2005 Veign, All Rights Reserved


Page 3 of 3 Quick Reference Guide $5.00

Operate against a single value, and return a single value based on the input value.

Sample of the common Scalar Functions:


AVG ( COLUMN_NAME ) - Returns the average value of a column
UCASE ( COLUMN_NAME ) - Converts a field to upper case
LCASE ( COLUMN_NAME ) - Converts a field to lower case
MID (COLUMN_NAME, start [,end]) - Extract characters from a text field
LEN ( COLUMN_NAME ) - Returns the length of a text field
INSTR ( COLUMN_NAME ) - Returns the numeric position of a named
character within a text field
LEFT (COLUMN_NAME, number_of_char) - Return the left part of a text
field requested
RIGHT (COLUMN_NAME, number_of_char) - Return the right part of a
text field requested
ROUND (COLUMN_NAME, decimals) - Rounds a numeric field to the
number of decimals specified
MOD (x,y) - Returns the remainder of a division operation
NOW () - Returns the current system date
FORMAT (COLUMN_NAME ,format) - Changes the way a field is
displayed
DATEDIFF (d,date1,date2) - Used to perform date calculations

http://www.veign.com Part Number: QRG0003 ©2005 Veign, All Rights Reserved

You might also like