Advertisement
d1i2p3a4k5

DBMS

Apr 23rd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.72 KB | None | 0 0
  1. 1) Student Database:
  2. Student (Sid, Sname, Scity, Gender)
  3. Course (Cid, Cname, Credits)
  4. Reg_for (Sid, Cid)
  5.  
  6. A. Find the details of students those who have registered for DBMS.
  7. B. Find the details of students those who have registered for at least 2 courses.
  8. C. Find the details of course for which more than 2 students have registered.
  9. D. To increase the credits of courses with current credits above 4, by 2 & remaining courses by 1.
  10. E. To display number of credits (total) student wise.
  11. Student
  12. Sid sname Scity gender
  13. 1 Amit Mumbai M
  14. 2 Sachin Navi Mumbai M
  15. 3 Neetu Ahemdabad F
  16. 4 Muskan Delhi F
  17. 5 Shital Chennai M
  18. 6 Naveen Punjab M
  19. 7 Ashwini Kolkata F
  20.  
  21. Course
  22. Cid Cname Credit
  23. 10 COA 4
  24. 11 CG 3
  25. 12 DBMS 5
  26. 13 AOA 5
  27.  
  28. Reg_for
  29. Sid cid
  30. 1 11
  31. 1 12
  32. 2 10
  33. 2 11
  34. 2 12
  35. 3 13
  36. 4 12
  37. 4 13
  38. 4 11
  39. 5 10
  40. 6 10
  41. 6 11
  42. 6 12
  43. 6 13
  44. 7 12
  45. 7 13
  46.  
  47. 2) Create a trigger to set Cid of Reg_for table to new Cid when Cid of Course table is modified/updated.
  48.  
  49. ANSWERS:
  50. 1.) SQL Queries:
  51. a.) Select * from Studentt
  52. where sid IN (Select sid from Reg_for
  53. where cid IN (Select cid from Course
  54. where Cname = 'DBMS'));
  55.  
  56. b.) select * from Studentt
  57. where sid IN (Select sid from Reg_for
  58. group by sid
  59. having count(cid)>1);
  60.  
  61. c.) select * from Course
  62. where cid IN (Select cid from Reg_for
  63. group by cid
  64. having count(sid)>2);
  65.  
  66. d.) update Course
  67. set Credits = Credits+2
  68. where Credits > 4;
  69. update Course
  70. set Credits = Credits+1
  71. where Credits <= 4;
  72.  
  73. e.) Select Reg_for.sid, sum(Course.Credits) AS NumberOfCredits from Course
  74. join Reg_for
  75. on Reg_for.cid = Course.cid
  76. group by Reg_for.sid;
  77.  
  78.  
  79. 2.) create Trigger update_trigger
  80. After Update on Course
  81. FOR EACH ROW
  82. when (new.cid is NOT NULL)
  83. BEGIN
  84. update Reg_for
  85. set cid = :new.cid
  86. where cid = :old.cid;
  87. END;
  88.  
  89.  
  90. 1) Student Database:
  91. Student (Sid, Sname, Scity, Gender)
  92. Course (Cid, Cname, Credits)
  93. Reg_for (Sid, Cid)
  94.  
  95. A. Display the details of courses in which girls have registered.
  96. B. To count number of girl students.
  97. C. To display number of students course wise.
  98. D. To display the list of students those who have registered for course with cid=12, in addition to any other course.
  99. E. To find the details of courses in which a student from Mumbai has registered.
  100. Student
  101. Sid sname Scity gender
  102. 1 Amit Mumbai M
  103. 2 Sachin Navi Mumbai M
  104. 3 Neetu Ahemdabad F
  105. 4 Muskan Delhi F
  106. 5 Shital Chennai M
  107. 6 Naveen Punjab M
  108. 7 Ashwini Kolkata F
  109.  
  110. Course
  111. Cid Cname Credit
  112. 10 COA 4
  113. 11 CG 3
  114. 12 DBMS 5
  115. 13 AOA 5
  116.  
  117. Reg_for
  118. Sid cid
  119. 1 11
  120. 1 12
  121. 2 10
  122. 2 11
  123. 2 12
  124. 3 13
  125. 4 12
  126. 4 13
  127. 4 11
  128. 5 10
  129. 6 10
  130. 6 11
  131. 6 12
  132. 6 13
  133. 7 12
  134. 7 13
  135.  
  136. 2) Create a trigger to set Sid of Reg_for table to new Sid when Sid of Student table is modified/updated.
  137.  
  138. ANSWERS:
  139. 1.) SQL Queries:
  140. a.) Select * from Course
  141. where cid IN (Select cid from Reg_for
  142. where sid IN (Select sid from Studentt
  143. where Gender = 'F'));
  144.  
  145. b.) Select count(sid) from Studentt
  146. where Gender = ‘F’;
  147.  
  148. c.) select cid, count(distinct sid) AS NumberOfStudents from Reg_for
  149. group by cid;
  150.  
  151. d.) select sid,sname from Studentt
  152. where sid IN(select sid from Reg_for
  153. where cid IN (select cid from Reg_for
  154. where sid IN(select sid from Reg_for
  155. group by sid
  156. having count(sid)>1)) and cid = 12);
  157. e.) Select * from Course
  158. where cid IN (Select cid from Reg_for
  159. where sid IN (Select sid from Studentt
  160. where scity = 'Mumbai'));
  161.  
  162.  
  163. 2.) create Trigger update_sid_trigger
  164. After Update on Studentt
  165. FOR EACH ROW
  166. when (new.sid is NOT NULL)
  167. BEGIN
  168. update Reg_for
  169. set sid = :new.sid
  170. where sid = :old.sid;
  171. END;
  172.  
  173.  
  174. 1) Emp (eid, ename, esal, ecity)
  175. Project (pid, pname, plocation, pleaderid)
  176. Works_on (eid, pid)
  177.  
  178. A. To find the details of all the employees those who are working on a project whose project leader is employee with eid=2.
  179. B. To find the details of emps those who are not working on any project.
  180. C. To find the details of projects on which emp from Mumbai is working.
  181. D. To increase the salary of those who are working on project “BIGDATA” by 20%.
  182. E. To display the details of project on which no employee is working.
  183. Emp
  184. Eid Ename Esal Ecity
  185. 1 Amit 50000 Mumbai
  186. 2 Sachin 30000 Pune
  187. 3 Neetu 20000 Pune
  188. 4 Muskan 60000 Mumbai
  189. 5 Shital 55000 Delhi
  190. 6 Naveen 45000 Murbad
  191. 7 Ashwini 59000 Chennai
  192. 8 Ritika 67000 hyderabad
  193.  
  194. Project
  195. Pid pname plocation Pleaderid
  196. 10 NETWORK Mumbai 3
  197. 11 BIG DATA Delhi 2
  198. 12 CLOUD USA 2
  199. 13 ANDROID UAE 3
  200. 14 IP NY NULL
  201.  
  202. Works_on
  203. Eid Pid
  204. 1 10
  205. 1 11
  206. 1 13
  207. 2 10
  208. 2 13
  209. 3 10
  210. 3 13
  211. 5 13
  212. 6 11
  213. 6 13
  214. 7 12
  215. 8 12
  216. 8 11
  217.  
  218. 2) Create a trigger to set Eid of Works_on table to new Eid when Eid of Employee table is modified/updated.
  219.  
  220. ANSWERS:
  221. 1.) SQL Queries:
  222. a.) select * from Emp
  223. where eid IN (select eid from Works_on
  224. where pid in (Select pid from Project
  225. where pleaderid = 2));
  226.  
  227. b.) select * from Emp
  228. where eid NOT IN (select eid from Works_on
  229. where pid is not null);
  230. OR (we can write only)
  231.  
  232. select * from Emp
  233. where eid NOT IN (select eid from Works_on);
  234. c.) select * from Project
  235. where pid IN (select pid from Works_on
  236. where eid in (Select eid from Emp
  237. where ecity = 'Mumbai'));
  238.  
  239. d.) update Emp
  240. set esal = esal + 0.20
  241. where eid in (select eid from Works_on
  242. where pid in (select pid from Project
  243. where pname = 'BIG DATA'));
  244.  
  245. e.) select * from Project
  246. where pid NOT IN (select pid from Works_on
  247. where eid is not null);
  248.  
  249. OR (we can write only)
  250.  
  251. select * from Project
  252. where pid NOT IN (select pid from Works_on);
  253.  
  254. 2.) create Trigger update_eid_trigger
  255. After Update on Emp
  256. FOR EACH ROW
  257. when (new.eid is NOT NULL)
  258. BEGIN
  259. update Works_on
  260. set eid = :new.eid
  261. where eid = :old.eid;
  262. END;
  263.  
  264.  
  265.  
  266. 1) Emp (eid, ename, esal, ecity)
  267. Project (pid, pname, plocation, pleaderid)
  268. Works_on (eid, pid)
  269.  
  270. A. To find details of employees those who are working on atleast all the projects whose project leader is an employee with id=3.
  271. B. To display employee details in the sorted order of their salary & name.
  272. C. To find top 5 employees as per their salary (top 5 earners).
  273. D. To find employee details with a city name starting with ‘m’, with second last letter as ‘A’ and consisting of 6 characters only.
  274. E. To find eid, ename of those with salary above the average salary of employees.
  275.  
  276. Emp
  277. Eid ename Esal Ecity
  278. 1 Amit 50000 Mumbai
  279. 2 Sachin 30000 Pune
  280. 3 Neetu 20000 Pune
  281. 4 Muskan 60000 Mumbai
  282. 5 Shital 55000 Delhi
  283. 6 Naveen 45000 Murbad
  284. 7 Ashwini 59000 Chennai
  285. 8 Ritika 67000 Hyderabad
  286.  
  287. Project
  288. Pid pname plocation Pleaderid
  289. 10 NETWORK Mumbai 3
  290. 11 BIG DATA Delhi 2
  291. 12 CLOUD USA 2
  292. 13 ANDROID UAE 3
  293. 14 IP NY NULL
  294.  
  295. Works_on
  296. Eid Pid
  297. 1 10
  298. 1 11
  299. 1 13
  300. 2 10
  301. 2 13
  302. 3 10
  303. 3 13
  304. 5 13
  305. 6 11
  306. 6 13
  307. 7 12
  308. 8 12
  309. 8 11
  310.  
  311. 2) Create a trigger to set Pid of Works_on table to new Pid when Pid of Project table is modified/updated.
  312.  
  313. ANSWERS:
  314. 1.) SQL Queries:
  315. a.) select * from Emp e
  316. where NOT EXISTS ((Select pid from Project
  317. where pleaderid = 3)except(select pid from Works_on w
  318. where e.eid = w.eid));
  319.  
  320. b.) select * from Emp
  321. order by ename,esal DESC;
  322.  
  323. c.) select * from (select * from Emp
  324. order by esal DESC)
  325. where ROWNUM <= 5;
  326.  
  327. d.) select * from Emp
  328. where ecity LIKE 'm___A_';
  329.  
  330. e.) select eid,ename from Emp
  331. where esal > (select AVG(esal) from Emp);
  332.  
  333. 2.) create Trigger update_eid_trigger
  334. After Update on Project
  335. FOR EACH ROW
  336. when (new.pid is NOT NULL)
  337. BEGIN
  338. update Works_on
  339. set pid = :new.pid
  340. where pid = :old.pid;
  341. END;
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348. 1)
  349. Table Salesperson
  350.  
  351. ID Name Age Salary
  352. 1 Abe 61 140000
  353. 2 Bob 34 44000
  354. 5 Chris 34 40000
  355. 7 Dan 41 52000
  356. 8 Ken 57 115000
  357. 11 Joe 38 38000
  358.  
  359.  
  360. Table Customer
  361.  
  362. ID Name City Industry Type
  363. 4 Samsonic pleasant J
  364. 6 Panasung oaktown J
  365. 7 Samony jackson B
  366. 9 Orange Jackson B
  367.  
  368. Table Orders
  369. (Note: Date in MM/DD/YYYY format)
  370.  
  371. Order_Number order_date cust_id salesperson_id Amount
  372. 10 8/2/1996 4 2 540
  373. 20 1/30/1999 4 8 1800
  374. 30 7/14/1995 9 1 460
  375. 40 1/29/1998 7 2 2400
  376. 50 2/3/1998 6 7 600
  377. 60 3/2/1998 6 7 720
  378. 70 5/6/1998 9 7 150
  379.  
  380. Create all the above three tables with following constraints:
  381. Create PRIMARY KEYS on each of these three tables
  382. Create FOREIGN KEYS for establishing referential integrity between various tables
  383.  
  384. A. Retrieve Id, Name and Age columns from SalesPerson table as “Sales Person ID”, “Sales Person Name” and “Age in Years” respectively
  385.  
  386. B. Retrieve Id and Name (in capital letters) as single column with ‘-‘ in between and with column heading as ‘Id and Name’ along with Age field from Sales Person table(Use Concatenation Operator)
  387.  
  388. C. Insert one sales person row in SalesPerson table for which Salary value is not available
  389.  
  390. D. Retrieve information of all sales persons for whom Salary value is available
  391.  
  392. E. Retrieve information of all sales persons having salary in the range of 90000 to 150000 both included
  393.  
  394. 2) Create view to find Sales Person ID wise total order amount having total order amount greater than 1500 in descending order of total order amount
  395.  
  396.  
  397. ANSWERS:
  398.  
  399. 1.) SQL Queries:
  400. a.) select ID as "Sales Person ID",Name As "Sales Person Name",Age As "Age In Years" from Salesperson;
  401.  
  402. b.) select (convert(varchar(12),Id)+'_'+ upper (Name)) as "ID and Name",Age from Salesperson;
  403. … [NOTE: use || in place of + in mysql or oracle]
  404.  
  405. c.) insert into Salesperson(ID,Name,Age)
  406. values (101,'Saneet',36);
  407.  
  408. d.) Select * from Salesperson
  409. where salary is not null;
  410.  
  411. e.) Select * from Salesperson
  412. where 90000 <= salary and salary <= 150000;
  413.  
  414. 2.) Create view Total_order_amt AS
  415. select top 100 percent Salesperson_id,Sum(Amount) AS Total_Amount
  416. from Orders
  417. group by salesperson_id
  418. having sum(amount) >1500
  419. order by Total_Amount DESC;
  420.  
  421.  
  422. 1)
  423. Table Salesperson
  424.  
  425. ID Name Age Salary
  426. 1 Abe 61 140000
  427. 2 Bob 34 44000
  428. 5 Chris 34 40000
  429. 7 Dan 41 52000
  430. 8 Ken 57 115000
  431. 11 Joe 38 38000
  432.  
  433.  
  434. Table Customer
  435.  
  436. ID Name City Industry Type
  437. 4 Samsonic pleasant J
  438. 6 Panasung oaktown J
  439. 7 Samony jackson B
  440. 9 Orange Jackson B
  441.  
  442. Table Orders
  443. (Note: Date in MM/DD/YYYY format)
  444.  
  445. Order_Number order_date cust_id salesperson_id Amount
  446. 10 8/2/1996 4 2 540
  447. 20 1/30/1999 4 8 1800
  448. 30 7/14/1995 9 1 460
  449. 40 1/29/1998 7 2 2400
  450. 50 2/3/1998 6 7 600
  451. 60 3/2/1998 6 7 720
  452. 70 5/6/1998 9 7 150
  453.  
  454. Create all the above three tables with following constraints:
  455. Create PRIMARY KEYS on each of these three tables
  456. Create FOREIGN KEYS for establishing referential integrity between various tables
  457.  
  458. A. Retrieve information of all sales persons having 3rd character as ‘r’ in their name
  459.  
  460. B. Retrieve information of all customers who reside in ‘Jackson’ city with City in small letters. Comparison should be case insensitive
  461.  
  462. C. Retrieve information about orders for salesperson_ID 7 and cust_ID 6 for amount greater than 650
  463.  
  464. D. Retrieve information about orders received in the year 1998 in ascending order of month
  465.  
  466. E. Find Industry Type wise number of customers
  467.  
  468. 2) Create view to find Sales Person ID wise total order amount having total order amount greater than 1500 in descending order of total order amount
  469.  
  470. ANSWERS:
  471.  
  472. 1.) SQL Queries:
  473. a.) select * from Salesperson
  474. where Name LIKE '__r%';
  475.  
  476. b.) select * from Customer
  477. where lower(city) = 'jackson';
  478.  
  479. c.) select * from Orders
  480. where salesperson_id = 7 and cust_id = 6 and Amount > 650;
  481.  
  482. d.) select * from Orders
  483. where order_date like '%1998'
  484. order by order_date ASC;
  485.  
  486. e.) Select Industry_type, count(ID) AS NoofCustomers from Customer
  487. group by Industry_type;
  488.  
  489.  
  490. 2.) Create view Total_order_amount As
  491. select Top 100 percent Salesperson_id,Sum(Amount) as Total_amount from Orders
  492. group by salesperson_id
  493. having sum(amount)>1500
  494. order by Total_amount DESC;
  495.  
  496.  
  497.  
  498.  
  499. 1)
  500.  
  501. Table Salesperson
  502.  
  503. ID Name Age Salary
  504. 1 Abe 61 140000
  505. 2 Bob 34 44000
  506. 5 Chris 34 40000
  507. 7 Dan 41 52000
  508. 8 Ken 57 115000
  509. 11 Joe 38 38000
  510.  
  511.  
  512. Table Customer
  513.  
  514. ID Name City Industry Type
  515. 4 Samsonic pleasant J
  516. 6 Panasung oaktown J
  517. 7 Samony Jackson B
  518. 9 Orange Jackson B
  519.  
  520. Table Orders
  521. (Note: Date in MM/DD/YYYY format)
  522.  
  523. Order_Number order_date cust_id salesperson_id Amount
  524. 10 8/2/1996 4 2 540
  525. 20 1/30/1999 4 8 1800
  526. 30 7/14/1995 9 1 460
  527. 40 1/29/1998 7 2 2400
  528. 50 2/3/1998 6 7 600
  529. 60 3/2/1998 6 7 720
  530. 70 5/6/1998 9 7 150
  531.  
  532. Create all the above three tables with following constraints:
  533. Create PRIMARY KEYS on each of these three tables
  534. Create FOREIGN KEYS for establishing referential integrity between various tables
  535. A. Retrieve Id, Name (in capital letters) and Age columns from SalesPerson table of all sales person having salary greater than or equal to 50000
  536.  
  537. B. Insert one customer row in Customer table for which Industry Type value is not available
  538.  
  539. C. Retrieve information of all Customers for whom Industry Type value is not available
  540.  
  541. D. Retrieve information of all sales persons having last character as ‘n’ in their name
  542.  
  543. E. Retrieve information of all customers having ID less than 6 and IndustryType not ‘b’;
  544. Note lower ‘b’ (Comparison should be case insensitive)
  545.  
  546. 2) Create view to retrieve information about orders for salesperson_ID 2 or 7 without using OR operator in the ascending order of Salesperson_ID and descending order of amount
  547.  
  548. ANSWERS:
  549.  
  550. 1.) SQL Queries:
  551. a.) select ID,upper(Name)AS Name,Age from Salesperson
  552. where Salary >=50000;
  553.  
  554. b.) insert into customer(ID,Name,City)
  555. values (10,'Naman','Mumbai');
  556.  
  557. c.) Select * from Customer
  558. where Industry_Type is null;
  559.  
  560. d.) Select * from Salesperson
  561. where Name like '%n';
  562.  
  563. e.) Select * from customer
  564. where ID < 6 and industry_type <>'b';
  565.  
  566. 2.) Create view order_by_2or3 As
  567. select Top 100 * from orders
  568. where Salesperson_id IN ((select ID from Salesperson
  569. where ID = 2)union(select ID from Salesperson
  570. where ID = 7))
  571. order by Salesperson_id,Amount DESC;
  572.  
  573.  
  574.  
  575. 1)
  576. Table Salesperson
  577.  
  578. ID Name Age Salary
  579. 1 Abe 61 140000
  580. 2 Bob 34 44000
  581. 5 Chris 34 40000
  582. 7 Dan 41 52000
  583. 8 Ken 57 115000
  584. 11 Joe 38 38000
  585.  
  586.  
  587. Table Customer
  588.  
  589. ID Name City Industry Type
  590. 4 Samsonic pleasant J
  591. 6 Panasung oaktown J
  592. 7 Samony jackson B
  593. 9 Orange Jackson B
  594.  
  595. Table Orders
  596. (Note: Date in MM/DD/YYYY format)
  597.  
  598. Order_Number order_date cust_id salesperson_id Amount
  599. 10 8/2/1996 4 2 540
  600. 20 1/30/1999 4 8 1800
  601. 30 7/14/1995 9 1 460
  602. 40 1/29/1998 7 2 2400
  603. 50 2/3/1998 6 7 600
  604. 60 3/2/1998 6 7 720
  605. 70 5/6/1998 9 7 150
  606.  
  607. Create all the above three tables with following constraints:
  608. Create PRIMARY KEYS on each of these three tables
  609. Create FOREIGN KEYS for establishing referential integrity between various tables
  610.  
  611. A. Retrieve information of all sales persons having salary either 40000 or 44000 or 52000 without using OR operator
  612.  
  613. B. Retrieve information about orders received from year 1995 to 1996 in the ascending order of year
  614.  
  615. C. Find the number of customers having Industry Type NULL / not available
  616.  
  617. D. Find Sales Person ID wise total order amount
  618.  
  619. E. Find Customer ID wise total order amount in descending order of total order amount
  620.  
  621. 2) Create a view to retrieve the information of a sales persons having age less than 40 and earning minimum salary
  622.  
  623. ANSWERS:
  624.  
  625. 1.) SQL Queries:
  626. a.) select * from Salesperson
  627. where ID IN ((select ID from Salesperson
  628. where salary = 40000)union(select ID from Salesperson
  629. where salary = 44000)union(select id from Salesperson
  630. where salary = 52000));
  631.  
  632. b.)
  633. c.) Select * from Customer
  634. where Industry_Type is null;
  635.  
  636. d.) select Salesperson_id,Sum(Amount) as TotalOrderAmount from Orders
  637. group by salesperson_id;
  638.  
  639. e.) select Cust_id,Sum(Amount) as TotalOrderAmount from Orders
  640. group by cust_id
  641. order by TotalOrderAmount DESC;
  642.  
  643.  
  644. 2.) create view min_Salary As
  645. select * from Salesperson
  646. where age < 40 and salary = (select min(salary) from Salesperson);
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653. 1)
  654. Create the following Tables:
  655.  
  656. LOCATION
  657. Location_ID Regional_Group
  658. 122 NEW YORK
  659. 123 DALLAS
  660. 124 CHICAGO
  661. 167 BOSTON
  662.  
  663. DEPARTMENT
  664. Department_ID Name Location_ID
  665. 10 ACCOUNTING 122
  666. 20 RESEARCH 124
  667. 30 SALES 123
  668. 40 OPERATIONS 167
  669.  
  670. JOB
  671. Job_ID Function
  672. 667 CLERK
  673. 668 STAFF
  674. 669 ANALYST
  675.  
  676.  
  677. EMPLOYEE
  678. EMPLOYEE_ID LAST_NAME FIRST_
  679. NAME MIDDLE_NAME JOB_ID MANAGER_ID HIREDATE SALARY COMM DEPARTMENT_ID
  680. 7369 SMITH JOHN Q 667 7902 17-DEC-84 800 NULL 20
  681. 7499 ALLEN KEVIN J 670 7698 20-FEB-85 1600 300 30
  682. 7505 DOYLE JEAN K 671 7839 04-APR-85 2850 NULL 30
  683. 7506 DENNIS LYNN S 671 7839 15-MAY-85 2750 NULL 30
  684. 7507 BAKER LESLIE D 671 7839 10-JUN-85 2200 NULL 40
  685.  
  686. 7521 WARK CYNTHIA D 670 7698 22-FEB-85 1250 500 30
  687.  
  688. Create all the above three tables with following constraints:
  689. Create PRIMARY KEYS on each of these three tables
  690. Create FOREIGN KEYS for nestablishing referential integrity between various tables
  691.  
  692. A. Modify LOCATION table structure to change regional _group column width to 15.
  693.  
  694. B. List out employee_id, last name, first name and annual salary for all the employees
  695.  
  696. C. List out the employees whose name length is 4 and start with “S”
  697.  
  698. D. List out the employee details according to their last_name in ascending order and salaries in descending order
  699.  
  700. E. List out the distinct jobs in Sales and Accounting Departments
  701.  
  702. 2) Create View to display details of all the employees’ with their designations (jobs)
  703.  
  704.  
  705. ANSWERS:
  706.  
  707. 1.) SQL Queries:
  708.  
  709. a.)
  710.  
  711. b.) select eid,last_name,first_name,Salary*12 As AnnualSalary from Employee;
  712.  
  713. c.) select * from Employee
  714. where last_name like 'S___' or first_name like 'S___' or middle_name like 'S___';
  715.  
  716. d.) select * from Employee
  717. order by last_name, Salary DESC;
  718.  
  719. e.) select distinct E.Job_ID, D.Name from Employee E, Department D
  720. where D.D_ID = E.D_ID and D.Name = 'Sales' and d.Name = 'Accounting';
  721.  
  722. 2.) create view DesignationOfEmployee AS
  723. select E.*, J.Functioned_As from Employee E, Job J
  724. where E.job_id = J.Job_ID;
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739. 1)
  740. Create the following Tables:
  741.  
  742. LOCATION
  743. Location_ID Regional_Group
  744. 122 NEW YORK
  745. 123 DALLAS
  746. 124 CHICAGO
  747. 167 BOSTON
  748.  
  749. DEPARTMENT
  750. Department_ID Name Location_ID
  751. 10 ACCOUNTING 122
  752. 20 RESEARCH 124
  753. 30 SALES 123
  754. 40 OPERATIONS 167
  755.  
  756. JOB
  757. Job_ID Function
  758. 667 CLERK
  759. 668 STAFF
  760. 669 ANALYST
  761.  
  762.  
  763. EMPLOYEE
  764. EMPLOYEE_ID LAST_NAME FIRST_
  765. NAME MIDDLE_NAME JOB_ID MANAGER_ID HIREDATE SALARY COMM DEPARTMENT_ID
  766. 7369 SMITH JOHN Q 667 7902 17-DEC-84 800 NULL 20
  767. 7499 ALLEN KEVIN J 670 7698 20-FEB-85 1600 300 30
  768. 7505 DOYLE JEAN K 671 7839 04-APR-85 2850 NULL 30
  769. 7506 DENNIS LYNN S 671 7839 15-MAY-85 2750 NULL 30
  770. 7507 BAKER LESLIE D 671 7839 10-JUN-85 2200 NULL 40
  771.  
  772. 7521 WARK CYNTHIA D 670 7698 22-FEB-85 1250 500 30
  773.  
  774. Create all the above three tables with following constraints:
  775. Create PRIMARY KEYS on each of these three tables
  776. Create FOREIGN KEYS for establishing referential integrity between various tables
  777.  
  778. A. Display the employees who are working in Sales department
  779.  
  780. B. Display the employee who got the maximum salary.
  781.  
  782. C. List out the distinct jobs in Sales and Accounting Departments
  783.  
  784. D. Update employee’s salaries, who are working as Clerk on the basis of 10%.
  785.  
  786. E. List out the employees who earn more than every employee in department 30
  787.  
  788. 2) Create a trigger to set Department_ID of Employee table to new department_ID when Department_ID of Department table is modified/updated.
  789.  
  790. ANSWERS:
  791.  
  792. 1.) SQL Queries:
  793. a.) Select E.eid,E.first_name,E.last_name from Employee E,Department D
  794. where E.D_ID = D.D_ID and d.Name = 'Sales';
  795.  
  796. b.) Select E.eid,E.first_name,E.last_name from Employee E
  797. where E.Salary = (Select max(Salary) from Employee);
  798.  
  799. c.) Same as the (e) of the above question.
  800.  
  801. d.) update Employee
  802. set Salary = salary + 0.10
  803. where job_id in (select j.Job_ID from Job j
  804. where j.Functioned_As = 'Clerk');
  805.  
  806. e.) select * from Employee
  807. where salary > (select max(salary) from Employee E
  808. where E.D_ID = 30
  809. group by E.D_ID);
  810.  
  811.  
  812. 2.) create Trigger update_D_ID_trigger
  813. After Update on Department
  814. FOR EACH ROW
  815. when (new.D_id is NOT NULL)
  816. BEGIN
  817. update Employee
  818. set D_id = :new.D_id
  819. where D_id = :old.D_id;
  820. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement