Docsity

Class Notes for Basic Query | Biological Network Analysis | CIS 6930, Study notes of Computer Science

Toggle sidebar
bg1
CIS6930 Database System: Design and Implementation
January 28, 2009 - Notes
Todays lecture focuses on SQL commands with examples based off of the following
schema:
Boats(bid, bname, color)
Sailors(sid, sname, rating, age)
Reserves(sid, bid, day)
Remember that the result of a query is a relation.
1 Basic Query
We use all upper case characters to represent SQL keywords
SELECT [DISTINCT] target list
FROM relation list
WHERE qualification list
target list: A list of relation names
relation list: A list of attributes of relations in relation-list
qualification list: Comparisons (Attr op const or Attr1 op Attr2, where op is one of
>, <, =,≤,≥,6=) combined using AND, OR and NOT.
DISTINCT is an optional keyword indicating that the answer should not contain dupli-
cates. If DISTINCT is omitted then duplicates will not be eliminated.
Query 1 Find all sailors with rating higher than 7.
SELECT S.sid
FROM Sailors S
WHERE S.rating >7;
It is a good practice to define variables such as above with Sailors S. We use this S to
represent Sailors. It is useful in cases where we have multiple operations on Sailors occurring.
Query 2 Find the names of sailors who reserved boat number 103.
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid =R.sid AND R.bid =103;
1
pf3
pf4
pf5

Partial preview of the text

Download Class Notes for Basic Query | Biological Network Analysis | CIS 6930 and more Study notes Computer Science in PDF only on Docsity!

CIS6930 Database System: Design and Implementation January 28, 2009 - Notes

Todays lecture focuses on SQL commands with examples based off of the following schema: Boats(bid, bname, color) Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Remember that the result of a query is a relation.

1 Basic Query

We use all upper case characters to represent SQL keywords SELECT [DISTINCT] target list FROM relation list WHERE qualification list target list: A list of relation names relation list: A list of attributes of relations in relation-list qualification list: Comparisons (Attr op const or Attr1 op Attr2, where op is one of

, <, =, ≤, ≥, 6 =) combined using AND, OR and NOT. DISTINCT is an optional keyword indicating that the answer should not contain dupli- cates. If DISTINCT is omitted then duplicates will not be eliminated.

Query 1 Find all sailors with rating higher than 7. SELECT S.sid FROM Sailors S WHERE S.rating > 7;

It is a good practice to define variables such as above with Sailors S. We use this S to represent Sailors. It is useful in cases where we have multiple operations on Sailors occurring.

Query 2 Find the names of sailors who reserved boat number 103. SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103;

2 Conceptual Evaluation of SQL Queries

SQL queries are evaluated with the following conceptual strategy

Step 1: cross product of all tables in relation list; (basically cross product of relation list)

Step 2: do “select based on the conditions in qualification list; (basically a join using where conditions)

Step 3: ‘project’ by the attributes in the target list;

Step 4: eliminate duplicates if DISTINCT flag was used;

WHERE is optional but exercise care when omitting it. SELECT * FROM WHERE ; provides all columns

2.1 More About DISTINCT

SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid = R.sid; In English the above query “selects id of sailors who have reserved a boat.” DISTINCT will only list who has reserved once, otherwise there will be multiple results, one result for each reservation. SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid; In English the above query “selects name of sailors who have reserved a boat.” If we include DISTINCT, as shown in the query below, duplicate sailors names will be removed. This can create problems especially as it is possible for two people to have the same name, in which case only one name will appear. SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid;

AND B.color = ‘red’) INTERSECT (select sid from Sailors S, Reserves R, Boats B where S.sid = R.sid AND R.bid = B.bid AND B.color = ‘green’) Or can be done by not using INTERSECT. We therefore have to use joins. SELECT R1.sid FROM Boats B1, Boats B2, Reserves R1, Reserves R WHERE R1.bid=B1.bid AND R2.bid=B2.bid AND R1.sid = R2.sid AND B1.color=‘red’ AND B2.color=‘green’;

Query 4 can be graphically represented by the table below showing the need for two Reserves and two Boats B1.bid B1.color R1.bid R1.sid B2.bid B2.color R2.bid R2.sid green red

4 Nested Queries

Nested queries are queries within queries. They can be inserted at any point in the query as seen below. SELECT FROM (SELECT FROM WHERE ) as T1, (SELECT FROM WHERE ) as T WHERE (SELECT FROM WHERE ); Todays lecture will focus on nesting in the WHERE clause. Revisiting Query 2 utilizing nested queries we have SELECT S.name FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103); IN is a set related operation. (set attr IN SET) if attr is in set then return true.

How do we evaluate nested queries? Conceptually: for each S.sid we run the nested table and try to match this attr with what is going on in sub query. The two queries (parent/master and child/nested) are unrelated as far as the DBMS is concerned.