SQL Notes
SQL Notes
SQL Notes
SELECT QUERIES
SQL Query Suntax.
This section teaches you to write a number of
different types of SQL queries.
SQL is based on the data transform language first
named SEQUEL.
This is a relational calculus type of language which
provides three capabilities in a very simple syntax.
The basic syntax as we have already seen is:
SELECT (column list)
FROM (table list)
WHERE (condition clause)
A Sample Query.
This result comes from the ORACLE database
which is in use at SIUE. This database contains all
tables given in the Oracle Press: The Complete
Reference, but you do not need to have this book
in order to use this set of notes.
Describing a Table.
Often it is useful to be able to describe the structure
of a table because you may not be familiar with a
particular table.
The Describe command is used to accomplish
this. This example gives the description of the
WEATHER and LOCATION tables in the ORACLE
database.
BILLED
BILL_NO NUMBER(5) - PRI KEY
PATIENT_NO NUMBER(9)
ITEM_CODE NUMBER(5)
CHARGE NUMBER(7,2)
TREATS
PHY_ID NUMBER(4) - PRI KEY
PATIENT_NO NUMBER(4) - PRI KEY
PROCEDURE_NO NUMBER(4) - PRI KEY
DATE_TREATED DATE - PRI KEY
TREAT_RESULT VARCHAR2(50)
ITEM
ITEM_CODE NUMBER(4) - PRI KEY
DESCRIPTION VARCHAR2(50)
NORMAL_CHARGE NUMBER(7,2)
PHYSICIANS
PHY_ID NUMBER(4) - PRI KEY
PHY_PHONE CHAR(8)
PHY_NAME VARCHAR2(50)
PATIENT
PATIENT_NO NUMBER(4) - PRI KEY
DATE_LAST_TREATED DATE
PAT_NAME VARCHAR2(50)
ROOM_LOCATION CHAR(4)
ROOM
ROOM_LOCATION CHAR(4) - PRI KEY
ROOM_ACCOMODATION CHAR(2)
ROOM_EXTENSION NUMBER(4)
PROCEDURES
PROCEDURE_NO NUMBER(4) - PRI KEY
PROC_DESCRIPTION VARCHAR2(50)
EXAMPLE #11.
To compute how long a patient was in the hospital,
you can perform arithmetic operations on both
numeric as well as date type data.
The example below shows the use of date type data
to list all patients hospitalized more than 6 days.
This also shows an example of using an arithmetic
expression in a WHERE clause.
SELECT patient_no, date_discharged,
date_admitted,
(date_discharged - date_admitted + 1)
As "No. of Days In"
FROM patient
WHERE (date_discharged - date_admitted +
1 ) > 6;
PATIENT_NO DATE_DISC DATE_ADMI No. of
Days In
---------- --------- ---------
--------------
1117 22-DEC-01 16-DEC-
01 7
1114 24-DEC-01 15-DEC-
01 10
1115 25-DEC-01 15-DEC-
01 11
THE IN OPERATOR
The IN operator may be used to replace the OR
operator to simplify a query.
Later you will see examples where IN must be used
for complex queries.
SUBQUERIES
The object of a WHERE clause can be another
SELECT query.
Sometimes this makes it simpler to write a query
that would otherwise have a very complex WHERE
clause.
The two queries shown in the example below are
equivalent. Both queries produce a list of
ITEM_CODEs and the associated description of the
items for a specific patient. To produce the list the
ITEM and BILLED tables must be joined.
EXAMPLE #18 A COMPLEX QUERY AND THE
SUBQUERY.
SELECT item.item_code, description
FROM item, billed
WHERE item.item_code = billed.item_code
AND
patient_no = 1116;
ITEM_CODE DESCRIPTION
---------- ----------------
2242 4 inch dressing
2243 Syringe, 8 gauge
VIEW DEFINITION
A VIEW is a virtual table that does not exist in
reality, but is a logical definition of a set of related
columns, usually from multiple tables.
A VIEW presents data to the end user of an
application system the way that the end user is
used to seeing the data.
o For example, the Customer Order Form is a
view of data from several different tables
including CUSTOMER, ORDERS, PRODUCT,
ORDERLINE, and SALESPERSON.
A VIEW can also be used to simplify query
generation and to add data security to a database
by limiting the data that an end user can access.
A VIEW definition is permanently stored as part of
the database.
The example below creates a view named
PATIENT_BILL that includes the PATIENT_NO,
ITEM_CODE and CHARGE columns from the
BILLED table and the DESCRIPTION column from
the ITEM table, and the DATE_DISCHARGED from
the PATIENT table.
EXAMPLE #20 CREATE A VIEW.
View created.
DERIVED COLUMNS
A view may contain derived (or virtual) columns.
For example, the total charges by patient for room
and special items in a room (item codes between
2200 and 2250).
A view for this aggregate data can be created from
the PATIENT_BILL view that was created in the
example above.
The new view (code shown below for this view of a
view) named ROOM_CHARGE has two virtual
columns named OCCUPANT and ROOM_CHGS.
Note that ROOM_CHARGES is the sum of the
charges for an occupant of the room.
OCCUPANT ROOM_CHGS
-------------------- ----------
Barbara Streisand 175
Eminem 782.55
Sally Field 397.42
END OF NOTES