7/11/2016 Prepared By- Shimul & Hirok,CSE,MBSTU 1
WELCOME
TO OUR
PRESENTATION
SQL and E-R diagram related
problems and its solution.
7/11/2016 2Prepared By- Shimul & Hirok,CSE,MBSTU
Presentation on……
Presented By
• NAME : ID:
• MD.MAHBUBUR RAHMAN CE-12038
• HIROK BISWAS CE-12032
We are going to talking about
 SQL expressions:
 sample bank database(3.19)
 relational schema(3.21,3.22)
E-R diagram for a hospital(6.15)
UML equivalents of the E-R diagrams(6.28)
Conclution
7/11/2016 Prepared By- Shimul & Hirok,CSE,MBSTU 3
Problem 1 :
Using the relations of our sample bank database, write SQL expressions to
define the following views:
a. A view containing the account numbers and customer names (but not the
balances) for all accounts at the Deer Park branch.
b. A view containing the names and addresses of all customers who have an
account with the bank, but do not have a loan.
c. A view containing the name and average account balance of every customer
of the Rock Ridge branch.
Solution of Problem 1 (a)
A view containing the account numbers and customer names (but not
the balances) for all accounts at the Deer Park branch.
Table: accountTable: depositor
7/11/2016 5Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 1 (a)( Cont…)
• Query:
select depositor.customer_name,depositor.account_number
from depositor,account
where depositor.account_number =account.account_number
and account.branch_name='Deer Park';
• Table view:
7/11/2016 6Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 1 (b)
A view containing the names and addresses of all customers who have
an account with the bank, but do not have a borrower.
Table: customer Table: depositor Table: borrower
7/11/2016 7Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 1 (b)(Cont…)
select c.customer_name, c.customer_street,
c.customer_city
from customer c, depositor d
where c.customer_name not in (select
customer_name
from borrower)
and c.customer_name = d.customer_name;
• Query:
• Table view:
7/11/2016 8Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 1 (c)
. A view containing the name and average account balance of every
customer of the 'sidney' branch.
Table: account Table: depositor
7/11/2016 9Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 1 (c) (Cont…)
select customer_name, avg(a.balance)
from account a, depositor d
where a.account_number = d.account_number
and branch_name='sidney'
group by customer_name;
• Query:
• Table view:
7/11/2016 10Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 1 (EXP-02) :
create procedure get_emp(employee_id in varchar(10))
return char
BEGIN
select employee.employee_id,department.department_name,salary.salary_scale,salary.salary_amount from department
inner join employee_department on department.department_id=employee_department.department_id
inner join employee on employee.employee_id=employee_department.employee_id
inner join employee_salary on employee.employee_id=employee_salary.employee_id
inner join salary on salary.salary_scale=employee_salary.salary_scale
where employee.employee_id=employee_id;
return(employee_id);
END;
Exec get_emp('emp02');
Solution of Problem 1 (Exp-2):
Create a procedure that will take Employee_ID it’s parameter return
Employee_ID ,Department_Name, Salary_Scale and Salary_Amount.
7/11/2016 11Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 2 :
Consider the following relational schema
employee(emp_no , name, office, age)
books(isbn , title, authors, publisher)
loan(emp_no , isbn, date)
Write the following queries in SQL.
a. Print the names of employees who have borrowed any book published by
McGraw-Hill.
b. Print the names of employees who have borrowed all books published by
McGraw-Hill.
c. For each publisher, print the names of employees who have borrowed more than five
books of that publisher.
7/11/2016 12Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (a)
Print the names of employees who have borrowed any book published
by McGraw-Hill.
Table: employee
Table: books
Table: loan
7/11/2016 13Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (a) (cont…)
• Query:
select distinct name as employee_name from employee
inner join loan on employee.emp_no=loan.emp_no
inner join books on loan.isbn=books.isbn
where books.publisher='Mcgraw-hill';
• Table view:
7/11/2016 14Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (b)
Print the names of employees who have borrowed all book published
by McGraw-Hill.
Table: employee
Table: books
Table: loan
result
7/11/2016 15Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (b) (cont…)
• Query:
select distinct name as employee_name from employee
inner join loan on employee.emp_no=loan.emp_no
inner join books on loan.isbn=books.isbn
where (select count(loan.isbn) from loan where
loan.emp_no=employee.emp_no)=(select count(books.isbn) from books
where books.publisher='Mcgraw-hill');
• Table view:
7/11/2016 16Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (c)
For each publisher, print the names of employees who have borrowed
more than five books of that publisher.
Table: employee
Table: books
Table: loan
result
More than
5
Books from
Mcgraw-hill
(only)
7/11/2016 17Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 2 (c) (cont…)
• Query:
select name from employee,loan,books
where employee.emp_no=loan.emp_no and
books.isbn=loan.isbn
group by employee.emp_no,name,books.publisher having
count(loan.isbn) >=5;
• Table view:
7/11/2016 18Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 3 :
Consider the relational schema
student(student_id , student_name)
registerd(student_id , course_id)
Write an SQL query to list the student-id and name of each
student along with the total number of courses that the student is
registered for. Students who are not registerd for any course must
also be listed, with the number of registerd
courses shown as 0.
7/11/2016 19Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 3
Table: student
Table: registerd
a is registerd by
3
course
b is registerd by
1
course
C is not
registerd
(0)
7/11/2016 20Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 3 (cont…)
• Query:
select student_id,student_name,(select count(registerd.course_id) from registerd
where student.student_id=registerd.student_id) as taken_course
from student;
• Table view:
7/11/2016 21Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 4:
Consider the relational schema:
Department (Department _ID , Department _Name)
Employee (Employee_ID , Employee_Name,Employee_Age)
Employee _Department( Employee _ID , Department _ID )
Salary (Salary _Scale , Salary _Amount)
Employee_ Salary (Employee _ID , Salary _Scale)
a) Find the Department_names where the employee’s average salary is more
than 10000.
b) Find the employee_names who has paid in salary scale 1.
c) Update the employee ages by increasing 10 years whose ages are not equal
to average age.
7/11/2016 22Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 4 (a)
Find the Department_names where the employee’s average salary is more
than 10000.
Table: department Table: employee_department
Table: employee_salary
Table: salary
7/11/2016 23Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 4(a) (cont…)
• Query:
select department_name,avg(salary.salary_amount) from department
inner join employee_department on
department.department_id=employee_department.department_id
inner join employee_salary on
employee_department.employee_id=employee_salary.employee_id
inner join salary on salary.salary_scale=employee_salary.salary_scale
having avg(salary.salary_amount)>10000
group by department.department_name;
• Table view:
7/11/2016 24Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 4 (b)
Find the employee_names who has paid in salary scale 1.
Table: Employee Table: employee_salary
Table: salary
result
7/11/2016 25Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 4(b) (cont…)
• Query:
select employee_name from employee inner join employee_salary
on employee_salary.employee_id=employee.employee_id
inner join salary on salary.salary_scale=employee_salary.salary_scale
where salary.salary_scale=1;
• Table view:
7/11/2016 26Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Problem 4(c)
• Query:
update employee
set employee.employee_age=employee.employee_age+1
where employee.employee_age not in (select avg(employee.employee_age) from employee);
• Table view:
Before update:
After update:
Average
age
7/11/2016 27Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 5
Construct an E-R diagram for a hospital with a
set of patients and a set of medical doctors.
Associate with each patient a log of the
various tests and examinations conducted.
7/11/2016 28Prepared By- Shimul & Hirok,CSE,MBSTU
Solution Of problem 5:
Fig: E-R diagram for a hospital system.
7/11/2016 29Prepared By- Shimul & Hirok,CSE,MBSTU
Problem 6
Draw the UML equivalents of the
E-R diagrams Of Figures
6.8c,6.9,6.17,6.72,And 6.20.
7/11/2016 30Prepared By- Shimul & Hirok,CSE,MBSTU
Needed Figure of Problem 6 And Its
Solution Given…………………….
7/11/2016 31Prepared By- Shimul & Hirok,CSE,MBSTU
Fig:6.8c Relationship(One –to-one)
Solution of Fig 6.8c:
7/11/2016 32Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Fig 6.9:
7/11/2016 33Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Fig 6.11:
7/11/2016 34Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Fig 6.12:
7/11/2016 35Prepared By- Shimul & Hirok,CSE,MBSTU
7/11/2016 36Prepared By- Shimul & Hirok,CSE,MBSTU
Solution of Fig 6.20(Cont….)
7/11/2016 37Prepared By- Shimul & Hirok,CSE,MBSTU
THANK
YOU
7/11/2016 Prepared By- Shimul & Hirok,CSE,MBSTU 38

