SQL Notes
SQL Notes
SQL Notes
common language.
• Productivity - IS professionals can become proficient in
its use.
• Application longevity - A language tends to remain
standard for a long time.
• Reduced dependence on a single vendor.
SQL Data Types.
• Data stored in a relational database can be stored using a
variety of data types. The primary ORACLE data types
are NUMBER, VARCHAR, and CHAR for storing
numbers anda text data; however, there are additional
data types that are supported to support backward
compatability with products.given below:
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.
SQL> Select City, Temperature, Humidity from
WEATHER
2 where Temperature > 80 and Humidity <
90
3 order by Temperature ;
CITY TEMPERATURE HUMIDITY
----------- ----------- ----------
PARIS 81 62
ATHENS 97 89
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)
or Page <> 6
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.
• For example, the Customer Order Form is a view of
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.
END OF NOTES
for any suggestions mail me...
mail:roshan_loyalka2005@rediff.com