Tips for using SQL
Defining terms:
Remember the terms we discussed: ROW, COLUMN, FIELD, TABLE, DATABASE
SQL is an interesting language. It's not a programming language like
FORTRAN, BASIC, COBOL or PASCAL. In these languages, you specify how the
computer deals with every individual piece of information. In other words,
the order and the structure of the data is rigid and you must tell the
computer not only where each bit of data is, but exactly what to do with
it. But SQL is a nonprocedural language or a language where you simply
tell a system what results you want. You don't have to tell it how to get
it...the system already knows.
Elements of SQL.
SQL is a very logical set of commands. On a given query, you are allowed
to specify the following conditions: the databases you want, the fields
you want, the order of the rows, the order of the columns, whether you
want data to be grouped by a particular attribute, whether you want to
filter the data by a particular attribute and where you want the data to
go.
IN SQL, simple queries usually follow this simple structure:
SELECT (fields)
FROM (databases)
WHERE (fields)<clause>(filter attribute)
GROUP BY (fields)
ORDER BY (fields)
HAVING (fields)
Here is an example of a simple database. We'll use this to illustrate
a few points.
Database name = contributors
| lastname | firstname | amount | candidate |
| Rogers | Mike | $100 | Clinton |
| Kennedy | Marian | $50 | Dole |
| Oglethorpe | Ella | $100 | Clinton |
| Flinchum | Phillip | $75 | Clinton |
| Spence | Skip | $500 | Dole |
| Flinchum | Sarah | $100 | Perot |
In general, when you write a query statement or ask the data a question,
you should do the following steps;
SELECT lastname, firstname, amount
FROM contributors
GROUP BY candidate
ORDER BY amount DESCENDING
HAVING amount>500
Examples:
SELECT * FROM contributors
| lastname | firstname | amount | candidate |
| Rogers | Mike | $100 | Clinton |
| Kennedy | Marian | $50 | Dole |
| Oglethorpe | Ella | $100 | Clinton |
| Flinchum | Phillip | $75 | Clinton |
| Spence | Skip | $500 | Dole |
| Flinchum | Sarah | $100 | Perot |
SELECT lastname, firstname FROM contributors
| lastname | firstname |
| Rogers | Mike |
| Kennedy | Marian |
| Oglethorpe | Ella |
| Flinchum | Phillip |
| Spence | Skip |
| Flinchum | Sarah |
SELECT * FROM contributors WHERE amount > 75 (note: greater than 75 does not include 75)
| Oglethorpe | Ella | $100 | Clinton |
| Spence | Skip | $500 | Dole |
| Flinchum | Sarah | $100 | Perot |
SELECT * FROM contributors WHERE candidate ='Clinton'
| lastname | firstname | amount | candidate |
| Rogers | Mike | $100 | Clinton |
| Oglethorpe | Ella | $100 | Clinton |
| Flinchum | Phillip | $75 | Clinton |
SELECT lastname, firstname, amount FROM contributors ORDER BY amount
| lastname | firstname | amount | candidate |
| Spence | Skip | $500 | Dole |
| Rogers | Mike | $100 | Clinton |
| Flinchum | Sarah | $100 | Perot |
| Oglethorpe | Ella | $100 | Clinton |
| Flinchum | Phillip | $75 | Clinton |
| Kennedy | Marian | $50 | Dole |
SELECT candidate FROM contributors GROUP BY candidate
| candidate |
| Dole |
| Clinton |
When using the GROUP BY function, there are a number of aggregate functions
you can use in the SELECT statement. Examples are SUM(fieldname), AVERAGE(fieldname),
COUNT(fieldname), AND MEDIAN(fieldname).
Examples of these are as follows.
SELECT candidate, SUM(amount) FROM contributors GROUP BY candidate
| candidate | amount |
| Clinton | $275 |
| Dole | $550 |
| Perot | $100 |
SELECT candidate, AVERAGE(amount) FROM contributors GROUP BY candidate
| candidate | amount |
| Clinton | $91.67 |
| Dole | $275 |
| Perot | $100 |