SQL and E R diagram

  • 1.
    7/11/2016 Prepared By-Shimul & Hirok,CSE,MBSTU 1 WELCOME TO OUR PRESENTATION
  • 2.
    SQL and E-Rdiagram related problems and its solution. 7/11/2016 2Prepared By- Shimul & Hirok,CSE,MBSTU Presentation on…… Presented By • NAME : ID: • MD.MAHBUBUR RAHMAN CE-12038 • HIROK BISWAS CE-12032
  • 3.
    We are goingto talking about  SQL expressions:  sample bank database(3.19)  relational schema(3.21,3.22) E-R diagram for a hospital(6.15) UML equivalents of the E-R diagrams(6.28) Conclution 7/11/2016 Prepared By- Shimul & Hirok,CSE,MBSTU 3
  • 4.
    Problem 1 : Usingthe relations of our sample bank database, write SQL expressions to define the following views: a. A view containing the account numbers and customer names (but not the balances) for all accounts at the Deer Park branch. b. A view containing the names and addresses of all customers who have an account with the bank, but do not have a loan. c. A view containing the name and average account balance of every customer of the Rock Ridge branch.
  • 5.
    Solution of Problem1 (a) A view containing the account numbers and customer names (but not the balances) for all accounts at the Deer Park branch. Table: accountTable: depositor 7/11/2016 5Prepared By- Shimul & Hirok,CSE,MBSTU
  • 6.
    Solution of Problem1 (a)( Cont…) • Query: select depositor.customer_name,depositor.account_number from depositor,account where depositor.account_number =account.account_number and account.branch_name='Deer Park'; • Table view: 7/11/2016 6Prepared By- Shimul & Hirok,CSE,MBSTU
  • 7.
    Solution of Problem1 (b) A view containing the names and addresses of all customers who have an account with the bank, but do not have a borrower. Table: customer Table: depositor Table: borrower 7/11/2016 7Prepared By- Shimul & Hirok,CSE,MBSTU
  • 8.
    Solution of Problem1 (b)(Cont…) select c.customer_name, c.customer_street, c.customer_city from customer c, depositor d where c.customer_name not in (select customer_name from borrower) and c.customer_name = d.customer_name; • Query: • Table view: 7/11/2016 8Prepared By- Shimul & Hirok,CSE,MBSTU
  • 9.
    Solution of Problem1 (c) . A view containing the name and average account balance of every customer of the 'sidney' branch. Table: account Table: depositor 7/11/2016 9Prepared By- Shimul & Hirok,CSE,MBSTU
  • 10.
    Solution of Problem1 (c) (Cont…) select customer_name, avg(a.balance) from account a, depositor d where a.account_number = d.account_number and branch_name='sidney' group by customer_name; • Query: • Table view: 7/11/2016 10Prepared By- Shimul & Hirok,CSE,MBSTU
  • 11.
    Problem 1 (EXP-02): create procedure get_emp(employee_id in varchar(10)) return char BEGIN select employee.employee_id,department.department_name,salary.salary_scale,salary.salary_amount from department inner join employee_department on department.department_id=employee_department.department_id inner join employee on employee.employee_id=employee_department.employee_id inner join employee_salary on employee.employee_id=employee_salary.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scale where employee.employee_id=employee_id; return(employee_id); END; Exec get_emp('emp02'); Solution of Problem 1 (Exp-2): Create a procedure that will take Employee_ID it’s parameter return Employee_ID ,Department_Name, Salary_Scale and Salary_Amount. 7/11/2016 11Prepared By- Shimul & Hirok,CSE,MBSTU
  • 12.
    Problem 2 : Considerthe following relational schema employee(emp_no , name, office, age) books(isbn , title, authors, publisher) loan(emp_no , isbn, date) Write the following queries in SQL. a. Print the names of employees who have borrowed any book published by McGraw-Hill. b. Print the names of employees who have borrowed all books published by McGraw-Hill. c. For each publisher, print the names of employees who have borrowed more than five books of that publisher. 7/11/2016 12Prepared By- Shimul & Hirok,CSE,MBSTU
  • 13.
    Solution of Problem2 (a) Print the names of employees who have borrowed any book published by McGraw-Hill. Table: employee Table: books Table: loan 7/11/2016 13Prepared By- Shimul & Hirok,CSE,MBSTU
  • 14.
    Solution of Problem2 (a) (cont…) • Query: select distinct name as employee_name from employee inner join loan on employee.emp_no=loan.emp_no inner join books on loan.isbn=books.isbn where books.publisher='Mcgraw-hill'; • Table view: 7/11/2016 14Prepared By- Shimul & Hirok,CSE,MBSTU
  • 15.
    Solution of Problem2 (b) Print the names of employees who have borrowed all book published by McGraw-Hill. Table: employee Table: books Table: loan result 7/11/2016 15Prepared By- Shimul & Hirok,CSE,MBSTU
  • 16.
    Solution of Problem2 (b) (cont…) • Query: select distinct name as employee_name from employee inner join loan on employee.emp_no=loan.emp_no inner join books on loan.isbn=books.isbn where (select count(loan.isbn) from loan where loan.emp_no=employee.emp_no)=(select count(books.isbn) from books where books.publisher='Mcgraw-hill'); • Table view: 7/11/2016 16Prepared By- Shimul & Hirok,CSE,MBSTU
  • 17.
    Solution of Problem2 (c) For each publisher, print the names of employees who have borrowed more than five books of that publisher. Table: employee Table: books Table: loan result More than 5 Books from Mcgraw-hill (only) 7/11/2016 17Prepared By- Shimul & Hirok,CSE,MBSTU
  • 18.
    Solution of Problem2 (c) (cont…) • Query: select name from employee,loan,books where employee.emp_no=loan.emp_no and books.isbn=loan.isbn group by employee.emp_no,name,books.publisher having count(loan.isbn) >=5; • Table view: 7/11/2016 18Prepared By- Shimul & Hirok,CSE,MBSTU
  • 19.
    Problem 3 : Considerthe relational schema student(student_id , student_name) registerd(student_id , course_id) Write an SQL query to list the student-id and name of each student along with the total number of courses that the student is registered for. Students who are not registerd for any course must also be listed, with the number of registerd courses shown as 0. 7/11/2016 19Prepared By- Shimul & Hirok,CSE,MBSTU
  • 20.
    Solution of Problem3 Table: student Table: registerd a is registerd by 3 course b is registerd by 1 course C is not registerd (0) 7/11/2016 20Prepared By- Shimul & Hirok,CSE,MBSTU
  • 21.
    Solution of Problem3 (cont…) • Query: select student_id,student_name,(select count(registerd.course_id) from registerd where student.student_id=registerd.student_id) as taken_course from student; • Table view: 7/11/2016 21Prepared By- Shimul & Hirok,CSE,MBSTU
  • 22.
    Problem 4: Consider therelational schema: Department (Department _ID , Department _Name) Employee (Employee_ID , Employee_Name,Employee_Age) Employee _Department( Employee _ID , Department _ID ) Salary (Salary _Scale , Salary _Amount) Employee_ Salary (Employee _ID , Salary _Scale) a) Find the Department_names where the employee’s average salary is more than 10000. b) Find the employee_names who has paid in salary scale 1. c) Update the employee ages by increasing 10 years whose ages are not equal to average age. 7/11/2016 22Prepared By- Shimul & Hirok,CSE,MBSTU
  • 23.
    Solution of Problem4 (a) Find the Department_names where the employee’s average salary is more than 10000. Table: department Table: employee_department Table: employee_salary Table: salary 7/11/2016 23Prepared By- Shimul & Hirok,CSE,MBSTU
  • 24.
    Solution of Problem4(a) (cont…) • Query: select department_name,avg(salary.salary_amount) from department inner join employee_department on department.department_id=employee_department.department_id inner join employee_salary on employee_department.employee_id=employee_salary.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scale having avg(salary.salary_amount)>10000 group by department.department_name; • Table view: 7/11/2016 24Prepared By- Shimul & Hirok,CSE,MBSTU
  • 25.
    Solution of Problem4 (b) Find the employee_names who has paid in salary scale 1. Table: Employee Table: employee_salary Table: salary result 7/11/2016 25Prepared By- Shimul & Hirok,CSE,MBSTU
  • 26.
    Solution of Problem4(b) (cont…) • Query: select employee_name from employee inner join employee_salary on employee_salary.employee_id=employee.employee_id inner join salary on salary.salary_scale=employee_salary.salary_scale where salary.salary_scale=1; • Table view: 7/11/2016 26Prepared By- Shimul & Hirok,CSE,MBSTU
  • 27.
    Solution of Problem4(c) • Query: update employee set employee.employee_age=employee.employee_age+1 where employee.employee_age not in (select avg(employee.employee_age) from employee); • Table view: Before update: After update: Average age 7/11/2016 27Prepared By- Shimul & Hirok,CSE,MBSTU
  • 28.
    Problem 5 Construct anE-R diagram for a hospital with a set of patients and a set of medical doctors. Associate with each patient a log of the various tests and examinations conducted. 7/11/2016 28Prepared By- Shimul & Hirok,CSE,MBSTU
  • 29.
    Solution Of problem5: Fig: E-R diagram for a hospital system. 7/11/2016 29Prepared By- Shimul & Hirok,CSE,MBSTU
  • 30.
    Problem 6 Draw theUML equivalents of the E-R diagrams Of Figures 6.8c,6.9,6.17,6.72,And 6.20. 7/11/2016 30Prepared By- Shimul & Hirok,CSE,MBSTU
  • 31.
    Needed Figure ofProblem 6 And Its Solution Given……………………. 7/11/2016 31Prepared By- Shimul & Hirok,CSE,MBSTU
  • 32.
    Fig:6.8c Relationship(One –to-one) Solutionof Fig 6.8c: 7/11/2016 32Prepared By- Shimul & Hirok,CSE,MBSTU
  • 33.
    Solution of Fig6.9: 7/11/2016 33Prepared By- Shimul & Hirok,CSE,MBSTU
  • 34.
    Solution of Fig6.11: 7/11/2016 34Prepared By- Shimul & Hirok,CSE,MBSTU
  • 35.
    Solution of Fig6.12: 7/11/2016 35Prepared By- Shimul & Hirok,CSE,MBSTU
  • 36.
    7/11/2016 36Prepared By-Shimul & Hirok,CSE,MBSTU
  • 37.
    Solution of Fig6.20(Cont….) 7/11/2016 37Prepared By- Shimul & Hirok,CSE,MBSTU
  • 38.
    THANK YOU 7/11/2016 Prepared By-Shimul & Hirok,CSE,MBSTU 38