Advertisement
Coolcap5

Untitled

Mar 19th, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.76 KB | None | 0 0
  1. CREATE TABLE PUBLISHER (
  2. NAME VARCHAR2(20) PRIMARY KEY,
  3. PHONE INTEGER,
  4. ADDRESS VARCHAR2(20)
  5. );
  6.  
  7. CREATE TABLE BOOK (
  8. BOOK_ID INTEGER PRIMARY KEY,
  9. TITLE VARCHAR2(20),
  10. PUB_YEAR VARCHAR2(20),
  11. PUBLISHER_NAME REFERENCES PUBLISHER(NAME) ON DELETE CASCADE
  12. );
  13.  
  14. CREATE TABLE BOOK_AUTHORS (
  15. AUTHOR_NAME VARCHAR2(20),
  16. BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,
  17. PRIMARY KEY (BOOK_ID, AUTHOR_NAME)
  18. );
  19.  
  20. CREATE TABLE LIBRARY_BRANCH (
  21. BRANCH_ID INTEGER PRIMARY KEY,
  22. BRANCH_NAME VARCHAR2(50),
  23. ADDRESS VARCHAR2(50)
  24. );
  25.  
  26. CREATE TABLE BOOK_COPIES (
  27. NO_OF_COPIES INTEGER,
  28. BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,
  29. BRANCH_ID INTEGER REFERENCES LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE,
  30. PRIMARY KEY (BOOK_ID, BRANCH_ID)
  31. );
  32.  
  33. CREATE TABLE CARD (
  34. CARD_NO INTEGER PRIMARY KEY
  35. );
  36.  
  37. CREATE TABLE BOOK_LENDING (
  38. DATE_OUT DATE,
  39. DUE_DATE DATE,
  40. BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE CASCADE,
  41. BRANCH_ID INTEGER REFERENCES LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE,
  42. CARD_NO INTEGER REFERENCES CARD(CARD_NO) ON DELETE CASCADE,
  43. PRIMARY KEY (BOOK_ID, BRANCH_ID, CARD_NO)
  44. );
  45.  
  46. INSERT INTO PUBLISHER VALUES ('MCGRAW-HILL', 9989076587, 'BANGALORE');
  47. INSERT INTO PUBLISHER VALUES ('PEARSON', 9889076565, 'NEWDELHI');
  48. INSERT INTO PUBLISHER VALUES ('RANDOM HOUSE', 7455679345, 'HYDERABAD');
  49. INSERT INTO PUBLISHER VALUES ('HACHETTE LIVRE', 8970862340, 'CHENNAI');
  50. INSERT INTO PUBLISHER VALUES ('GRUPO PLANETA', 7756120238, 'BANGALORE');
  51.  
  52. INSERT INTO BOOK VALUES (1, 'DBMS', 'JAN-2017', 'MCGRAW-HILL');
  53. INSERT INTO BOOK VALUES (2, 'ADBMS', 'JUN-2016', 'MCGRAW-HILL');
  54. INSERT INTO BOOK VALUES (3, 'CN', 'SEP-2016', 'PEARSON');
  55. INSERT INTO BOOK VALUES (4, 'CG', 'SEP-2015', 'GRUPO PLANETA');
  56. INSERT INTO BOOK VALUES (5, 'OS', 'MAY-2016', 'PEARSON');
  57.  
  58. INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 1);
  59. INSERT INTO BOOK_AUTHORS VALUES ('NAVATHE', 2);
  60. INSERT INTO BOOK_AUTHORS VALUES ('TANENBAUM', 3);
  61. INSERT INTO BOOK_AUTHORS VALUES ('EDWARD ANGEL', 4);
  62. INSERT INTO BOOK_AUTHORS VALUES ('GALVIN', 5);
  63.  
  64. INSERT INTO LIBRARY_BRANCH VALUES (10, 'RR NAGAR', 'BANGALORE');
  65. INSERT INTO LIBRARY_BRANCH VALUES (11, 'RNSIT', 'BANGALORE');
  66. INSERT INTO LIBRARY_BRANCH VALUES (12, 'RAJAJI NAGAR', 'BANGALORE');
  67. INSERT INTO LIBRARY_BRANCH VALUES (13, 'NITTE', 'MANGALORE');
  68. INSERT INTO LIBRARY_BRANCH VALUES (14, 'MANIPAL', 'UDUPI');
  69.  
  70. INSERT INTO BOOK_COPIES VALUES (10, 1, 10);
  71. INSERT INTO BOOK_COPIES VALUES (5, 1, 11);
  72. INSERT INTO BOOK_COPIES VALUES (2, 2, 12);
  73. INSERT INTO BOOK_COPIES VALUES (5, 2, 13);
  74. INSERT INTO BOOK_COPIES VALUES (7, 3, 14);
  75. INSERT INTO BOOK_COPIES VALUES (1, 5, 10);
  76. INSERT INTO BOOK_COPIES VALUES (3, 4, 11);
  77.  
  78. INSERT INTO CARD VALUES (100);
  79. INSERT INTO CARD VALUES (101);
  80. INSERT INTO CARD VALUES (102);
  81. INSERT INTO CARD VALUES (103);
  82. INSERT INTO CARD VALUES (104);
  83.  
  84. INSERT INTO BOOK_LENDING VALUES ('01-JAN-17', '01-JUN-17', 1, 10, 101);
  85. INSERT INTO BOOK_LENDING VALUES ('11-JAN-17', '11-MAR-17', 3, 14, 101);
  86. INSERT INTO BOOK_LENDING VALUES ('21-FEB-17', '21-APR-17', 2, 13, 101);
  87. INSERT INTO BOOK_LENDING VALUES ('15-MAR-17', '15-JUL-17', 4, 11, 101);
  88. INSERT INTO BOOK_LENDING VALUES ('12-APR-17', '12-MAY-17', 1, 11, 104);
  89.  
  90.  
  91. -- 1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in each branch, etc.
  92. SELECT B.BOOK_ID, B.TITLE, B.PUBLISHER_NAME, A.AUTHOR_NAME,
  93. C.NO_OF_COPIES, L.BRANCH_ID
  94. FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH L
  95. WHERE B.BOOK_ID=A.BOOK_ID
  96. AND B.BOOK_ID=C.BOOK_ID
  97. AND L.BRANCH_ID=C.BRANCH_ID;
  98.  
  99. -- 2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun 2017.
  100. SELECT CARD_NO
  101. FROM BOOK_LENDING
  102. WHERE DATE_OUT BETWEEN '01-JAN-2017' AND '01-JUL-2017'
  103. GROUP BY CARD_NO
  104. HAVING COUNT(*) > 3;
  105.  
  106. -- 3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation operation.
  107. DELETE FROM BOOK
  108. WHERE BOOK_ID = 3;
  109.  
  110. -- 4.Partition the BOOK table based on year of publication. Demonstrate its working with a
  111. simple query.
  112. CREATE VIEW V_PUBLICATION AS
  113. SELECT PUB_YEAR
  114. FROM BOOK;
  115.  
  116. -- 5. Create a view of all books and its number of copies that are currently available in the Library.
  117. CREATE VIEW V_BOOKS AS
  118. SELECT B.BOOK_ID, B.TITLE, C.NO_OF_COPIES
  119. FROM BOOK B
  120. JOIN BOOK_COPIES C ON B.BOOK_ID = C.BOOK_ID;
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. 2ND TEXT --
  128.  
  129.  
  130.  
  131.  
  132.  
  133. CREATE TABLE SALESMAN (
  134. SALESMAN_ID NUMBER(4) PRIMARY KEY,
  135. NAME VARCHAR(20),
  136. CITY VARCHAR(20),
  137. COMMISSION VARCHAR(20)
  138. );
  139.  
  140. CREATE TABLE CUSTOMER (
  141. CUSTOMER_ID NUMBER(4) PRIMARY KEY,
  142. CUST_NAME VARCHAR(20),
  143. CITY VARCHAR(20),
  144. GRADE NUMBER(3),
  145. SALESMAN_ID NUMBER(4) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE SET NULL
  146. );
  147.  
  148. CREATE TABLE ORDERS (
  149. ORD_NO NUMBER(5) PRIMARY KEY,
  150. PURCHASE_AMT NUMBER(10, 2),
  151. ORD_DATE DATE,
  152. CUSTOMER_ID NUMBER(4) REFERENCES CUSTOMER(CUSTOMER_ID) ON DELETE CASCADE,
  153. SALESMAN_ID NUMBER(4) REFERENCES SALESMAN(SALESMAN_ID) ON DELETE CASCADE
  154. );
  155.  
  156. INSERT INTO SALESMAN VALUES (1000, 'JOHN', 'BANGALORE', '25%');
  157. INSERT INTO SALESMAN VALUES (2000, 'RAVI', 'BANGALORE', '20%');
  158. INSERT INTO SALESMAN VALUES (3000, 'KUMAR', 'MYSORE', '15%');
  159. INSERT INTO SALESMAN VALUES (4000, 'SMITH', 'DELHI', '30%');
  160. INSERT INTO SALESMAN VALUES (5000, 'HARSHA', 'HYDERABAD', '15%');
  161.  
  162. INSERT INTO CUSTOMER VALUES (10, 'PREETHI', 'BANGALORE', 100, 1000);
  163. INSERT INTO CUSTOMER VALUES (11, 'VIVEK', 'MANGALORE', 300, 1000);
  164. INSERT INTO CUSTOMER VALUES (12, 'BHASKAR', 'CHENNAI', 400, 2000);
  165. INSERT INTO CUSTOMER VALUES (13, 'CHETHAN', 'BANGALORE', 200, 2000);
  166. INSERT INTO CUSTOMER VALUES (14, 'MAMATHA', 'BANGALORE', 400, 3000);
  167.  
  168. INSERT INTO ORDERS VALUES (50, 5000, '2017-05-04', 10, 1000);
  169. INSERT INTO ORDERS VALUES (51, 450, '2017-01-20', 10, 2000);
  170. INSERT INTO ORDERS VALUES (52, 1000, '2017-02-24', 13, 2000);
  171. INSERT INTO ORDERS VALUES (53, 3500, '2017-04-13', 14, 3000);
  172. INSERT INTO ORDERS VALUES (54, 550, '2017-03-09', 12, 2000);
  173.  
  174.  
  175. 1. Count the customers with grades above Bangalore’s average.
  176. SELECT GRADE, COUNT (DISTINCT CUSTOMER_ID)
  177. FROM CUSTOMER1
  178. GROUP BY GRADE
  179. HAVING GRADE > (SELECT AVG(GRADE)
  180. FROM CUSTOMER1
  181. WHERE CITY='BANGALORE');
  182. 18CSL58:DBMS Lab Manual 2022-23
  183.  
  184. 2. Find the name and numbers of all salesmen who had more than one customer.
  185. SELECT SALESMAN_ID, NAME
  186. FROM SALESMAN A
  187. WHERE 1 < (SELECT COUNT (*)
  188. FROM CUSTOMER1
  189. WHERE SALESMAN_ID=A.SALESMAN_ID);
  190.  
  191. 3. List all salesmen and indicate those who have and don’t have customers in their
  192. cities (Use UNION operation.)
  193. SELECT SALESMAN.SALESMAN_ID, NAME, CUST_NAME, COMMISSION
  194. FROM SALESMAN, CUSTOMER1
  195. WHERE SALESMAN.CITY = CUSTOMER1.CITY
  196. UNION
  197. SELECT SALESMAN_ID, NAME, 'NO MATCH', COMMISSION
  198. FROM SALESMAN
  199. WHERE NOT CITY = ANY
  200. (SELECT CITY
  201. FROM CUSTOMER1)
  202. ORDER BY 2 DESC;
  203.  
  204. 4. Create a view that finds the salesman who has the customer with the highest order
  205. of a day.
  206. CREATE VIEW ELITSALESMANAS
  207. SELECT B.ORD_DATE, A.SALESMAN_ID, A.NAME
  208. FROM SALESMAN A, ORDERS B
  209. 18CSL58:DBMS Lab Manual 2022-23
  210. RNSIT, Bangalore Page 31
  211. WHERE A.SALESMAN_ID = B.SALESMAN_ID
  212. AND B.PURCHASE_AMT=(SELECT MAX (PURCHASE_AMT)
  213. FROM ORDERS C
  214. WHERE C.ORD_DATE = B.ORD_DATE);
  215.  
  216.  
  217. 5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
  218. must also be deleted.
  219. Use ON DELETE CASCADE at the end of foreign key definitions while creating child table
  220. orders and then execute the following:
  221. Use ON DELETE SET NULL at the end of foreign key definitions while creating child table
  222. customers and then executes the following:
  223.  
  224. DELETE FROM SALESMAN
  225. WHERE SALESMAN_ID=1000;
  226.  
  227.  
  228.  
  229.  
  230.  
  231. 3RD TEXT---
  232.  
  233.  
  234.  
  235.  
  236.  
  237. CREATE TABLE ACTOR (
  238. ACT_ID NUMBER (3),
  239. ACT_NAME VARCHAR (20),
  240. ACT_GENDER CHAR (1),
  241. PRIMARY KEY (ACT_ID));
  242.  
  243. CREATE TABLE DIRECTOR (
  244. DIR_ID NUMBER (3),
  245. DIR_NAME VARCHAR (20),
  246. DIR_PHONE NUMBER (10),
  247. PRIMARY KEY (DIR_ID));
  248.  
  249. CREATE TABLE MOVIES (
  250. MOV_ID NUMBER (4),
  251. MOV_TITLE VARCHAR (25),
  252. MOV_YEAR NUMBER (4),
  253. MOV_LANG VARCHAR (12),
  254. DIR_ID NUMBER (3),
  255. PRIMARY KEY (MOV_ID),
  256. FOREIGN KEY (DIR_ID) REFERENCES DIRECTOR (DIR_ID));
  257.  
  258. CREATE TABLE MOVIE_CAST (
  259. ACT_ID NUMBER (3),
  260. MOV_ID NUMBER (4),
  261. ROLE VARCHAR (10),
  262. PRIMARY KEY (ACT_ID, MOV_ID),
  263. FOREIGN KEY (ACT_ID) REFERENCES ACTOR (ACT_ID),
  264. FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID));
  265.  
  266. CREATE TABLE RATING (
  267. MOV_ID NUMBER (4),
  268. REV_STARS VARCHAR (25),
  269. PRIMARY KEY (MOV_ID),
  270. FOREIGN KEY (MOV_ID) REFERENCES MOVIES (MOV_ID));
  271.  
  272.  
  273. INSERT INTO ACTOR VALUES (301, 'ANUSHKA', 'F');
  274. INSERT INTO ACTOR VALUES (302, 'PRABHAS', 'M');
  275. INSERT INTO ACTOR VALUES (303, 'PUNITH', 'M');
  276. INSERT INTO ACTOR VALUES (304, 'JERMY', 'M');
  277.  
  278. INSERT INTO DIRECTOR VALUES (60, 'RAJAMOULI', 8751611001);
  279. INSERT INTO DIRECTOR VALUES (61, 'HITCHCOCK', 7766138911);
  280. INSERT INTO DIRECTOR VALUES (62, 'FARAN', 9986776531);
  281.  
  282. INSERT INTO DIRECTOR VALUES (63, 'STEVEN SPIELBERG', 8989776530);
  283. INSERT INTO MOVIES VALUES (1001, 'BAHUBALI-2', 2017, 'TELAGU', 60);
  284. INSERT INTO MOVIES VALUES (1002, 'BAHUBALI-1', 2015, 'TELAGU', 60);
  285. INSERT INTO MOVIES VALUES (1003, 'AKASH', 2008, 'KANNADA', 61);
  286. INSERT INTO MOVIES VALUES (1004, 'WAR HORSE', 2011, 'ENGLISH', 63);
  287.  
  288. INSERT INTO MOVIE_CAST VALUES (301, 1002, 'HEROINE');
  289. INSERT INTO MOVIE_CAST VALUES (301, 1001, 'HEROINE');
  290. INSERT INTO MOVIE_CAST VALUES (303, 1003, 'HERO');
  291. INSERT INTO MOVIE_CAST VALUES (303, 1002, 'GUEST');
  292. INSERT INTO MOVIE_CAST VALUES (304, 1004, 'HERO');
  293.  
  294. INSERT INTO RATING VALUES (1001, 4);
  295. INSERT INTO RATING VALUES (1002, 2);
  296. INSERT INTO RATING VALUES (1003, 5);
  297. INSERT INTO RATING VALUES (1004, 4);
  298.  
  299.  
  300. 1. List the titles of all movies directed by ‘Hitchcock’.
  301. SELECT MOV_TITLE
  302. FROM MOVIES
  303. WHERE DIR_ID IN (SELECT DIR_ID
  304. FROM DIRECTOR
  305. WHERE DIR_NAME = ‘HITCHCOCK’);
  306.  
  307. 2. Find the movie names where one or more actors acted in two or more movies.
  308. SELECT MOV_TITLE
  309. FROM MOVIES M, MOVIE_CAST MV
  310. WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN (SELECT ACT_ID
  311. FROM MOVIE_CAST GROUP BY ACT_ID
  312. HAVING COUNT (ACT_ID)>1)
  313. GROUP BY MOV_TITLE
  314. HAVING COUNT (*)>1;
  315.  
  316. 3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use
  317. JOIN operation).
  318. SELECT ACT_NAME, MOV_TITLE, MOV_YEAR
  319. FROM ACTOR A
  320. 18CSL58:DBMS Lab Manual 2022-23
  321. RNSIT, Bangalore Page 38
  322. JOIN MOVIE_CAST C
  323. ON A.ACT_ID=C.ACT_ID
  324. JOIN MOVIES M
  325. ON C.MOV_ID=M.MOV_ID
  326. WHERE M.MOV_YEAR NOT BETWEEN 2000 AND 2015;
  327. OR
  328. SELECT A.ACT_NAME, A.ACT_NAME, C.MOV_TITLE, C.MOV_YEAR
  329. FROM ACTOR A, MOVIE_CAST B, MOVIES C
  330. WHERE A.ACT_ID=B.ACT_ID
  331. AND B.MOV_ID=C.MOV_ID
  332. AND C.MOV_YEAR NOT BETWEEN 2000 AND 2015;
  333.  
  334. 4. Find the title of movies and number of stars for each movie that has at least one
  335. rating and find the highest number of stars that movie received. Sort the result by
  336. movie title.
  337. SELECT MOV_TITLE, MAX (REV_STARS)
  338. FROM MOVIES
  339. INNER JOIN RATING USING (MOV_ID)
  340. GROUP BY MOV_TITLE
  341. HAVING MAX (REV_STARS)>0
  342. ORDER BY MOV_TITLE;
  343.  
  344. 5. Update rating of all movies directed by ‘Steven Spielberg’ to 5
  345. KL
  346. UPDATE RATING
  347. SET REV_STARS=5
  348. WHERE MOV_ID IN (SELECT MOV_ID FROM MOVIES
  349. WHERE DIR_ID IN (SELECT DIR_ID
  350. FROM DIRECTOR
  351. WHERE DIR_NAME = ‘STEVEN
  352. SPIELBERG’));
  353.  
  354.  
  355.  
  356.  
  357. 4TH TEXT---
  358.  
  359.  
  360.  
  361. CREATE TABLE STUDENT (
  362. USN VARCHAR (10) PRIMARY KEY,
  363. SNAME VARCHAR (25),
  364. ADDRESS VARCHAR (25),
  365. PHONE NUMBER (10),
  366. GENDER CHAR (1));
  367.  
  368. CREATE TABLE SEMSEC (
  369. SSID VARCHAR (5) PRIMARY KEY,
  370. SEM NUMBER (2),
  371. SEC CHAR (1));
  372.  
  373. CREATE TABLE CLASS (
  374. USN VARCHAR (10),
  375. SSID VARCHAR (5),
  376. PRIMARY KEY (USN, SSID),
  377. FOREIGN KEY (USN) REFERENCES STUDENT (USN),
  378. FOREIGN KEY (SSID) REFERENCES SEMSEC (SSID));
  379.  
  380.  
  381. CREATE TABLE SUBJECT (
  382. SUBCODE VARCHAR (8),
  383. TITLE VARCHAR (20),
  384. SEM NUMBER (2),
  385. CREDITS NUMBER (2),
  386. PRIMARY KEY (SUBCODE));
  387.  
  388. CREATE TABLE IAMARKS (
  389. USN VARCHAR (10),
  390. SUBCODE VARCHAR (8),
  391. SSID VARCHAR (5),
  392. TEST1 NUMBER (2),
  393. TEST2 NUMBER (2),
  394. TEST3 NUMBER (2),
  395. FINALIA NUMBER (2),
  396. PRIMARY KEY (USN, SUBCODE, SSID),
  397. FOREIGN KEY (USN) REFERENCES STUDENT (USN),
  398. FOREIGN KEY (SUBCODE) REFERENCES SUBJECT (SUBCODE),
  399. FOREIGN KEY (SSID) REFERENCES SEMSEC (SSID));
  400.  
  401. INSERT INTO STUDENT VALUES ('1RN13CS020','AKSHAY','BELAGAVI', 8877881122,'M');
  402. INSERT INTO STUDENT VALUES ('1RN13CS062','SANDHYA','BENGALURU', 7722829912,'F');
  403. INSERT INTO STUDENT VALUES ('1RN13CS091','TEESHA','BENGALURU', 7712312312,'F');
  404. INSERT INTO STUDENT VALUES ('1RN13CS066','SUPRIYA','MANGALURU', 8877881122,'F');
  405. INSERT INTO STUDENT VALUES ('1RN14CS010','ABHAY','BENGALURU', 9900211201,'M');
  406. INSERT INTO STUDENT VALUES ('1RN14CS032','BHASKAR','BENGALURU', 9923211099,'M');
  407. INSERT INTO STUDENT VALUES ('1RN14CS025','ASMI','BENGALURU', 7894737377,'F');
  408. INSERT INTO STUDENT VALUES ('1RN15CS011','AJAY','TUMKUR', 9845091341,'M');
  409. INSERT INTO STUDENT VALUES ('1RN15CS029','CHITRA','DAVANGERE', 7696772121,'F');
  410. INSERT INTO STUDENT VALUES ('1RN15CS045','JEEVA','BELLARY', 9944850121,'M');
  411. INSERT INTO STUDENT VALUES ('1RN15CS091','SANTOSH','MANGALURU', 8812332201,'M');
  412. INSERT INTO STUDENT VALUES ('1RN16CS045','ISMAIL','KALBURGI', 9900232201,'M');
  413. INSERT INTO STUDENT VALUES ('1RN16CS088','SAMEERA','SHIMOGA', 9905542212,'F');
  414. INSERT INTO STUDENT VALUES ('1RN16CS122','VINAYAKA','CHIKAMAGALUR', 8800880011,'M');
  415.  
  416. INSERT INTO SEMSEC VALUES ('CSE8A', 8,'A');
  417. INSERT INTO SEMSEC VALUES ('CSE8B', 8,'B');
  418. INSERT INTO SEMSEC VALUES ('CSE8C', 8,'C');
  419. INSERT INTO SEMSEC VALUES ('CSE7A', 7,'A');
  420. INSERT INTO SEMSEC VALUES ('CSE7B', 7,'B');
  421. INSERT INTO SEMSEC VALUES ('CSE7C', 7,'C');
  422. INSERT INTO SEMSEC VALUES ('CSE6A', 6,'A');
  423. INSERT INTO SEMSEC VALUES ('CSE6B', 6,'B');
  424. INSERT INTO SEMSEC VALUES ('CSE6C', 6,'C');
  425. INSERT INTO SEMSEC VALUES ('CSE5A', 5,'A');
  426. INSERT INTO SEMSEC VALUES ('CSE5B', 5,'B');
  427. INSERT INTO SEMSEC VALUES ('CSE5C', 5,'C');
  428. INSERT INTO SEMSEC VALUES ('CSE4A', 4,'A');
  429. INSERT INTO SEMSEC VALUES ('CSE4B', 4,'B');
  430. INSERT INTO SEMSEC VALUES ('CSE4C', 4,'C');
  431. INSERT INTO SEMSEC VALUES ('CSE3A', 3,'A');
  432. INSERT INTO SEMSEC VALUES ('CSE3B', 3,'B');
  433. INSERT INTO SEMSEC VALUES ('CSE3C', 3,'C');
  434. INSERT INTO SEMSEC VALUES ('CSE2A', 2,'A');
  435. INSERT INTO SEMSEC VALUES ('CSE2B', 2,'B');
  436. INSERT INTO SEMSEC VALUES ('CSE2C', 2,'C');
  437. INSERT INTO SEMSEC VALUES ('CSE1A', 1,'A');
  438. INSERT INTO SEMSEC VALUES ('CSE1B', 1,'B');
  439. INSERT INTO SEMSEC VALUES ('CSE1C', 1,'C');
  440.  
  441. INSERT INTO CLASS VALUES ('1RN13CS020','CSE8A');
  442. INSERT INTO CLASS VALUES ('1RN13CS062','CSE8A');
  443. INSERT INTO CLASS VALUES ('1RN13CS066','CSE8B');
  444. INSERT INTO CLASS VALUES ('1RN13CS091','CSE8C');
  445. INSERT INTO CLASS VALUES ('1RN14CS010','CSE7A');
  446. INSERT INTO CLASS VALUES ('1RN14CS025','CSE7A');
  447. INSERT INTO CLASS VALUES ('1RN14CS032','CSE7A');
  448. INSERT INTO CLASS VALUES ('1RN15CS011','CSE4A');
  449. INSERT INTO CLASS VALUES ('1RN15CS029','CSE4A');
  450. INSERT INTO CLASS VALUES ('1RN15CS045','CSE4B');
  451. INSERT INTO CLASS VALUES ('1RN15CS091','CSE4C');
  452. INSERT INTO CLASS VALUES ('1RN16CS045','CSE3A');
  453. INSERT INTO CLASS VALUES ('1RN16CS088','CSE3B');
  454. INSERT INTO CLASS VALUES ('1RN16CS122','CSE3C');
  455.  
  456. INSERT INTO SUBJECT VALUES ('10CS81','ACA', 8, 4);
  457. INSERT INTO SUBJECT VALUES ('10CS82','SSM', 8, 4);
  458. INSERT INTO SUBJECT VALUES ('10CS83','NM', 8, 4);
  459. INSERT INTO SUBJECT VALUES ('10CS84','CC', 8, 4);
  460. INSERT INTO SUBJECT VALUES ('10CS85','PW', 8, 4);
  461. INSERT INTO SUBJECT VALUES ('10CS71','OOAD', 7, 4);
  462. INSERT INTO SUBJECT VALUES ('10CS72','ECS
  463.  
  464.  
  465. 1.List all the student details studying in fourth semester ‘C’ section.
  466. SELECT S.*
  467. FROM STUDENT S, SEMSEC SS, CLASS C
  468. WHERE S.USN = C.USN AND
  469. SS.SSID = C.SSID AND
  470. SS.SEM = 4 AND
  471. SS.SEC = 'C';
  472.  
  473. 2. Compute the total number of male and female students in each semester and in each
  474. section.
  475. SELECT SS.SEM, SS.SEC, S.GENDER, COUNT(S.GENDER) AS COUNT
  476. FROM STUDENT S, SEMSEC SS, CLASS C
  477. WHERE S.USN = C.USN AND
  478. SS.SSID = C.SSID
  479. GROUP BY SS.SEM, SS.SEC, S.GENDER
  480. ORDER BY SS.SEM;
  481.  
  482.  
  483. 3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.
  484. CREATE VIEW STU_TEST1_MARKS_VIEW AS
  485. SELECT TEST1, SUBCODE
  486. FROM IAMARKS
  487. WHERE USN = '1RN13CS091';
  488.  
  489.  
  490. 4. Calculate the FinalIA (average of best two test marks) and update the corresponding
  491. table for all students.
  492.  
  493. -- Procedure definition
  494. CREATE OR REPLACE PROCEDURE AVGMARKS IS
  495. CURSOR C_IAMARKS IS
  496. SELECT GREATEST(TEST1, TEST2) AS A, GREATEST(TEST1, TEST3) AS B,
  497. GREATEST(TEST3, TEST2) AS C
  498. FROM IAMARKS
  499. WHERE FINALIA IS NULL
  500. FOR UPDATE;
  501. C_A NUMBER;
  502. C_B NUMBER;
  503. C_C NUMBER;
  504. C_SM NUMBER;
  505. C_AV NUMBER;
  506. BEGIN
  507. OPEN C_IAMARKS;
  508. LOOP
  509. FETCH C_IAMARKS INTO C_A, C_B, C_C;
  510. EXIT WHEN C_IAMARKS%NOTFOUND;
  511. IF (C_A != C_B) THEN
  512. C_SM := C_A + C_B;
  513. ELSE
  514. C_SM := C_A + C_C;
  515. END IF;
  516. C_AV := C_SM / 2;
  517. UPDATE IAMARKS SET FINALIA = C_AV WHERE CURRENT OF C_IAMARKS;
  518. END LOOP;
  519. CLOSE C_IAMARKS;
  520. END;
  521. /
  522.  
  523.  
  524. SELECT * FROM IAMARKS;
  525.  
  526. Below SQL code is to invoke the PL/SQL stored procedure from the command line:
  527.  
  528. BEGIN
  529. AVGMARKS; --IT INVOKES THE STORED PROCEDURE
  530. END;
  531. /
  532.  
  533.  
  534. 5. Categorize students based on the following criterion:
  535. If FinalIA = 17 to 20 then CAT = ‘Outstanding’
  536. If FinalIA = 12 to 16 then CAT = ‘Average’
  537. If FinalIA< 12 then CAT = ‘Weak’
  538. Give these details only for 8th semester A, B, and C section students.
  539.  
  540. SELECT S.USN, S.SNAME, S.ADDRESS, S.PHONE, S.GENDER,
  541. (CASE
  542. WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
  543. WHEN IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
  544. ELSE 'WEAK'
  545. END) AS CAT
  546. FROM STUDENT S, SEMSEC SS, IAMARKS IA, SUBJECT SUB
  547. WHERE S.USN = IA.USN AND
  548. SS.SSID = IA.SSID AND
  549. SUB.SUBCODE = IA.SUBCODE AND
  550. SUB.SEM = 8;
  551.  
  552.  
  553.  
  554.  
  555. 5TH TEXT---
  556.  
  557.  
  558.  
  559. CREATE TABLE DEPARTMENT
  560. (DNO VARCHAR2 (20) PRIMARY KEY,
  561. DNAME VARCHAR2 (20),
  562. MGRSTARTDATE DATE);
  563. CREATE TABLE EMPLOYEE
  564. (SSN VARCHAR2 (20) PRIMARY KEY,
  565. FNAME VARCHAR2 (20),
  566. LNAME VARCHAR2 (20),
  567. ADDRESS VARCHAR2 (20),
  568. SEX CHAR (1),
  569. SALARY INTEGER,
  570. SUPERSSN REFERENCES EMPLOYEE (SSN),
  571. DNO REFERENCES DEPARTMENT (DNO));
  572. ALTER TABLE DEPARTMENT
  573. ADD MGRSSN REFERENCES EMPLOYEE (SSN);
  574. CREATE TABLE DLOCATION
  575. (DLOC VARCHAR2 (20),
  576. DNO REFERENCES DEPARTMENT (DNO),
  577. PRIMARY KEY (DNO, DLOC));
  578. CREATE TABLE PROJECT
  579. (PNO INTEGER PRIMARY KEY,
  580. PNAME VARCHAR2 (20),
  581. PLOCATION VARCHAR2 (20),
  582. DNO REFERENCES DEPARTMENT (DNO));
  583. CREATE TABLE WORKS_ON
  584. (HOURS NUMBER (2),
  585. SSN REFERENCES EMPLOYEE (SSN),
  586. PNO REFERENCES PROJECT(PNO),
  587. PRIMARY KEY (SSN, PNO));
  588. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  589. (‘RNSECE01’,’JOHN’,’SCOTT’,’BANGALORE’,’M’, 450000);
  590. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  591. (‘RNSCSE01’,’JAMES’,’SMITH’,’BANGALORE’,’M’, 500000);
  592. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  593. (‘RNSCSE02’,’HEARN’,’BAKER’,’BANGALORE’,’M’, 700000);
  594. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  595. (‘RNSCSE03’,’EDWARD’,’SCOTT’,’MYSORE’,’M’, 500000);
  596. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  597. (‘RNSCSE04’,’PAVAN’,’HEGDE’,’MANGALORE’,’M’, 650000);
  598. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  599. (‘RNSCSE05’,’GIRISH’,’MALYA’,’MYSORE’,’M’, 450000);
  600. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  601. (‘RNSCSE06’,’NEHA’,’SN’,’BANGALORE’,’F’, 800000);
  602. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  603. (‘RNSACC01’,’AHANA’,’K’,’MANGALORE’,’F’, 350000);
  604. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  605. (‘RNSACC02’,’SANTHOSH’,’KUMAR’,’MANGALORE’,’M’, 300000);
  606. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  607. (‘RNSISE01’,’VEENA’,’M’,’MYSORE’,’M’, 600000);
  608. INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
  609. (‘RNSIT01’,’NAGESH’,’HR’,’BANGALORE’,’M’, 500000);
  610. INSERT INTO DEPARTMENT VALUES (‘1’,’ACCOUNTS’,’01-JAN-01’,’RNSACC02’);
  611. INSERT INTO DEPARTMENT VALUES (‘2’,’IT’,’01-AUG-16’,’RNSIT01’);
  612. INSERT INTO DEPARTMENT VALUES (‘3’,’ECE’,’01-JUN-08’,’RNSECE01’);
  613. INSERT INTO DEPARTMENT VALUES (‘4’,’ISE’,’01-AUG-15’,’RNSISE01’);
  614. INSERT INTO DEPARTMENT VALUES (‘5’,’CSE’,’01-JUN-02’,’RNSCSE05’);
  615. UPDATE EMPLOYEE SET
  616. SUPERSSN=NULL, DNO=’3’
  617. WHERE SSN=’RNSECE01’;
  618. UPDATE EMPLOYEE SET
  619. SUPERSSN=’RNSCSE02’, DNO=’5’
  620. WHERE SSN=’RNSCSE01’;
  621. UPDATE EMPLOYEE SET
  622. SUPERSSN=’RNSCSE03’, DNO=’5’
  623. WHERE SSN=’RNSCSE02’;
  624. UPDATE EMPLOYEE SET
  625. SUPERSSN=’RNSCSE04’, DNO=’5’
  626. WHERE SSN=’RNSCSE03’;
  627. UPDATE EMPLOYEE SET
  628. DNO=’5’, SUPERSSN=’RNSCSE05’
  629. WHERE SSN=’RNSCSE04’;
  630. UPDATE EMPLOYEE SET
  631. DNO=’5’, SUPERSSN=’RNSCSE06’
  632. WHERE SSN=’RNSCSE05’;
  633. UPDATE EMPLOYEE SET
  634. DNO=’5’, SUPERSSN=NULL
  635. WHERE SSN=’RNSCSE06’;
  636. UPDATE EMPLOYEE SET
  637. DNO=’1’, SUPERSSN=’RNSACC02’
  638. WHERE SSN=’RNSACC01’;
  639. UPDATE EMPLOYEE SET
  640. DNO=’1’, SUPERSSN=NULL
  641. WHERE SSN=’RNSACC02’;
  642. UPDATE EMPLOYEE SET
  643. DNO=’4’, SUPERSSN=NULL
  644. WHERE SSN=’RNSISE01’;
  645. UPDATE EMPLOYEE SET
  646. DNO=’2’, SUPERSSN=NULL
  647. WHERE SSN=’RNSIT01’;
  648. INSERT INTO DLOCATION VALUES (’BANGALORE’, ‘1’);
  649. INSERT INTO DLOCATION VALUES (’BANGALORE’, ‘2’);
  650. INSERT INTO DLOCATION VALUES (’BANGALORE’, ‘3’);
  651. INSERT INTO DLOCATION VALUES (’MANGALORE’, ‘4’);
  652. INSERT INTO DLOCATION VALUES (’MANGALORE’, ‘5’);
  653. INSERT INTO PROJECT VALUES (100,’IOT’,’BANGALORE’,’5’);
  654. INSERT INTO PROJECT VALUES (101,’CLOUD’,’BANGALORE’,’5’);
  655. INSERT INTO PROJECT VALUES (102,’BIGDATA’,’BANGALORE’,’5’);
  656. INSERT INTO PROJECT VALUES (103,’SENSORS’,’BANGALORE’,’3’);
  657. INSERT INTO PROJECT VALUES (104,’BANK MANAGEMENT’,’BANGALORE’,’1’);
  658. INSERT INTO PROJECT VALUES (105,’SALARY MANAGEMENT’,’BANGALORE’,’1’);
  659. INSERT INTO PROJECT VALUES (106,’OPENSTACK’,’BANGALORE’,’4’);
  660. INSERT INTO PROJECT VALUES (107,’SMART CITY’,’BANGALORE’,’2’);
  661. INSERT INTO WORKS_ON VALUES (4, ‘RNSCSE01’, 100);
  662. INSERT INTO WORKS_ON VALUES (6, ‘RNSCSE01’, 101);
  663. INSERT INTO WORKS_ON VALUES (8, ‘RNSCSE01’, 102);
  664. INSERT INTO WORKS_ON VALUES (10, ‘RNSCSE02’, 100);
  665. INSERT INTO WORKS_ON VALUES (3, ‘RNSCSE04’, 100);
  666. INSERT INTO WORKS_ON VALUES (4, ‘RNSCSE05’, 101);
  667. INSERT INTO WORKS_ON VALUES (5, ‘RNSCSE06’, 102);
  668. INSERT INTO WORKS_ON VALUES (6, ‘RNSCSE03’, 102);
  669. INSERT INTO WORKS_ON VALUES (7, ‘RNSECE01’, 103);
  670. INSERT INTO WORKS_ON VALUES (5, ‘RNSACC01’, 104);
  671. INSERT INTO WORKS_ON VALUES (6, ‘RNSACC02’, 105);
  672. INSERT INTO WORKS_ON VALUES (4, ‘RNSISE01’, 106);
  673. INSERT INTO WORKS_ON VALUES (10, ‘RNSIT01’, 107);
  674. (SELECT DISTINCT P.PNO
  675. FROM PROJECT P, DEPARTMENT D, EMPLOYEE E
  676. WHERE E.DNO=D.DNO
  677. AND D.MGRSSN=E.SSN
  678. AND E.LNAME=’SCOTT’)
  679. UNION
  680. (SELECT DISTINCT P1.PNO
  681. FROM PROJECT P1, WORKS_ON W, EMPLOYEE E1
  682. WHERE P1.PNO=W.PNO
  683. AND E1.SSN=W.SSN
  684. AND E1.LNAME=’SCOTT’);
  685. SELECT E.FNAME, E.LNAME, 1.1*E.SALARY AS INCR_SAL
  686. FROM EMPLOYEE E, WORKS_ON W, PROJECT P
  687. WHERE E.SSN=W.SSN
  688. AND W.PNO=P.PNO
  689. AND P.PNAME=’IOT’;
  690. SELECT SUM (E.SALARY), MAX (E.SALARY), MIN (E.SALARY), AVG
  691. (E.SALARY)
  692. FROM EMPLOYEE E, DEPARTMENT D
  693. WHERE E.DNO=D.DNO
  694. AND D.DNAME=’ACCOUNTS’;
  695. SELECT E.FNAME, E.LNAME
  696. FROM EMPLOYEE E
  697. WHERE NOT EXISTS((SELECT PNO
  698. FROM PROJECT
  699. WHERE DNO=’5’)
  700. EXCEPT(SELECT PNO
  701. FROM WORKS_ON
  702. WHERE E.SSN=SSN));
  703. Or
  704. SELECT LNAME, FNAME, DNO
  705. FROM EMPLOYEE
  706. WHERE NOT EXISTS (SELECT *
  707. FROM WORKS_ON B
  708. WHERE (B.PNO IN(SELECT P.PNO
  709. FROM PROJECT P
  710. WHERE P.DNO=’5’)
  711. AND
  712. NOT EXISTS (SELECT *
  713. FROM WORKS_ON C
  714. WHERE C.SSN=SSN
  715. AND C.PNO=B.PNO)));
  716. SELECT D.DNO, COUNT (*)
  717. FROM DEPARTMENT D, EMPLOYEE E
  718. WHERE D.DNO=E.DNO
  719. AND E.SALARY>600000
  720. AND D.DNO IN (SELECT E1.DNO
  721. FROM EMPLOYEE E1
  722. GROUP BY E1.DNO
  723. HAVING COUNT (*)>5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement