Download presentation
Presentation is loading. Please wait.
Published byPaula Douglas Modified over 8 years ago
1
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-usewww.db-book.com 3 SQL
2
©Silberschatz, Korth and Sudarshan3.2Database System Concepts, 5 th Edition, Oct 5, 2006 3-8 a. Select count(*) from license a Where exists(select 1 from owns b,person c where a.car=b.license and b.driver_id=c.drive_id and c.name=‘John Smith’) b. Update license set damage_amount=3000 where report_number=‘AR2197’ and car=‘AABB2000’
3
©Silberschatz, Korth and Sudarshan3.3Database System Concepts, 5 th Edition, Oct 5, 2006 3-9 a. select employee_name from works where company_name=‘First Bank Corporation’ b. select a.employee_name,b.employee_name from employee a,employee b where a.city=b.city and exists( select 1 from works c,works d, company e,company f where a.employee_name=c.employee_name and b.employee_name=d.employee_name and c.company_name=e.company_name and d.company_name=f.company_name and e.city=f.city)
4
©Silberschatz, Korth and Sudarshan3.4Database System Concepts, 5 th Edition, Oct 5, 2006 3-9 c. select a.employee_name,b.employee_name from employee a,employee b where a.city=b.city and a.street=b.street and exists( select 1 from mamages c,manages d where a.employee_name=c.employee_name and b.employee_name=d.employee_name and c.manager_name=d.manager_name) d. select employee_name from works a where a.salary > (select avg(b.salary) from works b where a.company_name=b.company_name group by b.company_name ) e. select company_name from works group by company_name having sum(salary) <= all (select sum(salary) from works group by company_name)
5
©Silberschatz, Korth and Sudarshan3.5Database System Concepts, 5 th Edition, Oct 5, 2006 3-9 e. select company_name from (select company_name,sum(salary),rownum from works group by company_name order by sum(salary)) where rownum=1; select company_name from (select company_name,rank() over order by sum(salary) rank from works group by company_name ) where rank=1;
6
©Silberschatz, Korth and Sudarshan3.6Database System Concepts, 5 th Edition, Oct 5, 2006 3-10 a. update works set salary=salary*1.1 where company_name=‘First Bank Corporation’ b. update works a set salary=salary*1.1 where company_name=‘First Bank Corporation’ and exists(select 1 from manages b where a.employee_name=b.manager_name) c. delete works where company_name=‘Small Bank Corporation’
7
©Silberschatz, Korth and Sudarshan3.7Database System Concepts, 5 th Edition, Oct 5, 2006 3-11 select A from R select * from R where B=17 select * from R,S select A,F from R,S where C=D
8
©Silberschatz, Korth and Sudarshan3.8Database System Concepts, 5 th Edition, Oct 5, 2006 3-12 select * from r1 union select * from r2 select * from r1 intersect select * from r2 select * from r1 minus select * from r2
9
©Silberschatz, Korth and Sudarshan3.9Database System Concepts, 5 th Edition, Oct 5, 2006 3-14 create view man_emp_avg_salary(manager_name,avg_salary) as select a.manager_name,avg(b.salary) from works a,manages b where a.empolyee_name=b.empolyee_name and b.empolyee_name<>b.manager_name group by a.mamager_name
10
©Silberschatz, Korth and Sudarshan3.10Database System Concepts, 5 th Edition, Oct 5, 2006 3-15 select branch_name from (select branch_name,sum(balance) summary from account group by branch_name) a where a.summary >(select avg(summary) from (select branch_name,sum(balance) summary from account group by branch_name) ) select branch_name from account group by branch_name having sum(balance) >(select avg(summary) from (select branch_name,sum(balance) summary from account group by branch_name) )
11
©Silberschatz, Korth and Sudarshan3.11Database System Concepts, 5 th Edition, Oct 5, 2006 3-19 create view account_Deer_Park (account_number,customer_name) as select account_number,customer_name from account a,customer b,depositor c where a.account_number=d.account_number and c.customer_name=b.customer_name and a.branch_name=‘Deer Park’ create view customer_no_loan (customer_name,customer_street,customer_city) as select customer_name,customer_street,customer_city from customer a where exists(select 1 from depositor b where a.customer_name=b.customer_name) and not exists(select 1 from borrower c where a.customer_name=c.customer_name)
12
©Silberschatz, Korth and Sudarshan3.12Database System Concepts, 5 th Edition, Oct 5, 2006 3-19 create view customer_Rock_Ridge(customer_name,avg_balance) as select c.customer_name,avg(a.balance) from account a,depositor b,customer c where a.account_number=b.account_number and b.customer_name=c.customer_name group by c.customer_name where a.branch_name=‘Rock_Ridge’
13
©Silberschatz, Korth and Sudarshan3.13Database System Concepts, 5 th Edition, Oct 5, 2006 3-21 select name from employee a where exists(select 1 from loan b,books c where a.empno=b.empno and b.isbn=c.isbn and c.publisher=‘McGraw-Hill’) select name from employee a where not exists(select 1 from books c where c.publisher=‘McGraw- Hill’ and not exists(select 1 from loan b where a.empno=b.empno and b.isbn=c.isbn ) select x.name,y.publisher from employee x, (select empno,publisher from books b,loan c where and b.isbn=c.isbn group by empno,publisher having count(*)>5) y where x.empno=y.empno
14
©Silberschatz, Korth and Sudarshan3.14Database System Concepts, 5 th Edition, Oct 5, 2006 3-22 select student_id,student_name,nvl(count(course_id),0) from student a left outer join registered b on a.student_id=b.student_id group by student_id,student_name select student_id,student_name,(count(course_id) from student a,registered b where a.student_id=b.student_id group by student_id,student_name union all select student_id,student_name,0 from student a where not exists(select 1 from registered b where a.student_id=b.student_id)
15
©Silberschatz, Korth and Sudarshan3.15Database System Concepts, 5 th Edition, Oct 5, 2006 3-23 select student_id,dense_rank() over (order by score desc) from marks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.