



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Basic format of the select command select [ distinct ] target_list from tuple_variable_list where qualification [ order by target_list_subset];
Simple query examples use this relational schema: sailors(sid, sname, rating) boats(bid, bname, colour) reserve(sid, bid, date)
“*” is an abbreviation for all attributes in the from list select * from sailors s where order by s.rating
Each item in the target list can be as general as attribute_name = expression , where the expression is any arithmetic or string expression over indexed tuple variables and constants. It can also contain some built-in functions like sqrt, sin, mod, etc. as well as aggregates (coming up later)
Qualifications: each item in a qualification ( where clause) can be as general as expression=expression
Example: select name1 = s1.sname, name2 = s2.sname from sailors s1, sailors s where 2*s1.rating = s2.rating-
Further elaboration: tuple variables can be implicit if the system can figure out which relation each attribute belongs to table names can be used as tuple variables
Example: find names, ages, and departments of employees who are over 40 and work on the first floor. select ename, age, emp.dname from emp, dept where age>40 and floor=1 and emp.dname=dept.dname
Note that it is usually possible to phrase a single query in multiple ways. The previous query could have also been written:
(select ename from emp where dname=“toy”) intersect (select ename from emp where sal? 60K)
Or also (even simpler):
select ename from emp where dname=“toy” and sal? 60K
Writing a query in different ways will usually change how efficient the query is -- the above query is very likely to be faster than the example using intersects , and that one is likely to be faster than the one using minus.
Example: find the names of employees who manage all the departments on the first floor. select mgr from dept d where (select d2.dname from dept d where d1.mgr=d2.mgr) contains (select dname from dept where floor=1)
SQL allows nested queries using the keyword in Example: find the names of employees who work on the first floor. select ename from emp wheredname in (select dname from dept where floor = 1) The same query in flat form is select dname from emp, dept where emp.dname=dept.dname and floor=
Scoping of variables works exactly as in Pascal or C
Example: find the names of students who take a course from their advisor. select sname from student wheres# in (select s# from enroll where c# in (select c# from class where prof=student.advisor))
delete relation_name where qualification
select full_target_list (or “*”) from relation_name where qualification
update relation_name set target_list where qualification
Ext_target_list is identical to target_list in the original update command, but augmented with tuple_variable.attribute_name for all attributes of the range of tuple_variable that don’t appear in target_list.