Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
Complex SQL Queries ERD
Null Values
When the value could be NULL
Not Know (Birth date of an employee)
(True) Know but I don't want to provide (My home phone I prefer to keep it secret)
(False) Not Applicable: Bachelor degree for student
Null Values
Q: Retrieve the names of all employees who do not have supervisors. (IS or IS NOT are used)
SELECT name FROM employees
WHERE Super_ssn IS NULL;
Nested Queries
Writing A query inside the WHERE clause
Operators:
IN (V IN (Select …) )TRUE if in
ANY = SOME
ALL
>,>=,<,<=, <> !
Nested Queries
SELECT name FROM employees
WHERE Salary > ALL (SELECT Salary FROM Employee WHERE Dno=5);
Q: Retrieve the names of each employees who has dependent with the same first name and the same sex as the employee
SELECT E.fname,E.Lname FROM employees AS E
WHERE E.Ssn IN (SELECT ESSN from DEPENDENT AS D
WHERE E.Fname=D.Dependent_name AND
E.Sex=D.Sex);
Nested Queries
Q: Retrieve the names of each employees who has dependent with the same first name and the same sex as the employee
SELECT E.fname,E.Lname FROM employees AS E, DEPENDENT AS D
WHERE E.Ssn = D.Essn AND E.Sex=D.Sex
AND E.Fname=D.Dependent_name );
Nested Queries
Q: Retrieve the names of each employees who has dependent with the same first name and the same sex as the employee
SELECT E.fname,E.Lname FROM employees AS E
WHERE EXISTS (SELECT * from DEPENDENT AS D
WHERE E.ssn= D.Essn AND
E.Fname=D.Dependent_name AND
E.Sex=D.Sex);
Nested Queries EXISTS
Q: Retrieve the names of employees who has no dependent
SELECT Fname,Lname FROM EMPLOYEE
WHERE NOT EXISTS (SELECT * from DEPENDENT WHERE ssn= Essn
);
Nested Queries EXISTS
Q: List the names of managers have at least one dependent
SELECT Fname,Lname FROM EMPLOYEE
WHERE EXISTS (SELECT * from DEPENDENT WHERE ssn= Essn )
AND EXISTS(SELECT *
from DEPARTMENT WHERE ssn= Mgr_ssn )
Nested Queries EXISTS
Q: What is the project number managed by a manager with last name = Smith
SELECT Pnumber FROM Project
WHERE Pnumber IN (SELECT Pnumber from Project,Department,Employee
WHERE Dnum=Dnumber AND
Mgr_ssn=Ssn AND Lname=‘Smith’);
Nested Queries
Join tables in the FROM clause
SELECT Fname,lname FROM (Employee JOIN Department ON
DNO=Dnumber) WHERE Dname=‘Research’;
Joined
Summarize information from multiple tuples into a single tuple summary:
Count
Sum
Max -Min
AVG
Aggregate functions
Select SUM(Salary),Max(Salary)
FROM Employee
Aggregate functions
Find the sum of the salaries of all employees of the ‘Research’ department, as well as the maximum salary, the minimum salary, and the average salary in this department.
SELECT SUM(Salary),MAX(Salary), FROM (Employee JOIN Department ON
DNO=Dnumber) WHERE Dname=‘Research’;
Joined
Apply the aggregate functions to subgroups of tuples
The subgroups are based on some attribute values.
Find the average salary of employees in each department
!
Grouping
SELECT AVG(Salary) FROM Employee GROUP BY DNO;
SELECT name FROM employees
WHERE Salary > ALL (SELECT Salary FROM Employee WHERE Dno=5);
For each project, retrieve the project number, the project name,and the number of employees who work on that project.
!!
Grouping
SELECT Pnumber,Pname, COUNT(*)) FROM PROJECT, wORKS_ON
WHERE Pnumber=Pno GROUP BY Pnumber,Pname;
Having clause retrieve groups satisfy certain condition
Grouping
SELECT Pnumber,Pname, COUNT(*)) FROM PROJECT, wORKS_ON
WHERE Pnumber=Pno GROUP BY Pnumber,Pname
HAVING COUNT(*) > 2;
ERDEntity (Rectangle) and Attributes (Oval)
ERD
Attributes Types
Composite VS Simple:
Simple: is not divisible (first name)?
Composite: Divided into smaller independent subparts (Address)
ERDSingle-Valued vs Multivalued Attributtes (Double oval)
Age single value
Color of a cars (white & black)
should have upper and lower limit
two colours per car at most
ERD
Derived vs Stored attribute
Age could be derived from birthdate and today’s date
Birth date attribute is stored attribute
ERD
Null Values: Not applicable for this person
Complex attributes: nested multivalued or composite attribute
EntityDB contains group of entities that are similar
Entity share the same attributes but each entity has its own value
Entity type: defines a collection of entities that have the same attributes
Each entity type is described by its name and attributes
Entity
Key Attributtes:
Entity has one or more attribute with distinct values
Each key attribute has underline
Case StudyAn online pharmacy database system that consists of information about the available medicine as well as information about the customers of the pharmacy. The data collected about the medicine is (name, ID, location,price,no.of pieces,etc..) and the information collected about the user is (name ,Id, address,etc). The operation that the system should control are the process of ordering a medicine and the process of returning a medicine. Design the database and the relational schema including the necessary constrains for each attribute and operation.