603 database systems
DESCRIPTION
603 Database Systems. Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems. SQL. Selection in SQL: The selection operator of relational algebra , and much more, is available through the WHERE clause in SQL. SELECT L - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/1.jpg)
603 Database Systems
Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E.
Lecture 20Lecture 20
A First Course in Database Systems
![Page 2: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/2.jpg)
SQLSelection in SQL:
The selection operator of relational algebra, and much more, is available through the WHERE clause in SQL.
SELECT L
FROM R
WHERE C
L( C (R))
![Page 3: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/3.jpg)
SQL SELECT L
FROM R
WHERE C
L( C (R))
We start with the relation in the FROM clause, apply
to each tuple whatever condition is indicated in the WHERE clause, and then project onto the list of attributes and / or expressions in the SELECT clause.
![Page 4: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/4.jpg)
SQLThe following query asks for all the movies made after 1970 that are in black-and-white.
SELECT title
FROM movie
WHERE year > 1970 AND NOT inColor ;
In this condition, we have the AND of two booleans.
The first is an ordinary comparison, but the second is the attribute inColor, negated.
![Page 5: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/5.jpg)
SQLConsider the Query:
SELECT title
FROM Movie
WHERE (year . 1970 OR length < 90)
AND studioName = ‘MGM’ ;
The parentheses are needed here because the precedence of logical operators in SQL is the same as
in most other languages: AND takes precedence over
OR, and NOT takes precedence over both.
![Page 6: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/6.jpg)
SQLRetrieve the name and address of all employees who work in the ‘Research’ department.
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = ‘Research’ AND
DNUMBER = DNO ;FNAME LNAME ADDRESS
John Smith 731 Fondren, Houston, Tx
Franklin Wong 638 Voss, Houston, Tx
Ramesh Narayan 975 Fire Oak, Humble, Tx
Joyce English 5631 Rice, Houston, Tx
![Page 7: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/7.jpg)
SQLFor every project located in ‘Stafford’, list the project number, the controlling department number, and the department managers’ last name , address, and birthdate.
SELECT PNUMBER, DNUM, LNAME, ADDRESS,
BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND
MGRSSN=SSN AND
PLOCATION=‘Stafford’ ;
![Page 8: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/8.jpg)
SQLSELECT PNUMBER, DNUM, LNAME, ADDRESS,
BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND
MGRSSN=SSN AND
PLOCATION=‘Stafford’ ;
The JOIN condition DNUM=DNUMBER relates a project to its controlling department
![Page 9: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/9.jpg)
SQLSELECT PNUMBER, DNUM, LNAME, ADDRESS,
BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND
MGRSSN=SSN AND
PLOCATION=‘Stafford’ ;
The JOIN condition MGRSSN=SSN relates the controlling department to the employee who manages that department
![Page 10: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/10.jpg)
The Database Language SQLDealing With Ambiguous Attribute Names and Aliasing:
In SQL the same name can be used for two (or more)
attributes ==> attributes must be in different relations
Given that this happens and a query refers to two or
more attributes with the same name
==> we must qualify the attribute with the relation
name to prevent ambiguity
![Page 11: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/11.jpg)
SQL LanguageDealing With Ambiguous Attribute Names and Aliasing:
To qualify the attribute name with the relation name prefix the relation name to the attribute name and separate the two by a period.
Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE
relation were called DNUMBER and NAME
and
DNAME attribute of DEPARTMENT was called NAME.
![Page 12: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/12.jpg)
SQL Language
Ex. COMPANY Schema - DNO and LNAME of EMPLOYEE
relation were called DNUMBER and NAME
and
DNAME attribute of DEPARTMENT was called NAME.
SELECT FNAME, EMPLOYEE.NAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DEPARTMENT.NAME=‘Research’ AND DEPARTMENT.DNUMBER=EMPLOYEE.DNUMBER ;
![Page 13: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/13.jpg)
SQLAmbiquity also arises when queries refer to the same relation twice!!
Ex. For each employee, retrieve the employee’s first name and last name and the first and last name of his or her immediate supervisor.
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E EMPLOYEE S
WHERE E.SUPERSSN = S.SSN
E & S ==> aliases for EMPLOYEE relation
![Page 14: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/14.jpg)
SQL
Unspecified WHERE clauses and use of ‘*’:
A missing WHERE clause indicates no condition on tuple selection, hence, all tuples of the relation specified in the FROM clause qualify to be selected for query result
A missing WHERE clause condition WHERE TRUE
for every row in the tuple
![Page 15: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/15.jpg)
SQLQuery: Select all EMPLOYEE SSNs.
SELECT SSN
FROM EMPLOYEE
SSN
123456789
333445555
. for all tuples in EMPLOYEE
.
88866555
![Page 16: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/16.jpg)
SQL
Select all combinations of an EMPLOYEE SSN and a Department DNAME.
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT
result is the Cartesian Crossproduct
![Page 17: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/17.jpg)
SQLRetrieve all attribute values of EMPLOYEE tuples who work in DEPARTMENT number 5.
SELECT *
FROM EMPLOYEE
WHERE DNO=5 ;
RESULT => all attributes for employees of DNO=5.
![Page 18: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/18.jpg)
SQL
Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she works in for every employee of the ‘Research’ department.
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME=‘Research’ AND
DNO=DNUMBER ;
![Page 19: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/19.jpg)
SQLSpecify the Cross Product of the EMPLOYEE and DEPARTMENT relations.
SELECT *
FROM EMPLOYEE , DEPARTMENT
![Page 20: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/20.jpg)
SQL
Tables as Sets in SQL:
In general SQL does not treat a relation as a set
==> duplicate tuples can appear more than once
or
in the result of a query
![Page 21: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/21.jpg)
SQLSQL does not automatically eliminate duplicate
tuples in the results of queries for the following
reasons:
* Duplicate elimination is an expensive
operation. One way to implement it is to
sort the tuples first and then eliminate them.
* The user may want to see duplicate tuples
in the result of a query.
* When an aggregate function is applied to
tuples, in most cases we don’t want to
eliminate duplicates.
![Page 22: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/22.jpg)
SQL
If you want to eliminate duplicates of tuples from the
result of a SQL query use the keyword DISTINCT in the SELECT clause
==> distinct tuples should remain in the result
==> a relation that is a set of tuples
![Page 23: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/23.jpg)
SQL
If we are interested only in distinct salary values (each value appearing once) use keyword DISTINCT:
SELECT DISTINCT SALARY
FROM EMPLOYEE
SALARY
30000
40000
50000
![Page 24: 603 Database Systems](https://reader036.vdocument.in/reader036/viewer/2022062519/56814e67550346895dbc045d/html5/thumbnails/24.jpg)
SQL
Next Lecture
MORE SQL