Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.30 KB | None | 0 0
  1. use hms;
  2.  
  3. select * from room_allocation;
  4. select * from patient_master;
  5. select * from doctor_master;
  6. select * from room_master;
  7.  
  8.  
  9.  
  10. /* 1: Display the patient_id, patient_name , phone number,
  11. ( if phone number is not there display address) for the people who are staying more then 10 days.*/
  12. select p.pid,name,phoneno from patient_master p,room_allocation r where p.pid=r.pid and (release_date-adm_date)>10;
  13.  
  14.  
  15. /* 2:Display the patient_id, patient_name, phone number( +91-999-999-9999 format) , type_of_ailment .
  16. type of ailement is decided with the number of days stay in hospital.
  17. if stay <=5 ---- Minor
  18. if stay >5 and <=15 days ----Medium
  19. if stay >15 days ---- Major */
  20.  
  21. select pid,name,phoneno
  22. from patient_master;
  23. select pid,sum(datediff(release_date,adm_date))
  24. from room_allocation
  25. group by pid;
  26.  
  27. select p.pid,p.name,concat('+91-', substr(p.phoneno,1,3),'-',substr(p.phoneno,4,3),'-',substr(p.phoneno,7)) as 'Phone No',
  28. case
  29. when t.count<=5 or t.count is null then 'Minor'
  30. when t.count>5 and t.count<=15 then 'Medium'
  31. else 'Major'
  32. end
  33. from patient_master p left outer join ( select pid,sum(datediff(release_date,adm_date)) as 'count'
  34. from room_allocation
  35. group by pid
  36. )t on p.pid=t.pid;
  37.  
  38.  
  39.  
  40.  
  41. select * from patient_master;
  42. /* 9123456789 */
  43. select concat('+91-', substr(phoneno,1,3),'-',substr(phoneno,4,3),'-',substr(phoneno,7))
  44. from patient_master;
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /* 3:Display the doctor_id and doctor name who is treating maximum patients. */
  51. select distinct d.doctorid,doctorname from doctor_master d,patient_master p where p.doctorid=d.doctorid and d.doctorid=(
  52. select doctorid from patient_master group by doctorid having count(*)=(
  53. select max(c) from(
  54. select count(*)c from patient_master group by doctorid
  55. )a
  56. )
  57. );
  58.  
  59.  
  60.  
  61. /* 4:Display the patients who were admitted in the month of january.*/
  62. select r.pid,p.name from room_allocation r,patient_master p where p.pid=r.pid and adm_date like '%-12-%';
  63.  
  64.  
  65. /* 5:Display the patient_id and patient_name who paid more then once.*/
  66. select distinct p.pid,p.name from patient_master p,bill_payment b where p.pid=b.pid and b.pid=(
  67. select pid from bill_payment group by pid having count(*)>1);
  68.  
  69.  
  70. /* 6:Display the doctor_id, doctor_name and count of patients.
  71. Display the data in descending order of the cont_of_patients. */
  72. select d.doctorid,d.doctorname,count(*) Count_no_patients
  73. from patient_master p,doctor_master d where p.doctorid=d.doctorid group by p.doctorid asc;
  74.  
  75.  
  76. /* 7: Display the room_no, room_type which are allocated more then once.*/
  77. select room_no,room_type from room_master where room_no=
  78. (select room_no from room_allocation group by room_no having count(*)>1);
  79.  
  80.  
  81. /*8:Display the room_no, room_type which are allocated more then once to the same patient. */
  82.  
  83. insert into room_allocation values('R0003','P0005','12-6-22','12-6-24');
  84. select * from room_allocation;
  85. select * from room_master;
  86.  
  87.  
  88. select distinct a.room_no,m.room_type
  89. from room_allocation a,room_master m
  90. where a.room_no in
  91. (select room_no
  92. from room_allocation
  93. group by pid
  94. having count(*)>1) and a.room_no=m.room_no;
  95.  
  96.  
  97.  
  98. /* 9: display the patient_id,patient_name, doctor_id, doctor_name, room_id, room_type, adm_date, bill_id, amount .
  99. Amount should be rounded of.*/
  100. select distinct p.pid,p.name,d.doctorid,d.doctorname,r.room_no,r.room_type,a.adm_date,b.billid,ROUND(amount,0) as Amount
  101. from patient_master p,doctor_master d,room_master r,room_allocation a,bill_payment b
  102. where b.pid=p.pid and d.doctorid=p.doctorid and p.pid=a.pid and r.room_no=a.room_no;
  103.  
  104.  
  105. /* 10: Display the patient_id, patient_name, billid, amount.
  106. Amount should be rounded of to single place of decimal.*/
  107. select p.pid,name,billid,ROUND(amount,1)
  108. from patient_master p,bill_payment b
  109. where p.pid=b.pid;
  110.  
  111. /* 11: Display the room_no which was never allocated to any patient.*/
  112. select room_no from room_master where room_no not in(
  113. select r.room_no from room_allocation r,patient_master m where r.pid=m.pid group by room_no);
  114.  
  115. /* 12: Display the the doctors_id who never treated any patients.*/
  116. select doctorid from doctor_master where doctorid not in(
  117. select p.doctorid from patient_master p,doctor_master d where d.doctorid=p.doctorid group by pid);
  118.  
  119. /* 13:The depatment which are having the maximum number of doctors.*/
  120. select dept from doctor_master group by dept having count(*)=(
  121. select max(c) from(
  122. select count(*)c from doctor_master group by dept)a
  123. );
  124.  
  125.  
  126. /* 14: Count the number of male and female patients.*/
  127. select gender,count(*) as count from patient_master group by gender;
  128. select count(*) as F from patient_master group by gender having gender="F";
  129. select count(*) as F from patient_master group by gender having gender="M";
  130.  
  131. /* 15:Count the %age of male and female MALE 20% FEMALE 80%*/
  132. select * from patient_master;
  133. select gender,round((sum(age)/s.sm)*100) as '%age'
  134. from patient_master,(select sum(age) as sm from patient_master)s
  135. group by gender;
  136.  
  137.  
  138. select gender,concat('%',cast(round((sum(age)/t.sm)*100) as char))
  139. from patient_master,( select sum(age) as sm
  140. from patient_master
  141. )t
  142. group by gender;
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. use lms;
  152. Create table LMS_MEMBERS
  153. (
  154. MEMBER_ID Varchar(10),
  155. MEMBER_NAME Varchar(30) NOT NULL,
  156. CITY Varchar(20),
  157. DATE_REGISTER Date NOT NULL,
  158. DATE_EXPIRE Date ,
  159. MEMBERSHIP_STATUS Varchar(15)NOT NULL,
  160. Constraint LMS_cts1 PRIMARY KEY(MEMBER_ID)
  161. );
  162. Create table LMS_SUPPLIERS_DETAILS
  163. (
  164. SUPPLIER_ID Varchar(3),
  165. SUPPLIER_NAME Varchar(30) NOT NULL,
  166. ADDRESS Varchar(50),
  167. CONTACT bigint(10) NOT NULL,
  168. EMAIL Varchar(15) NOT NULL,
  169. Constraint LMS_cts2 PRIMARY KEY(SUPPLIER_ID)
  170. );
  171. Create table LMS_FINE_DETAILS
  172. (
  173. FINE_RANGE Varchar(3),
  174. FINE_AMOUNT decimal(10,2) NOT NULL,
  175. Constraint LMS_cts3 PRIMARY KEY(FINE_RANGE)
  176. );
  177. Create table LMS_BOOK_DETAILS
  178. (
  179. BOOK_CODE Varchar(10),
  180. BOOK_TITLE Varchar(50) NOT NULL,
  181. CATEGORY Varchar(15) NOT NULL,
  182. AUTHOR Varchar(30) NOT NULL,
  183. PUBLICATION Varchar(30),
  184. PUBLISH_DATE Date,
  185. BOOK_EDITION int(2),
  186. PRICE decimal(8,2) NOT NULL,
  187. RACK_NUM Varchar(3),
  188. DATE_ARRIVAL Date NOT NULL,
  189. SUPPLIER_ID Varchar(3) NOT NULL,
  190. Constraint LMS_cts4 PRIMARY KEY(BOOK_CODE),
  191. Constraint LMS_cts41 FOREIGN KEY(SUPPLIER_ID) References LMS_SUPPLIERS_DETAILS(SUPPLIER_ID)
  192. );
  193. Create table LMS_BOOK_ISSUE
  194. (
  195. BOOK_ISSUE_NO int,
  196. MEMBER_ID Varchar(10) NOT NULL,
  197. BOOK_CODE Varchar(10) NOT NULL,
  198. DATE_ISSUE Date NOT NULL,
  199. DATE_RETURN Date NOT NULL,
  200. DATE_RETURNED Date NULL,
  201. FINE_RANGE Varchar(3),
  202. Constraint LMS_cts5 PRIMARY KEY(BOOK_ISSUE_NO),
  203. Constraint LMS_Mem FOREIGN KEY(MEMBER_ID) References LMS_MEMBERS(MEMBER_ID),
  204. Constraint LMS_BookDetail FOREIGN KEY(BOOK_CODE) References LMS_BOOK_DETAILS(BOOK_CODE),
  205. Constraint LMS_FineDetail FOREIGN KEY(FINE_RANGE) References LMS_FINE_DETAILS(FINE_RANGE)
  206. );
  207. Insert into LMS_MEMBERS
  208. Values('LM001', 'AMIT', 'CHENNAI', '2012-02-12', '2013-02-11','Temporary');
  209. Insert into LMS_MEMBERS
  210. Values('LM002', 'ABDHUL', 'DELHI', '2012-04-10', '2013-04-09','Temporary');
  211. Insert into LMS_MEMBERS
  212. Values('LM003', 'GAYAN', 'CHENNAI', '2012-05-13','2013-05-12', 'Permanent');
  213. Insert into LMS_MEMBERS
  214. Values('LM004', 'RADHA', 'CHENNAI', '2012-04-22', '2013-04-21', 'Temporary');
  215. Insert into LMS_MEMBERS
  216. Values('LM005', 'GURU', 'BANGALORE', '2012-03-30', '2013-05-16','Temporary');
  217. Insert into LMS_MEMBERS
  218. Values('LM006', 'MOHAN', 'CHENNAI', '2012-04-12', '2013-05-16','Temporary');
  219.  
  220.  
  221. Insert into LMS_SUPPLIERS_DETAILS
  222. Values ('S01','SINGAPORE SHOPPEE', 'CHENNAI', 9894123555,'sing@gmail.com');
  223. Insert into LMS_SUPPLIERS_DETAILS
  224. Values ('S02','JK Stores', 'MUMBAI', 9940123450 ,'jks@yahoo.com');
  225. Insert into LMS_SUPPLIERS_DETAILS
  226. Values ('S03','ROSE BOOK STORE', 'TRIVANDRUM', 9444411222,'rose@gmail.com');
  227. Insert into LMS_SUPPLIERS_DETAILS
  228. Values ('S04','KAVARI STORE', 'DELHI', 8630001452,'kavi@redif.com');
  229. Insert into LMS_SUPPLIERS_DETAILS
  230. Values ('S05','EINSTEN BOOK GALLARY', 'US', 9542000001,'eingal@aol.com');
  231. Insert into LMS_SUPPLIERS_DETAILS
  232. Values ('S06','AKBAR STORE', 'MUMBAI',7855623100 ,'akbakst@aol.com');
  233.  
  234. Insert into LMS_FINE_DETAILS Values('R0', 0);
  235. Insert into LMS_FINE_DETAILS Values('R1', 20);
  236. insert into LMS_FINE_DETAILS Values('R2', 50);
  237. Insert into LMS_FINE_DETAILS Values('R3', 75);
  238. Insert into LMS_FINE_DETAILS Values('R4', 100);
  239. Insert into LMS_FINE_DETAILS Values('R5', 150);
  240. Insert into LMS_FINE_DETAILS Values('R6', 200);
  241.  
  242. Insert into LMS_BOOK_DETAILS
  243. Values('BL000001', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice Hall', '1999-12-10', 6, 600.00, 'A1', '2011-05-10', 'S01');
  244. Insert into LMS_BOOK_DETAILS
  245. Values('BL000002', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt', 'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2011-05-10', 'S03');
  246. Insert into LMS_BOOK_DETAILS
  247. Values('BL000003', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice Hall', '1999-05-10', 6, 600.00, 'A1', '2012-05-10', 'S01');
  248. Insert into LMS_BOOK_DETAILS
  249. Values('BL000004', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt', 'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2012-05-11', 'S01');
  250. Insert into LMS_BOOK_DETAILS
  251. Values('BL000005', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice Hall', '1999-12-10', 6, 600.00, 'A1', '2012-05-11', 'S01');
  252. Insert into LMS_BOOK_DETAILS
  253. Values('BL000006', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt', 'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2012-05-12', 'S03');
  254. Insert into LMS_BOOK_DETAILS
  255. Values('BL000007', 'Let Us C', 'C', 'Yashavant Kanetkar ', 'BPB Publications', '2010-12-11', 9, 500.00 , 'A3', '2010-11-03', 'S03');
  256. Insert into LMS_BOOK_DETAILS
  257. Values('BL000008', 'Let Us C', 'C', 'Yashavant Kanetkar ','BPB Publications', '2010-05-12', 9, 500.00 , 'A3', '2011-08-09', 'S04');
  258.  
  259.  
  260.  
  261. Insert into LMS_BOOK_ISSUE
  262. Values (001, 'LM001', 'BL000001', '2012-05-01', '2012-05-16', '2012-05-16', 'R0');
  263. Insert into LMS_BOOK_ISSUE
  264. Values (002, 'LM002', 'BL000002', '2012-05-01', '2012-05-06','2012-05-16', 'R2');
  265. Insert into LMS_BOOK_ISSUE
  266. Values (003, 'LM003', 'BL000007', '2012-04-01', '2012-04-16', '2012-04-20','R1');
  267. Insert into LMS_BOOK_ISSUE
  268. Values (004, 'LM004', 'BL000005', '2012-04-01', '2012-04-16','2012-04-20', 'R1');
  269. Insert into LMS_BOOK_ISSUE
  270. Values (005, 'LM005', 'BL000008', '2012-03-30', '2012-04-15','2012-04-20' , 'R1');
  271. Insert into LMS_BOOK_ISSUE
  272. Values (006, 'LM005', 'BL000008', '2012-04-20', '2012-05-05','2012-05-05' , 'R0');
  273. Insert into LMS_BOOK_ISSUE
  274. Values (007, 'LM003', 'BL000007', '2012-04-22', '2012-05-07','2012-05-25' , 'R4');
  275.  
  276.  
  277.  
  278. /*-------------------Simple qry with solutions------------------------*/
  279.  
  280. /* Problem # 1:
  281. Write a query to display the member id, member name, city and membership status who are all having life time membership.
  282. Hint: Life time membership status is “Permanent”.*/
  283. select member_id,member_name,city,membership_status from lms_members
  284. where membership_status="permanent";
  285.  
  286.  
  287. /*Problem # 2:
  288. Write a query to display the member id, member name who have not returned the books.
  289. Hint: Book return status is book_issue_status ='Y' or 'N'.*/
  290. select member_id from lms_members where member_id not in
  291. (select member_id from lms_book_issue where date_returned is not null);
  292.  
  293. /*Problem # 3:
  294. Write a query to display the member id, member name who have taken the book with book code 'BL000002'.*/
  295. select b.member_id,m.member_name from lms_members m,lms_book_issue b
  296. where b.member_id=m.member_id and b.book_code="BL000002";
  297.  
  298.  
  299. /*Problem # 4:
  300. Write a query to display the book code, book title and author of the books whose author name begins with 'P'.*/
  301. select book_code,book_title,author from lms_book_details
  302. where author like "p%";
  303.  
  304. /*Problem # 5:
  305. Write a query to display the total number of Java books available in library with alias name ‘NO_OF_BOOKS’.*/
  306. select distinct count(*) as 'NO_OF_BOOKS' from lms_book_details
  307. where category = 'JAVA' group by category;
  308.  
  309. /*Problem # 6:
  310. Write a query to list the category and number of books in each category with alias name ‘NO_OF_BOOKS’.*/
  311. select count(*) as 'NO OF BOOKS',category from lms_book_details
  312. group by category;
  313.  
  314. /*Problem # 7:
  315. Write a query to display the number of books published by "Prentice Hall” with the alias name “NO_OF_BOOKS”.*/
  316. select count(*) as 'NO OF BOOKS' from lms_book_details
  317. where publication='Prentice Hall' group by publication;
  318.  
  319. /* Problem # 8:
  320. Write a query to display the book code, book title of the books which are issued on the date "1st April 2012".*/
  321. select i.book_code,d.book_title from lms_book_details d,lms_book_issue i
  322. where i.book_code=d.book_code and i.date_issue like '2012-04-01';
  323.  
  324. /*Problem # 9:
  325. Write a query to display the member id, member name, date of registration and expiry date of the members
  326. whose membership expiry date is before APR 2013.*/
  327. select member_id from lms_members where date_expire<"2013-04-01";
  328.  
  329.  
  330. /* Problem # 10:
  331. write a query to display the member id, member name, date of registration, membership status of
  332. the members who registered before "March 2012" and membership status is "Temporary"*/
  333. select member_id,member_name,date_register,membership_status
  334. from lms_members where membership_status="temporary" and date_register>"2012-03-01";
  335.  
  336.  
  337. /*Problem #11:
  338. Write a query to display the member id, member name who’s City is CHENNAI or DELHI. Hint:
  339. Display the member name in title case with alias name 'Name'.*/
  340. select member_id,member_name as 'name' from lms_members
  341. where city="chennai" or city="delhi";
  342.  
  343. /*Problem #12:
  344. Write a query to concatenate book title, author and display in the following format.
  345. Book_Title_is_written_by_Author
  346. Example: Let Us C_is_written_by_Yashavant Kanetkar
  347. Hint: display unique books. Use “BOOK_WRITTEN_BY” as alias name.*/
  348. select concat(book_title,'is written by',author) as 'book_written_by'
  349. from lms_book_details;
  350.  
  351. /*Problem #13:
  352. Write a query to display the average price of books which is belonging to ‘JAVA’ category with alias
  353. name “AVERAGEPRICE”.*/
  354. select avg(price) from lms_book_details
  355. where category="java";
  356.  
  357. /*Problem #14:
  358. Write a query to display the supplier id, supplier name and email of the suppliers who are all having gmail account.*/
  359. select supplier_id,supplier_name,email
  360. from lms_suppliers_details
  361. where email like "%@gmail.com";
  362.  
  363. /*Problem#15:
  364. Write a query to display the supplier id, supplier name and contact details.
  365. Contact details can be either phone number or email or address with alias name “CONTACTDETAILS”.
  366. If phone number is null then display email, even if email also null then display the address of the supplier.
  367. Hint: Use Coalesce function.*/
  368. select supplier_id,supplier_name,coalesce(cast(contact as char),cast(email as char),cast(address as char)) as 'Contact'
  369. from lms_suppliers_details;
  370.  
  371. /*Problem#16:
  372. Write a query to display the supplier id, supplier name and contact.
  373. If phone number is null then display ‘No’ else display ‘Yes’ with alias name “PHONENUMAVAILABLE”. Hint: Use NVL2.*/
  374.  
  375. select supplier_id,supplier_name,
  376. case
  377. when contact is null then 'no'
  378. else
  379. 'yes'
  380. end as 'phoneavailable'
  381. from lms_suppliers_details;
  382.  
  383.  
  384.  
  385. /*----------------------Average query-------------------------------------*/
  386. /*Problem # 1:
  387. Write a query to display the member id, member name of the members, book code and book title of the books taken
  388. by them.*/
  389. select m.member_id,m.member_name,d.book_code,d.book_title
  390. from lms_members m,lms_book_issue i,lms_book_details d
  391. where d.book_code=i.book_code and m.member_id=i.member_id;
  392.  
  393. /*Problem # 2:
  394. Write a query to display the total number of books available in the library with alias name “NO_OF_BOOKS_AVAILABLE”
  395. (Which is not issued). Hint: The issued books details are available in the LMS_BOOK_ISSUE table.*/
  396. select count(*) as 'NO_of_books' from lms_book_details where book_code not in
  397. (select book_code from lms_book_issue) group by book_code;
  398.  
  399. /*Problem # 3:
  400. Write a query to display the member id, member name, fine range and fine amount of the members
  401. whose fine amount is less than 100.*/
  402. select m.member_id,m.member_name,f.fine_range,f.fine_amount
  403. from lms_fine_details f,lms_members m,lms_book_issue i
  404. where i.member_id=m.member_id and i.fine_range=f.fine_range
  405. and f.fine_amount<100;
  406.  
  407. /* Problem # 4:
  408. Write a query to display the book code, book title and availability status of the ‘JAVA’ books whose edition is "6”.
  409. Show the availability status with alias name “AVAILABILITYSTATUS”. Hint: Book availability status can be fetched
  410. from “BOOK_ISSUE_STATUS” column of LMS_BOOK_ISSUE table.*/
  411. select book_code,book_title from lms_book_details
  412. where book_edition=6;
  413. -- no status available see from amol solutions--
  414.  
  415. /*Problem # 5:
  416. Write a query to display the book code, book title and rack number of the books which are placed in rack 'A1'
  417. and sort by book title in ascending order.*/
  418. select book_code,book_title,rack_num
  419. from lms_book_details
  420. where rack_num='A1'
  421. order by book_tlms_memberslms_membersitle asc;
  422.  
  423. /*Problem # 6:
  424. Write a query to display the member id, member name, due date and date returned of the members who has returned
  425. the books after the due date. Hint: Date_return is due date and Date_returned is actual book return date.*/
  426. select member_id,date_return,date_returned
  427. from lms_book_issue
  428. where date_returned>date_return;
  429.  
  430. /*Problem # 7:
  431. Write a query to display the member id, member name and date of registration who
  432. have not taken any book.*/
  433.  
  434. select member_id,member_name,date_register
  435. from lms_members where member_id not in
  436. (select member_id from lms_book_issue);
  437.  
  438.  
  439. /*Problem # 8:
  440. Write a Query to display the member id and member name of the members who has not paid any fine
  441. in the year 2012.*/
  442. select b.member_id,m.member_name from lms_book_issue b,lms_fine_details f,lms_members m
  443. where b.fine_range=f.fine_range and b.member_id=m.member_id
  444. and f.fine_range="R0" and b.date_returned like "2012-%-%";
  445.  
  446. /*Problem # 9:
  447. Write a query to display the date on which the maximum numbers of books were issued and
  448. the number of books issued with alias name “NOOFBOOKS”.*/
  449. select distinct date_issue,count(*) as 'NOOFBOOKS' from lms_book_issue
  450. group by date_issue having date_issue in
  451. (select date_issue from lms_book_issue group by date_issue having count(*) in
  452. (select max(c) from
  453. (select count(*)c from lms_book_issue
  454. group by date_issue)a
  455. )
  456. );
  457.  
  458.  
  459. /*Problem # 10:
  460. Write a query to list the book title and supplier id for the books authored by “Herbert Schildt"
  461. and the book edition is 5 and supplied by supplier ‘S01’.*/
  462. select d.book_title,s.supplier_id from lms_book_details d,lms_suppliers_details s
  463. where d.supplier_id=s.supplier_id and d.author="herbert Schildt"
  464. and d.book_edition='5' and s.supplier_id="S01";
  465.  
  466. /*Problem # 11:
  467. Write a query to display the rack number and the number of books in each rack with alias
  468. name “NOOFBOOKS” and sort by rack number in ascending order.*/
  469. select rack_num,count(*) as 'noofbooks' from lms_book_details group by rack_num asc;
  470.  
  471. /*Problem # 12:
  472. Write a query to display book issue number, member name, date or registration, date of expiry,
  473. book title, category author, price, date of issue, date of return, actual returned date, issue status,
  474. fine amount.*/
  475. select s.book_issue_no,m.member_name,m.date_register,m.date_expire,d.book_title,d.category,d.author
  476. ,d.price,s.date_issue,s.date_return,s.date_returned,f.fine_amount
  477. from lms_book_details d,lms_members m,lms_fine_details f,lms_book_issue s
  478. where d.book_code=s.book_code and s.member_id=m.member_id
  479. and f.fine_range=s.fine_range;
  480.  
  481. /*Problem # 13:
  482. Write a query to display the book code, title, publish date of the books which is
  483. been published in the month of December.*/
  484. select book_code,book_title,publish_date
  485. from lms_book_details
  486. where publish_date like "%-12-%";
  487.  
  488. /*Problem # 14:
  489. Write a query to display the book code, book title and availability status of the ‘JAVA’ books
  490. whose edition is "5”. Show the availability status with alias name “AVAILABILITYSTATUS”.
  491. Hint: Book availability status can be fetched from “BOOK_ISSUE_STATUS” column of LMS_BOOK_ISSUE table.*/
  492. select d.book_code,d.book_title,i.book_issue_status as 'AVAILABILITYSTATUS'
  493. from lms_book_details d,lms_book_issue i
  494. where d.book_code=i.book_code and d.book_edition="5"
  495. and d.category="java";
  496.  
  497. /*--------------COMPLEX QUERY---------------------------*/
  498.  
  499. /*Problem # 1:
  500. Write a query to display the book code, book title and supplier name of the supplier who
  501. has supplied maximum number of books. For example, if “ABC Store” supplied 3 books,
  502. “LM Store” has supplied 2 books and “XYZ Store” has supplied 1 book. So “ABC Store” has supplied
  503. maximum number of books, hence display the details as mentioned below.
  504. Example:BOOK_CODE BOOK_TITLE SUPPLIER_NAME
  505. BL000008 Easy Reference for Java ABC STORE
  506. BL000001 Easy Reference for C ABC STORE
  507. BL000003 Easy Reference for VB ABC STORE*/
  508. select d.book_code,d.book_title,s.supplier_name
  509. from lms_book_details d,lms_suppliers_details s
  510. where s.supplier_id=d.supplier_id and s.supplier_id in
  511. (select supplier_id from lms_book_details group by supplier_id having count(*)=
  512. (select max(c) from
  513. (select count(*)c from lms_book_details group by supplier_id)a
  514. )
  515. );
  516.  
  517. /*Problem # 2:
  518. Write a query to display the member id, member name and number of remaining books he/she
  519. can take with “REMAININGBOOKS” as alias name. Hint: Assuming a member can take maximum 3 books.
  520. For example, Ramesh has already taken 2 books; he can take only one book now.
  521. Hence display the remaining books as 1 in below format.
  522. Example:MEMBER_ID MEMBER_NAME REMAININGBOOKS
  523. LM001 RAMESH 1
  524. LM002 MOHAN 3*/
  525. select s.member_id,m.member_name,3-count(*) as 'Remainingbooks'
  526. from lms_book_issue s,lms_members m
  527. where m.member_id=s.member_id
  528. group by s.book_code;
  529.  
  530. /*Problem # 3
  531. Write a query to display the supplier id and supplier name of the supplier who has
  532. supplied minimum number of books. For example, if “ABC Store” supplied 3 books, “LM Store” has supplied
  533. 2 books and “XYZ Store” has supplied 1 book. So “XYZ Store” has supplied minimum number of books,
  534. hence display the details as mentioned below.
  535. Example:
  536. SUPPLIER_ID SUPPLIER_NAME
  537. S04 XYZ STORE*/
  538. select s.supplier_id,s.supplier_name
  539. from lms_book_details d,lms_suppliers_details s
  540. where s.supplier_id=d.supplier_id and d.supplier_id=
  541. (select supplier_id from lms_book_details group by supplier_id having count(*)=
  542. (select min(c) from
  543. (select count(*)c from lms_book_details group by supplier_id)a
  544. )
  545. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement