30
Complex SQL Queries ERD

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

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

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

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

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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
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

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

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

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

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

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

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

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
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

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.