complex sql queries - dr. ahmed eltahawey · complex sql queries erd . null values when the value...
TRANSCRIPT
![Page 1: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/1.jpg)
Complex SQL Queries ERD
![Page 2: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/2.jpg)
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
![Page 3: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/3.jpg)
![Page 4: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/4.jpg)
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;
![Page 5: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/5.jpg)
Nested Queries
Writing A query inside the WHERE clause
Operators:
IN (V IN (Select …) )TRUE if in
ANY = SOME
ALL
>,>=,<,<=, <> !
![Page 6: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/6.jpg)
Nested Queries
SELECT name FROM employees
WHERE Salary > ALL (SELECT Salary FROM Employee WHERE Dno=5);
![Page 7: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/7.jpg)
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
![Page 8: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/8.jpg)
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
![Page 9: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/9.jpg)
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
![Page 10: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/10.jpg)
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
![Page 11: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/11.jpg)
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
![Page 12: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/12.jpg)
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
![Page 13: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/13.jpg)
Join tables in the FROM clause
SELECT Fname,lname FROM (Employee JOIN Department ON
DNO=Dnumber) WHERE Dname=‘Research’;
Joined
![Page 14: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/14.jpg)
Summarize information from multiple tuples into a single tuple summary:
Count
Sum
Max -Min
AVG
Aggregate functions
![Page 15: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/15.jpg)
Select SUM(Salary),Max(Salary)
FROM Employee
Aggregate functions
![Page 16: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/16.jpg)
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
![Page 17: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/17.jpg)
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;
![Page 18: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/18.jpg)
SELECT name FROM employees
WHERE Salary > ALL (SELECT Salary FROM Employee WHERE Dno=5);
![Page 19: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/19.jpg)
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;
![Page 20: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/20.jpg)
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;
![Page 21: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/21.jpg)
![Page 22: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/22.jpg)
ERDEntity (Rectangle) and Attributes (Oval)
![Page 23: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/23.jpg)
ERD
Attributes Types
Composite VS Simple:
Simple: is not divisible (first name)?
Composite: Divided into smaller independent subparts (Address)
![Page 24: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/24.jpg)
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
![Page 25: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/25.jpg)
ERD
Derived vs Stored attribute
Age could be derived from birthdate and today’s date
Birth date attribute is stored attribute
![Page 26: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/26.jpg)
ERD
Null Values: Not applicable for this person
Complex attributes: nested multivalued or composite attribute
![Page 27: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/27.jpg)
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
![Page 28: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/28.jpg)
Entity
Key Attributtes:
Entity has one or more attribute with distinct values
Each key attribute has underline
![Page 29: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/29.jpg)
![Page 30: Complex SQL queries - DR. Ahmed Eltahawey · 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](https://reader033.vdocuments.site/reader033/viewer/2022050415/5f8bd4e1709c13573705ae66/html5/thumbnails/30.jpg)
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.