SQL (Part 5)
SQL (Part 5)
In a relational database, data is stored in tables. An example table would relate Social
Security Number, Name, and Address:
EmployeeAddressTable
SSN FirstName LastName Address City State
512687458 Accenture Smith 83 First Street Howard Ohio
758420012 Mary Scott 842 Vine Ave. Losantiville Ohio
102254896 Shashi Jones 33 Elm St. Paris New York
876512563 IT Ackerman 440 U.S. 110 Upton Michigan
To see the address of each employee. We would Use the SELECT statement, like so:
The general form for a SELECT statement, retrieving all of the rows in the table is:
To get all columns of a table without typing all column names, use:
Conditional Selection
To further discuss the SELECT statement, let's look at a new example table
Empstats
EmployeeIDNo Salary Benefits Position
010 75000 15000 Manager
105 65000 15000 Manager
152 60000 15000 Manager
215 60000 12500 Manager
244 50000 12000 Staff
300 45000 10000 Staff
335 40000 10000 Staff
400 32000 7500 Entry-Level
441 28000 7500 Entry-Level
Relational Operators
There are six Relational Operators in SQL, and after introducing them, we'll see how
they're used:
= Equal
< or != (see
Not Equal
manual)
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To
The WHERE clause is used to specify that only certain rows of the table are displayed,
based on the criteria described in that WHERE clause. It is most easily understood by
looking at a couple of examples.
If you wanted to see the EMPLOYEEIDNO's of those making at or over $50,000, use the
following:
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE SALARY >= 50000;
Notice that the >= (greater than or equal to) sign is used, as we wanted to see those who
made greater than $50,000, or equal to $50,000, listed together. This displays:
EMPLOYEEIDNO
------------
010
105
152
215
244
The WHERE description, SALARY >= 50000, is known as a condition (an operation
which evaluates to True or False).
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE POSITION = 'Manager';
This displays the ID Numbers of all Managers. Generally, with text columns, stick to
equal to or not equal to, and make sure that any text that appears in the statement is
surrounded by single quotes ('.
The AND operator joins two or more conditions, and displays a row only if that row's
data satisfies ALL conditions listed (i.e. all conditions hold true). For example, to display
all staff making over $40,000, use:
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE SALARY > 40000 AND POSITION = 'Staff';
The OR operator joins two or more conditions, but returns a row if ANY of the
conditions listed hold true. To see all those who make less than $40,000 or have less than
$10,000 in benefits, listed together, use the following query:
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE SALARY < 40000 OR BENEFITS < 10000;
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE POSITION = 'Manager' AND SALARY > 60000 OR BENEFITS > 12000;
To generalize this process, SQL performs the AND operation(s) to determine the rows
where the AND operation(s) hold true (remember: all of the conditions are true), then
these results are used to compare with the OR conditions, and only display those
remaining rows where any of the conditions joined by the OR operator hold true (where a
condition or result from an AND is paired with another condition or AND result to use to
evaluate the OR, which evaluates to true if either value is true). Mathematically, SQL
evaluates all of the conditions, then evaluates the AND "pairs", and then evaluates the
OR's (where both operators evaluate left to right).
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE POSITION = 'Manager' AND (SALARY > 50000 OR BENEFITS > 10000);
IN & BETWEEN
An easier method of using compound conditions uses IN or BETWEEN. For example, if
you wanted to list all managers and staff:
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE POSITION IN ('Manager', 'Staff');
or to list those making greater than or equal to $30,000, but less than or equal to $50,000,
use:
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE SALARY BETWEEN 30000 AND 50000;
SELECT EMPLOYEEIDNO
FROM EMPSTATS
WHERE SALARY NOT BETWEEN 30000 AND 50000;
1. NOT
2. AND
3. OR
Using LIKE
Look at the Empstats, and say you wanted to see all people whose last names started with
"S"; try:
SELECT EMPLOYEEIDNO
FROM EMPLOYEEADDRESSTABLE
WHERE LASTNAME LIKE 'S%';
The percent sign (%) is used to represent any possible character (number, letter, or
punctuation) or set of characters that might appear after the "S". To find those people with
LastName's ending in "S", use '%S', or if you wanted the "S" in the middle of the word,
try '%S%'. NOT LIKE displays rows not fitting the given description.
Let's say that you want to list the ID and names of only those people who have sold an
antique. Obviously, you want a list where each seller is only listed once--you don't want
to know how many antiques a person sold This means that you will need to tell SQL to
eliminate duplicate sales rows, and just list each person only once. To do this, use the
DISTINCT keyword.
We also want to eliminate multiple occurrences of the SellerID in our listing, so we use
DISTINCT on the column where the repeats may occur (however, it is generally not
necessary to strictly put the Distinct in front of the column name).
To throw in one more twist, we will also want the list alphabetized by LastName, then by
FirstName (on a LastName tie). Thus, we will use the ORDER BY clause:
In this example, since everyone has sold an item, we will get a listing of all of the s, in
alphabetical order by last name.
Aggregate Functions
five important aggregate functions: SUM, AVG, MAX, MIN, and COUNT. They are
called aggregate functions because they summarize the results of a query, rather than
listing all of the rows.
• SUM () gives the total of all the rows, satisfying any conditions, of the given
column, where the given column is numeric.
• AVG () gives the average of the given column.
• MAX () gives the largest figure in the given column.
• MIN () gives the smallest figure in the given column.
• COUNT(*) gives the number of rows satisfying the conditions.
Looking at the tables at the top of the document, let's look at three examples:
This query shows the total of all salaries in the table, and the average salary of all of the
entries in the table.
SELECT MIN(BENEFITS)
FROM EMPSTATS
WHERE POSITION = 'Manager';
This query gives the smallest figure of the Benefits column, of the employees who are
Managers, which is 12500.
SELECT COUNT(*)
FROM EMPSTATS
WHERE POSITION = 'Staff';
This query tells you how many employees have Staff status (3).