Download Structured Query Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IMDb wikipedia , lookup

Oracle Database wikipedia , lookup

Concurrency control wikipedia , lookup

DBase wikipedia , lookup

Microsoft Access wikipedia , lookup

Functional Database Model wikipedia , lookup

Relational algebra wikipedia , lookup

Ingres (database) wikipedia , lookup

Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

PL/SQL wikipedia , lookup

Versant Object Database wikipedia , lookup

Clusterpoint wikipedia , lookup

SQL wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Tutorial 3: Structured Query Language
This tutorial introduces you to Structured Query Language (SQL). In this tutorial, you will continue
using the Infinity Computers order entry database you worked with in the earlier database tutorials.
Figure 1 shows the structure of the database. Figure 2 shows the initial data in the database.
Figure 1: Database Structure
Figure 2: Initial Data in Database Tables
Customers
CustID CustName
CustStreet
CustCity
text(5)
text(35)
text(50)
text(35)
00001 R. G. Bisco 14 First Street
Tuscaloosa
00002 O. McDonald 1 Hadafarm Street
Northport
00003 D. Duck
23 Pond Lane
Tuscaloosa
00004 F. Bear
5302 University Blvd.
Tuscaloosa
00005 R. Rabbit
1601 Presidential Lane Northport
00006 P. H. Boss
5607 Hardy Street
Hattiesburg
00007 D. Bert
1304 Shenandoa Lane Hattiesburg
00008 C. Bert
12 Oak Ridge Road
Hattiesburg
00009 M. Welby
1 8th Street
Laurel
00010 E. John
19 Jones Lane
Laurel
00011 B. Joel
2308 Red Run Road
Laurel
1
CustState
text(2)
AL
AL
AL
AL
AL
MS
MS
MS
MS
MS
MS
CustZip DiscountRate
text(5) Single (0-0.15)
35404
0.05
35403
0.1
35402
0
35404
0
35403
0.05
39402
0
39401
0.1
39402
0.05
39380
0
39381
0
39380
0.1
SalesReps
SalesRepID SalesRepName SalesRepPhone CommissionRate
text(5)
text(35)
text(10)
Single (0 -0.10)
00001
E. Stein
(555) 555-1234
0.05
00002
Charles E. Chees (555) 555-2345
0.05
00003
D. Harry
(555) 555-0970
0
00004
F. W. Mac
(555) 555-9721
0.1
00005
S. N. Croft
(555) 555-5234
0
ProductID
text(5)
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
Products
Description
text(50)
Pentium 4 2GHz Desktop
Pentium 4 1.5GHz Notebook
128MB Memory Expansion - Notebook
256MB Memory Expansion - Desktop
80GB Hard Disk - Desktop
20GB Hard Disk - Notebook
Iomega Zip 100MB USB
Iomega Zip 250MB ATAPI
Iomega Zip 250MB USB
HP DVD+RW External
HP DVD+RW Internal
2
Price
Reorder Discontinued
Currency Integer
Yes/No
$1,500.00
5
No
$2,500.00
3
No
$64.95
10
No
$79.99
20
No
$129.95
4
No
$179.99
2
No
$79.00
3
No
$149.00
5
No
$179.00
2
No
$449.00
2
No
$349.00
4
No
OrderID
AutoNumber
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Orders
OrderDate CustID
Date/Time text(5)
10/12/2002 00001
10/12/2002 00002
10/13/2002 00005
10/14/2002 00007
10/14/2002 00004
10/16/2002 00006
11/1/2002 00001
11/3/2002 00002
11/7/2002 00003
11/11/2002 00004
11/15/2002 00006
11/16/2002 00003
11/18/2002 00001
11/20/2002 00002
11/25/2002 00006
11/27/2002 00002
11/30/2002 00004
OrderItems
OrderID ProductID Quantity
Long Integer text(5)
Integer
1 10001
3
1 10002
3
2 10001
2
2 10002
1
2 10003
2
2 10004
2
2 10005
2
3 10002
1
3 10003
1
3 10009
1
4 10001
2
4 10004
2
4 10008
2
5 10005
10
6 10010
2
7 10002
1
7 10009
1
8 10005
3
9 10004
5
9 10008
5
10 10001
2
10 10004
2
10 10005
2
10 10008
2
11 10006
2
12 10003
1
13 10001
1
13 10002
1
14 10010
1
14 10011
2
15 10001
3
16 10005
3
17 10003
1
RepID
text(5)
00001
00002
00004
00003
00002
00002
00001
00001
00002
00003
00004
00002
00003
00002
00002
00003
3
Data Manipulation Language Queries
Step 1: Use the Projects.mdb database to practice the DML queries (INSERT, UPDATE, and
DELETE) in the attached Structured Query Language Quick Reference. Enter the sample queries.
Step 2: Use the OrdEnt3.mdb database to create the following DML SQL queries in SQL View.
Figure 2 shows the initial data in the database. The first row in each figure summarizes the type of data
each field may hold, as well as any special validation rules that apply.
Query Notes
• Process each DML query separately; in other words, do not look ahead and combine queries.
• Figure 2 shows the initial data in each table.
• After you perform each INSERT, UPDATE, or DELETE, open the table modified and verify the
changes. When doing an INSERT, make note of the key value assigned to the new record.
• Pay careful attention to data types and validation rules.
• The primary key for the Orders table is an AutoNumber field.
• The primary keys for the other tables are not AutoNumber fields; you must assign a valid primary
key for each record when inserting the record.
• Save each query after you get it working correctly (e.g., DML 01, DML 02, …).
Create the following queries in the OrdEnt3.mdb database using SQL.
1. Add a new customer. The customer’s name is G. Estefan. Estefan’s address is 901 RPG Lane,
Tuscaloosa, AL 35408. Estefan’s discount rate is unknown at this time.
Primary Key Value Assigned to New Record = ___________
2. Once you have successfully executed and saved query 1, close the query. Open the query by
clicking the Open button. Click Yes in response to the “You are about run an append query that will
modify data in your table” message. Click Yes in response to the “You are about to append 1
row(s)” message. You should receive a message that says, “Microsoft Access can’t append all the
records in the append query.” Why do you get this message?
3. Create a query to change the sales representative ID for order number 2 in the Orders table to
‘09999’ and execute the query. Click Yes in response to the “You are about to update 1 row(s)”
message. You should receive a message that says, “Microsoft Access can’t update all the records in
the update query.” Why do you get this message?
4. Add a new sales representative. The sale rep’s name is B. Simpson. Simpson’s phone number is
(555) 555-2340; note that you must enter the phone number without the formatting characters or
spaces. Simpson’s commission rate is 0%.
Primary Key Value Assigned to New Record = ___________
5. Change customer Estefan’s discount rate to 5%.
4
6. Add a new product. The product’s description is SanDisk Cruzer 128MB. The price is $96.95. The
reorder point is 5. Since this is a new product, it is not discontinued.
Primary Key Value Assigned to New Record = ___________
7. Add a new order. The order date is 12/3/2002. The customer is G. Estefan. The sales rep is B.
Simpson.
Primary Key Value Assigned to New Record = ___________
8. Add a new order item for the last order you created (DML command 7). The product purchased is a
SanDisk Cruzer 128MB. The quantity purchased is 3.
Primary Key Value Assigned to New Record = ___________
9. Add a new order item for the last order you created (DML command 7). The product purchased is
an 80GB Hard Disk – Desktop. The quantity purchased is 1.
10. Add a new order. The order date is 12/8/2002. The customer is D. Duck. The sales rep is E. Stein.
Primary Key Value Assigned to New Record = ___________
11. Add a new order item for the last order (DML command 10). The product purchased is a Pentium 4
2GHz Desktop. The quantity purchased is 1.
12. Add a new order item for the last order you created (DML command 10). The product purchased is
an 80GB Hard Disk – Desktop. The quantity purchased is 1.
13. Add a new order item for the last order you created (DML command 10). The product purchased is
a 20GB Hard Disk - Notebook. The quantity purchased is 1.
14. You realize the order item you just entered (DML command 13) is wrong. Delete the order item.
(Be careful that you completely identify the record you want to delete.)
15. Add a new order item for the last order you created (DML command 10). The product purchased is
a HP DVD+RW External. The quantity purchased is 1.
5
Data Query Language (DQL) Queries
Step 1: Use the Projects.mdb database to practice the DQL queries (SELECT) in the attached
Structured Query Language Quick Reference. Enter the sample queries.
Step 2: Use the OrdEnt4.mdb database to create the following SQL queries in SQL View.
Query Notes
• Process each DQL query separately; in other words, do not look ahead and combine queries.
• Pay careful attention to data types.
• When necessary, refer to the tables to determine key and other data va lues.
• Save each query after you get it to work correctly (e.g., DQL 01, DQL 02, …).
Create the following queries in the OrdEnt4.mdb database using SQL.
1. Create a query to display the customer ID and name of each customer.
2. Create a query to display the customer ID and name of each customer. Sort the recordset by the
customer name.
3. Create a query to display the customer ID and name of each customer. Instead of displaying the
field names (custid and custname), display ID and Name as the column headings.
4. Create a query to display the customer ID, name, and discount rate of each customer with a discount
rate less than 7.5%.
5. Create a query to display the customer ID, name, and discount rate of each customer with an
undefined (null) discount rate. (There is a trick to this. Instead of using “discountrate = null ”
in the WHERE clause, use “discountrate IS null ” to find records with a null value for the
discount rate.)
6. Create a query to display the customer ID, name, and order ID for each order.
7. Create a query to display the customer ID, name, and order ID for each order. Sort by the order ID.
8. Create a query to display the customer ID, name, order ID, and product ID for each product
purchased on each order. (You do not need the Products table for this que ry.)
9. Create a query to display the customer ID, name, order ID, product ID, and quantity of each product
purchased on each order. (You do not need the Products table for this query.)
10. Create a query to display the customer ID, name, order ID, product ID, quantity, description, and
price of each product purchased on each order.
11. Create a query to display the customer ID, name, order ID, product ID, quantity, description, price,
and extended price (quantity * price) of each product purchased on each order. Label the extended
price as ExtPrice.
12. Create a query to display the largest (maximum) extended price amount for order items. Label the
amount MaxItem. Do not display any other fields.
6
13. Create a query to display the average extended price amount for order items. Label the amount
AveItem.
14. Create a query to display the customer ID, name, order ID, and sum of the extended price (quantity
* price) for each order. Label the sum as OrdTotal.
15. Create a query to display the customer ID, name, order ID, and sum of the extended price (quantity
* price) for each order. Label the sum as OrdTotal. Sort by the order ID.
16. Create a query to display the customer ID, name, order ID, and sum of the extended price (quantity
* price) for each order. Label the sum as OrdTotal. Sort by the order amount from lowest to
highest.
17. Create a query to display the customer ID, name, order ID, and sum of the extended price (quantity
* price) for each order. Label the sum as OrdTotal. Sort by the order amount from highest to
lowest.
18. Look at the following query. Describe what this query does. (Hint: Start with the SQL code inside
parentheses starting on the second line [the FROM clause]; describe the output from this code.
Based on that, determine what the query as a whole does.)
SELECT MAX(OrdTotal)
FROM (SELECT orders.orderid AS Ord,SUM(quantity*price) AS OrdTotal
FROM orders, orderitems, products
WHERE orders.orderid=orderitems.orderid
AND orderitems.productid=products.productid
GROUP BY orders.orderid);
7
Structured Query Language Quick Reference
A database stores data using an organizational scheme that permits fast, easy access to the data. Databases are common today,
including phonebooks, dictionaries, encyclopedias, maps, and catalogs. Today, many databases are on computers, either a desktop
computer or a large database server on the Internet. A database management system (DBMS) is software used to define the structure
of the database, add or change data, and retrieve data.
Relational Database Concepts
The relational database is the most
common type of database used in
business today. A relational database
provides a flexible, powerful way to
organize and store data.
Sample Database
Tables
A relational database consists of tables.
Each table stores data about one class of
objects. For example, a database might
contain a table to store data about
employees, another for clients, another
for projects, etc. (See the sample
database on the right.)
Records (or Rows)
Within a table, each record stores data
about one member of the class
associated with the table. For example,
in Clients each record stores data about
a different client.
Fields (or Columns)
Within a record, each field stores one
fact about the member of the class
described by the record. For example,
in Clients there might be one field for
the client’s ID (CID) and another for
the client’s name (Name).
Data Types
Each field stores a specific type of data. The data type defines the values that may be stored in the field. The data type als o defines
the ways the field may be used. For example, a field assigned a text data type may store alphabetic characters, numeric digits,
punctuation symbols, and other character data; however, the DBMS cannot use the field for mathematical operations. Common data
types include text, integer, single, double, decimal, currency, date, time, Boolean (yes/no), memo, and binary large object (BLOB).
Primary Keys
Each record in a table must have a unique identifier. A primary key is a field or set of fields whose value uniquely identifies an
individual record in the table. The uniqueness requirement is critical when defining a primary key. For example, in Clients there is a
possibility that two or more clients share the same name; as a result, Name does not u niquely identify an individual record. However
if we assign CID so that no two clients ever share the same client ID, CID is a good primary key.
Relationships
A database generally consists of more than one table. To realize full value from a relational database, there must be some relationship
among the tables. For example, clients hire our company to perform projects. As a result, in the database there is a relationship
between Clients and Projects.
Foreign Keys
To implement a relationship, we must structure the database so that related tables share some fact. A foreign key is a field in one table
that stores the primary key of a record in a related table. Each time a client requests a new project, we store a copy of the client’s ID
in the new project’s record to show that the project is associated with the specified client. CID in Projects is a foreign key since it
stores the client ID for the client requesting the project, providing a way to link a project to the client. Foreign key/primary key
pairings provide the capability to join tables together.
1
Structured Query Language (SQL)
SQL is the primary language used by software to interact with relational databases. Although there is a standard definition of SQL,
individual DBMS developers implement SQL in different ways, resulting in some incompatibilities. We will use the version of SQL
implemented in Microsoft Access 2002.
SQL Queries in Microsoft Access
By default, Microsoft Access displays a QBE interface in the Query Design View. To run SQL queries, do the following.
Select the Queries tab in the Database window. Click the New
icon. Do not add any tables to the query. Click Close in the
Show Table dialog.
Select SQL to display the SQL View.
The recordset resulting from the query displays in a Datasheet
View.
Enter the query in the SQL View. Click the Run icon (red
exclamation mark) to execute the query.
Use the View SQL selection to return to the SQL View to make
changes.
SQL Commands
SQL is a command driven language. SQL consists of three types of commands: Data Definition Language (DDL) commands, Data
Manipulation Language (DML) commands, and Data Query Language (DQL) commands.
Database administrators use DDL commands to define new tables, fields, and primary keys, as well as delete tables and modify
database access rights. This quick reference does not describe DDL commands since experienced database administrators generally
use these commands.
DML Commands
Database users employ DML commands to insert new records, change existing records, and delete records. The primary DML
commands are INSERT, UPDATE, and DELETE.
Example:
INSERT INTO EmpOnProj
VALUES (‘P001’, ‘E103’, #11/2/2002#, null);
INSERT
The INSERT command adds a new record to a table. As a
minimum, you must provide the entire primary key when
inserting a new record. Use the following syntax if you know the
field values at the time of the insert or you wish to assign a null
value for unknown field values.
By using the null keyword in the example above, we specified
that the last field’s value (Hours) is unknown at this time.
The following INSERT achieves the same objective. Use this
syntax if some field values are not know at the time of the insert
or a field in the table has a self-incrementing data type like
AutoNumber. (You must use this format for AutoNumber
primary keys.)
INSERT INTO table
VALUES (value1, value2, . . .);
2
INSERT INTO table (field1, field2, . . .)
VALUES (value1, value2, . . .);
With the INSERT and UPDATE commands, in most cases you
must delimit data values.
Example:
INSERT INTO Projects (PID, CID, ProjMgr)
VALUES (‘P003’, ‘C504’, ‘E102’);
Data Type
Text
Date
Boolean
Number
UPDATE
The UPDATE command changes the value of a field in a record.
The syntax for the UPDATE command is as follows.
Example
‘my text’
‘11/1/2002’ or #11/1/2002#
Yes/No or True/False
11 or 395.2 (No dollar sign or commas)
When you do not know the actual value for a field, you may use
the null keyword to assign a null value.
UPDATE table
SET field = value
WHERE expression;
DELETE
The DELETE command removes a record from a table.
Example:
UPDATE EmpOnProj
SET Hours = 20
WHERE PID = ‘P001’ AND EID = ‘E103’;
DELETE FROM table
WHERE expression;
Example:
DELETE FROM Projects
WHERE PID = ‘P003’;
Use the expression to identify the records or records to update.
To update a single record, specify the entire primary key value of
the record in the expression. In this example, if we had omitted
the “AND EID = ‘E103’” portion of the WHERE clause the
UPDATE command would have changed all records for project
P001.
As with the UPDATE command, a single DELETE may remove
multiple records. To delete a single record specify the entire
primary key.
To update multiple fields using a single UPDATE, separate each
“field = value” pair in the SET clause with a comma.
DQL Commands
Database users employ DQL commands to retrieve data from a database and manipulate data to produce information. The primary
DQL command is SELECT. A query is a request for information from the database implemented using the SELECT command. A
query retrieves records from one or more tables and lists the records in a recordset. A recordset is similar to a table, except a recordset
may consist of data from multiple tables, and the DBMS does not allocate space in the database for the recordset. The recordset only
exists while the query is open.
Query 2
The basic syntax of the SELECT command is as follows.
SELECT *
FROM Clients;
SELECT field1, field2, . . .
FROM table;
Query 1
Recordset 2
SELECT CID, Name
FROM Clients;
CID
Name
City
C501 Smith Farms Dayton
C502 Davis Tractors Dublin
Recordset 1
C503 Jones Feed
Athens
C504 Smith Fencing Hoover
CID
Name
C501 Smith Farms
The SELECT command supports a number of clauses, including
WHERE, ORDER BY, and GROUP BY.
C502 Davis Tractors
C503 Jones Feed
C504 Smith Fencing
WHERE
The WHERE clause performs the same function in the SELECT
command as in the UPDATE and DELETE commands. It selects
records based on s pecific characteristics defined in an expression.
Use an asterisk (*) instead of the field list to list all fields in the
table.
3
SELECT field1, field2, . . .
FROM table
WHERE expression;
this. The first two examples yield the same results, even though
parentheses enclose the two conditions separated by AND in the
second example. This illustrates that the AND operator has
priority over the OR operator. In the third example, parentheses
enclose the two conditions separated by OR yielding a different
recordset.
As illustrated earlier, you can use an expression to select records
based on the value of a single field.
Query 3
Query 6
SELECT *
FROM EmpOnProj
WHERE PID = ‘P001’;
SELECT *
FROM EmpOnProj
WHERE Hours = 20
OR EID = ‘E101’
AND StDate = #11/1/2002#
Recordset 3
PID EID StDate Hours
P001 E101 11/1/2002
40
P001 E102 11/5/2002
15
P001 E103 11/2/2002
Recordset 6
PID EID
P001 E101
P002 E103
P001 E103
20
AND & OR
The AND operator enables you to construct an expression using
two or more fields. The AND operator specifies a condition
where both parts of the expression must be true for a record to be
included in the recordset.
StDate Hours
11/1/2002
40
11/1/2002
20
11/2/2002
20
Query 7
SELECT *
FROM EmpOnProj
WHERE Hours = 20
OR (EID = 'E101'
AND StDate = #11/1/2002#);
Query 4
SELECT *
FROM EmpOnProj
WHERE PID = ‘P001’
AND StDate = #11/1/2002#;
Recordset 7
PID EID
P001 E101
P002 E103
P001 E103
Recordset 4
PID
EID
P001
E101
StDate
Hours
11/1/2002
40
The OR operator also enables you to construct an expression
using two or more fields. The OR operator specifies a condition
where if either part of the expression is true the record appears in
the recordset.
Query 8
SELECT *
FROM EmpOnProj
WHERE (Hours = 20
OR EID = ‘E101’)
AND StDate = #11/1/2002#;
Query 5
SELECT *
FROM EmpOnProj
WHERE PID = ‘P001’
OR StDate = #11/1/2002#;
Recordset 8
PID EID
P001 E101
P002 E103
Recordset 5
PID EID StDate Hours
P001 E101 11/1/2002
40
P001 E102 11/5/2002
P002 E103 11/1/2002
P001 E103 11/2/2002
StDate Hours
11/1/2002
40
11/1/2002
20
11/2/2002
20
StDate Hours
11/1/2002
40
11/1/2002
20
Joins
In many cases, you will construct queries that use more than one
table. A join exists any time you connect two tables in a query.
SQL supports several methods for joining tables; one of the
simplest is by specifying the foreign key/primary key pairing as
part of the expression in a WHERE clause. The basic syntax is as
follows.
15
20
20
The AND operator has precedence over the OR operator unless
overridden using parentheses. The next three examples illustrate
4
Query 12
SELECT table1.field1, table1.field2,
table2.field1, . . .
FROM table1, table2
WHERE table2.foreignkey = table1.primarykey;
SELECT c.CID, c.Name, p.PID, ep.EID
FROM Clients AS c, Projects AS p,
EmpOnProj AS ep
WHERE c.CID = p.CID
AND p.PID = ep.PID;
Query 9
SELECT Clients.CID, Projects.PID
FROM Clients, Projects
WHERE Projects.CID = Clients.CID;
Recordset 12
Recordset 9
CID
Name
PID EID
C501 Smith Farms P001 E101
CID PID
C501 P001
C503 P002
C501 Smith Farms P001 E102
C501 Smith Farms P001 E103
C503 Jones Feed P002 E101
C503 Jones Feed
C503 Jones Feed
Table and Field Aliases
Referencing tables using the SELECT command can become
cumbersome. SQL provides a technique, called aliasing, to
assign short names to the tables in a query.
P002 E102
P002 E103
In some cases, a table may be included in a query even if no data
from the table appears in the recordset. This occurs when two
tables (e.g., Projects and Employees) are not directly related, but
both tables relate to a third table (EmpOnProj). Clearly define
the route from one table to another using the relationships
defined.
Query 10
SELECT c.CID, p.PID
FROM Clients AS c, Projects AS p
WHERE p.CID = c.CID;
Query 13
SELECT c.CID, c.Name, p.PID, e.Name
FROM Clients AS c, Projects AS p,
EmpOnProj AS ep, Employees AS e
WHERE c.CID = p.CID
AND p.PID = ep.PID
AND ep.EID = e.EID;
Recordset 10
CID PID
C501 P001
C503 P002
Note that the results of this query are identical to the results of the
previous query. The only change is that table references in the
SELECT and WHERE parts of the query are shorter. This
technique is especially beneficial in complex queries.
Recordset 13
CID
c.Name
PID e.Name
C501 Smith Farms P001 J. Doe
C501 Smith Farms P001 R. Bee
C503 Jones Feed P002 J. Doe
You can also use aliasing to assign meaningful names to columns.
Query 11
C503 Jones Feed P002 R. Bee
C503 Jones Feed P002 G. Crick
C501 Smith Farms P001 G. Crick
SELECT c.CID AS client, p.PID AS project
FROM Clients AS c, Projects AS p
WHERE p.CID = c.CID;
Combining Joins with Other WHERE Conditions
A single query may combine joins and other expressions to
include data from multiple tables but exclude rows based on
some set of conditions.
Recordset 11
client project
C501 P001
C503 P002
Query 14
SELECT c.CID, c.Name, p.PID, e.Name
FROM Clients AS c, Projects AS p,
EmpOnProj AS ep, Employees AS e
WHERE c.CID = p.CID
AND p.PID = ep.PID
AND ep.EID = e.EID
AND Hours > 15;
Joins with More Than Two Tables
For each additional table in the FROM clause, include a foreign
key/primary key pairing so that the table relates to other tables in
the query.
5
Recordset 14
CID
c.Name
Query 17
SELECT *
FROM Clients
ORDER BY Name DESC;
PID e.Name
C501 Smith Farms P001 J. Doe
C501 Smith Farms P001 G. Crick
C503 Jones Feed P002 R. Bee
C503 Jones Feed
Recordset 17
P002 G. Crick
CID
C504
C501
C503
C502
Derived Values
SQL provides the capability to perform arithmetic operations as
part of queries. SQL supports typical arithmetic operators,
including addition (+), subtraction (-), multiplication (*), and
division (/). SQL performs mathematical operations using the
standard order of precedence (multiplication, division, addition,
and subtraction) unless you override this precedence by grouping
operations using parentheses.
Name
Smith Fencing
Smith Farms
Jones Feed
Davis Tractors
If multiple fields appear in an ORDER BY clause, the DBMS
performs the sort from left to right.
Query 15
Query 18
SELECT c.CID, c.Name, p.PID, e.EID, e.Name,
(Hours * Rate) AS Charge
FROM Clients AS c, Projects AS p,
EmpOnProj AS ep, Employees AS e
WHERE c.CID = p.CID
AND p.PID = ep.PID
AND ep.EID = e.EID;
SELECT *
FROM EmpOnProj
ORDER BY PID DESC,
c.Name
PID
EID
PID
P002
P002
P002
P001
P001
P001
e.Name Charge
C501 Smith Farms P001 E101 J. Doe
C501 Smith Farms P001 E102 R. Bee
C503 Jones Feed P002 E101 J. Doe
1800
570
450
C503 Jones Feed P002 E102 R. Bee
C503 Jones Feed P002 E103 G. Crick
C501 Smith Farms P001 E103 G. Crick
1140
960
960
EID ASC;
Recordset 18
Recordset 15
CID
City
Hoover
Dayton
Athens
Dublin
EID
E101
E102
E103
E101
E102
E103
StDate Hours
11/2/2002
10
11/8/2002
30
11/1/2002
20
11/1/2002
40
11/5/2002
15
11/2/2002
20
GROUP BY and Aggregate Functions
In addition to providing a way to compute derived values using
individual records in a recordset, SQL provides a set of aggregate
functions that operate on multiple records. Common aggregate
functions include average (AVG), count (COUNT), maximum
(MAX), minimum (MIN), and summation (SUM).
ORDER BY
The ORDER BY clause specifies the sort order for records in the
recordset. The default sort order is ascending (ASC).
Aggregate functions and GROUP BY work together. Generally
an aggregate function computes values based on a subset or
subsets of records. The GROUP BY clause defines the subset or
subsets.
Query 16
SELECT *
FROM Clients
ORDER BY Name;
Query 19
Recordset 16
CID
C502
C503
C501
C504
Name
Davis Tractors
Jones Feed
Smith Farms
Smith Fencing
SELECT p.PID, SUM(Hours*Rate) AS Total
FROM Projects AS p,
EmpOnProj AS ep,
Employees AS e
WHERE p.PID = ep.PID
AND ep.EID = e.EID
GROUP BY p.PID;
City
Dublin
Athens
Dayton
Hoover
The DESC parameter changes the sort order to descending.
6
Recordset 19
PID
P001
P002
Query 22
SELECT p.PID, ep.EID,
SUM(Hours*Rate) AS Total
FROM Projects AS p,
EmpOnProj AS ep,
Employees AS e
WHERE p.PID = ep.PID
AND ep.EID = e.EID
GROUP BY p.PID, ep.EID
ORDER BY SUM(Hours*Rate) DESC;
Total
3330
2550
All fields listed in the SELECT clause must be included in the
GROUP BY clause, except fields manipulated by aggregate
functions.
Each field in the GROUP BY clause expands the number of
subsets, use the minimum number of fields in the SELECT and
GROUP BY clauses as possible.
Recordset 22
Query 20
SELECT p.PID, ep.EID,
SUM(Hours*Rate) AS Total
FROM Projects AS p,
EmpOnProj AS ep,
Employees AS e
WHERE p.PID = ep.PID
AND ep.EID = e.EID
GROUP BY p.PID, ep.EID;
EID
E101
E102
E103
E101
E102
E103
Total
1800
P002 E102
P002 E103
P001 E103
1140
960
960
P001 E102
P002 E101
570
450
Notice that you must specify the formula in the ORDER BY
clause if you wish to sort records based on a derived value.
Recordset 20
PID
P001
P001
P001
P002
P002
P002
PID EID
P001 E101
Nested Queries
One interesting, but advanced feature of SQL is the ability to nest
queries within queries. To illustrate, the process to compute the
largest and smallest projects in terms of total charges consists of
two steps: (1) compute the total charge for each project, and (2)
identify the maximum and minimum charges. You can
implement these two steps in one query as follows.
Total
1800
570
960
450
1140
960
Query 23
SELECT MAX(Total) AS MaxCharge,
MIN(Total) AS MinCharge
FROM (SELECT p.PID,
SUM(Hours*Rate) AS Total
FROM Projects AS p, EmpOnProj AS ep,
Employees AS e
WHERE p.PID = ep.PID
AND ep.EID = e.EID
GROUP BY p.PID);
You may use multiple aggregate functions in the same query.
Query 21
SELECT MAX(Hours*Rate) AS Largest,
MIN(Hours*Rate) AS Smallest
FROM Projects AS p,
EmpOnProj AS ep,
Employees AS e
WHERE p.PID = ep.PID
AND ep.EID = e.EID;
Recordset 23
MaxCharge MinCharge
3330
2550
Recordset 21
Largest Smallest
1800
450
The query following the first FROM clause computes the charge
for each project (called Total). This serves as the record source
for the main query that finds the maximum and minimum
charges. This simple example illustrates the power of SQL.
Place the ORDER BY clause after the GROUP BY clause when
used together.
7