Cartoon Edmund

Home

Calendar

Contact

Knight Vision

Sitemap



Edmund J. Sutcliffe
Thoughtful Solutions, Creatively Implemented and Communicated

Edmund's SQL Quick Reference - 1 2
Click here for a printable version of this page.
Basics of SELECT
This section relates to the standard SQL language found in the majority of relational databases. It is recognised that some of the names may differ by manufacturer. It is also recognised that some database systems my also require you to authenticate (log in).

SIMPLE SELECT
SELECT 4 + 2

Select on its own provides a print statement. If you run this on your system you will get 6.

SELECT SYNTAX
SELECT column1,column...
FROM tablename
[WHERE condition
HAVING condition
GROUP BY columns
ORDER BY columns
COMPUTE BY columns]

[] optional statements.
column is any valid column name for the data in your database tablename.
The tablename is any valid table within your active database.
The condition is a true or false (boolean) statement, used to compare values.

SELECTING ALL
SELECT * FROM tablename

This will select all columns and records from the tablename specified.

CONDITIONAL SELECT

SELECT * FROM stock_cars
WHERE price >= 5000

Conditions allow us to specify a subset of the list of all data stored within a table. The condition is supplied after the where clause and uses logical operators between the column name and the value you are looking for.

LIST OF OPERATORS

= Equal
<> or != Not Equal
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To

Many conditions can be placed after the where clause by using AND and OR logical conditioning.
Example: WHERE price > 5000 AND colour='Red'

IN BETWEEN....
SELECT * FROM
tablename
WHERE manufacturer IN ('Audi', 'Mercedes', 'Jaguar')

SELECT * FROM tablename
WHERE age BETWEEN 18 AND 30

BETWEEN and IN are useful for ranges of values.

LIKE This...
SELECT firstname,surname FROM employees
WHERE position LIKE '%[Mm]anager%'

The use of the like command allows for wildcard substitution, where % is any number of any characters, [list] is a single character within the list, and _ represents any single character.

JOINS
Relational databases store data in tables, and are broken down into normal form. To make a relationship between the tables a join clause is required. This is acheived through a WHERE clause which joins the common columns of 2 or more tables together.

SELECT a.firstname, b.title FROM authors a, books b
WHERE a.au_id=b.au_id

Note that joins require the tables to be joined to follow the FROM clause and for shortcut we have given an alias which follows the table name, but precedes the column names. Our join happens at the WHERE clause using author id from both tables as being the common field.
A JOIN is normally performed across keys of tables, e.g. a Primary key being associated with a Foreign key. A Primary key is a column(s) in a table where all entries must be unique. A Foreign key is a column in a table that relates to a Primary key in another table.


Cartoon Edmund

Quick Ref

Tech Tips

Travel Tips

Tech News

Images