SQL - Webtop Reference
Updated: 30 July 1998

The Structured Query Language (SQL) is, just what its name implies: a structured language for querying a database. Although many different databases do use their own proprietary language, such as Oracle's PL/SQL, the non-proprietary "standard SQL" is used in almost every database.

There are really only four commands you need to know to perform simple database functions: they are SELECT, INSERT, UPDATE, and DELETE and they're self-explanatory.

There are only a few basic rules.

Strings (any alphanumeric value) must be "wrapped" with tick marks ('). For instance, if I enter my favorite movie into a database, the string should look like: 'Star Wars' -- the database must identify this field as TEXT (or equivalent).

Integers cannot be "wrapped" with anything. So if I enter my favorite number into a database, the integer should look like: 12 -- the database must identify this field as NUMBER (or equivalent).

Date/Time values are a little trickier since it depends on which database you're working with. Usually, however, the date/time values should be "wrapped" with pound signs (#). For instance, if I enter my date of birth, the date/time should look like: #April 12, 1975# -- the database must identify this field as DATE/TIME (or equivalent). Other databases let you "wrap" these values with tick marks or they may not require any such "wrappers."

Commas follow a different rule in SQL compared to English grammar: commas are not placed within the tick marks. For example, if I enter three of my favorite fruits into a database, the command should look something like: 'oranges', 'grapes', 'bananas'. In English grammar, however, the list is typed: "oranges," "grapes," and "bananas." Pay close attention to where the punctuations are placed.

== ^ Return to top of page ==

Select
Basic SELECT statements use only two commands: SELECT and WHERE. The asterisk (*) tells the SELECT statement to grab all the fields in the database table and the WHERE command identifies which table in the database to look in.

SELECT   *
FROM myTable

Advance SELECT statements may employ conditional statements using the WHERE command. These conditional statements include relational operators, logical operators, and other complex conditionals.

Relational Operators
Operator: Symbol:
Equal: =
Not equal: <> or !=
Less than: <
Greater than: >
Less than or equal: <=
Greater than or equal: >=


Logical Operators
Precedence: Operator: Example:
high NOT Age NOT BETWEEN 18 AND 21
medium AND Hair = 'black' AND Eyes = 'brown'
low OR Hair = 'red' OR Hair = 'blonde'


Complex Conditionals
Command: Example:
IN Grade IN ( 'A', 'B' )

instead of [Grade = 'A' AND Grade = 'B']

BETWEEN Age BETWEEN 18 AND 21

instead of [Age >= 18 AND Age <= 21]

LIKE Name LIKE 'J%'

to select all names beginning with "J"



Example:
SELECT   field1, field2
FROM myTable
WHERE ( field3 = 'string3' AND field4 = 'string4' ) OR
( field5 >= integer5 AND field6 = 'string6' )

Joins provide good access to information stored in different table. If, for example, you have two distinct tables -- one to store background information (name, birthday, eye color) called "Employee" and another to store variable information (company, position, salary) called "Job" -- but provide one display with these datafields, you need to join the two tables together. Both tables should have an Employee_ID field to tie the data together.

SELECT   First_Name, Last_Name, Birthday, Position, Salary
FROM Employee, Job
WHERE Employee.Employee_ID = Job.Employee_ID AND
Last_Name LIKE 'A%'
ORDER BY   Last_Name, First_Name ASC

This query gets pertinent information about all employees with a last names beginning with "A." Since both tables have the Employee_ID field, you must "tie" the two tables together. That's what's done with Employee.Employee_ID = Job.Employee_ID. The "ORDER BY" command sorts the information in ascending order (alphabetically) by name. Valid values for ORDER BY are ASC for ascending (default) and DESC for descending.

Aggregate functions provide simple, but useful processing with relatively little effort.

Aggregate Functions
Function: Example:
Count(*) Count(*)
SUM() SUM(Items_Sold)
AVG() AVG(July_Sales)
MAX() MAX(Fees)
MIN() MIN(Cost)


Example:
SELECT   Count(*) As Total
FROM Employees
WHERE First_Name LIKE '%and%'

The example counts how many employees have "and" somewhere in their first name. If Andy, John, Candice, and Madeline are in that table, the example above would return a variable named "Total" with a value of "2."

== ^ Return to top of page ==

Insert
Basic INSERT statements use only two commands: "INSERT INTO" and VALUES.

INSERT INTO   myTable
VALUES ( 'string1', 'string2' )

Advance INSERT statements ...

INSERT INTO   myTable ( field1, field2 )
VALUES ( 'string1', 'string2' )
WHERE field3 = 'string3' AND
field4 = 'string4'

== ^ Return to top of page ==

Update
Basic UPDATE statements use three commands: UPDATE, SET, and WHERE.

Example:
UPDATE   Job
SET Position = 'Manager'
WHERE Employee_ID = 12345678

== ^ Return to top of page ==

Delete
Basic DELETE statements use only two commands: "DELETE FROM" and WHERE.

Example:
DELETE FROM   Employee
WHERE First_Name = 'John' AND
Last_Name = 'Smith'

== ^ Return to top of page ==

http://www.csua.berkeley.edu/~jgwang/sql.htm

Reference:

SQL Tutorial: Introduction to Structured Query Language
http://w3.one.net/~jhoffman/sqltut.htm Exit >

Disclaimer


This web site works best with a JavaScript-enabled browser.

Home | Personal | Web | Aid | Info | Feedback | New