













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!














-^ Written Homework 1 due Nov 2– See course web page– Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only).•^ Today– continue with SQL (chapter 8) -^ SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
optionalclauses
-^ all SELECT statements must have SELECTand FROM clauses–^ SELECT a1, a2, ..., an FROM t1, t2, ..., tm -^ conceptually evaluation of SELECT-FROM1.^ form cross product (t1 x t2 x ... x tm)2. project out attributes (a1, a2, ..., an) from the CP
-^ a * in attribute list is shorthand for “all attributes”– SELECT * FROM employee;– above statement returns the entire employee tableexamples 1,2,
SELECT
a1, a2, ..., an FROM^
t1, t2, ..., tm WHERE cond
1.^ form cross product (t1 x t2 x ... x tm)2.^ retain CP rows where
cond
evaluates to ‘
true’
3.^ project (a1, a2, ..., an) from retained rowssimilar to a m-way join
-^ Query 2: For every project located in 'Stafford', list the projectnumber, the controlling department number, and the departmentmanager's last name, address, and birthdate.
-^ Tables can be given short names to make querieseasier to write (and to resolve ambiguity) SELECT
dname, dlocation FROM^
department as D, dept_locations as DL WHERE D.dnumber = DL.dnumber;
-^ Some queries need to refer to the same relation twice–^ In this case,
aliases^ are given to the relation name
-^ Query 8: For each employee, retrieve the employee's name, and thename of his or her immediate supervisor.Q8:^ SELECT
E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM^
EMPLOYEE AS E, EMPLOYEE AS S WHERE
E.SUPERSSN=S.SSN
-^ In Q8, the alternate relation names E and S are called
aliases^ or
tuple
variables for the EMPLOYEE relation
-^ We can think of E and S as two different
copies^ of EMPLOYEE; E
represents employees in role of
supervisees
and S represents
employees in role of
supervisors
-^ ORDER BY
clause causes returned tuples to be ordered by some
SELECT
attribute (or attributes) SELECT fname, lname, salaryFROM^
employee ORDER BY salary;SELECT fname, lname, salaryFROM^
employee ORDER BY salary DESC;
returns results indescending order
# the ordering field can be a string# (lexical ordering is used)SELECT
fname, lname, salary FROM^
employee ORDER BY
**lname;
fname, lname FROM^
employee ORDER BY
salary;
Conceptual processing ofSELECT-FROM-WHERE
aggregates
SELECT
MAX(salary) FROM^
employee WHERE
dno = 5;
1.^ form cross product (t1 x t2 x ... x tm)2.^ retain CP rows where
cond
evaluates to ‘
true’
3.^ apply aggregate functions in SELECT tocolumn(s) of retained rows
-^ you cannot mix aggregate functions andattributes in select clause (without GROUP BY) SELECT ssn, MAX(salary)FROM employee;
not a legal SQLstatement
how do you write a query to return the SSN andsalary the highest paid employee?
-^ Query 21: For each project, retrieve the project number,project name, and the number of employees who workon that project.Q21:
SELECT
PNUMBER, PNAME, COUNT (*) FROM^
PROJECT, WORKS_ON WHERE
PNUMBER=PNO GROUP BY
PNUMBER, PNAME
-^ HAVING is used w/ GROUP BY to imposeconditions on groups present in result