Useful Database Queries using SQL

Download Useful Database Queries using SQL

Post on 27-Apr-2015

52 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

<p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ I&gt; CLIENT_MASTER4: Name Null? Type</p> <p>------------------------------- -------- ---CLIENT_NO NAME CITY PIN STATE BALDUE VARCHAR2(6) VARCHAR2(20) VARCHAR2(15) NUMBER(8) VARCHAR2(15) NUMBER(10,2)</p> <p>CLIENT NAME</p> <p>CITY</p> <p>PIN</p> <p>STATE</p> <p>BALDUE</p> <p>------ -------------------- --------------- --------- --------------- --------- --------------- -----------C0001 X.MANSFIELD C0002 P.BANERJEE C0003 M.UNNIKRISHNAN C0004 D.SHARMA MUMBAI KOLKATA CHENNAI MANGALORE 400042 MANHATAN 700061 WEST BENGAL 200039 TAMILNADU 560051 KARNATAKA 18000 14000 82000 10000</p> <p>PRODUCT_MASTER4: Name Null? Type</p> <p>------------------------------- -------- ---PNO DESCRIP PROFIT VARCHAR2(6) VARCHAR2(15) NUMBER(4,2)</p> <p>_______________________________________________________ P age |1</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ UNITS QTY_ON_HAND REORDER SALES_PRICE COST_PRICE VARCHAR2(10) NUMBER(8) NUMBER(8) NUMBER(8,2) NUMBER(8,2)</p> <p>PNO DESCRIP</p> <p>PROFIT UNITS</p> <p>QTY_ON_HAND REORDER SALES_PRICE COST_PRICE</p> <p>------ --------------- --------- ---------- ----------- --------- ---------- ---------- -------------- ---------- ------------P0001 T.SHIRTS P0345 SHIRTS P0634 JEANS P0794 DENIMS P0085 SKIRTS 5 6 10 4 10 PIECE PIECE PIECE PIECE PIECE 200 150 350 80 40 50 50 60 100 100 350 500 1020 590 175 250 350 390 390 79</p> <p>SALESMAN_MASTER4: Name Null? Type</p> <p>------------------------------- -------- ---S_NO SNAME ADDRESS CITY PIN STATE SALE_AMT VARCHAR2(6) VARCHAR2(20) VARCHAR2(20) VARCHAR2(20) NUMBER(8) VARCHAR2(20) NUMBER(8,2)</p> <p>_______________________________________________________ P age |2</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ TARGET YTDSALES REMARKS NUMBER(6,2) NUMBER(6,2) VARCHAR2(10)</p> <p>S_NO SNAME REMARKS</p> <p>ADDRESS</p> <p>CITY</p> <p>PIN</p> <p>STATE</p> <p>SALE_AMT TARGET YTDSALES</p> <p>------ -------------------- -------------------- -------------------- --------- ----------------------------- --------- --------- ------------------- ---------------- ----S0001 AMAN.K GOOD A/14 SHIBPUR MUMBAI 400002 MAHARASHTRA 3000 100 50</p> <p>S0002 OMKAR.T GOOD</p> <p>65 NARIMAN</p> <p>MUMBAI</p> <p>400001 MAHARASHTRA</p> <p>3000</p> <p>200</p> <p>100</p> <p>S0003 BANERJEE.R GOOD</p> <p>P7 BANDRA</p> <p>MUMBAI</p> <p>400032 MAHARASHTRA</p> <p>3000</p> <p>200</p> <p>150</p> <p>S0004 ASHISH.M GOOD</p> <p>A/5 ALIGAR</p> <p>CHENNAI</p> <p>200017 TAMILNADU</p> <p>3500</p> <p>200</p> <p>180</p> <p>1) Retrieve the list of names, city &amp; state of all the clients ordered by state. SQL&gt; SELECT NAME,CITY,STATE 2 FROM CLIENT_MASTER4 3 ORDER BY STATE; OUTPUT: NAME CITY STATE</p> <p>-------------------- --------------- --------------D.SHARMA X.MANSFIELD MANGALORE MUMBAI KARNATAKA MANHATAN</p> <p>_______________________________________________________ P age |3</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ M.UNNIKRISHNAN CHENNAI P.BANERJEE KOLKATA TAMILNADU WEST BENGAL</p> <p>2) List all the clients who are located in Mumbai. SQL&gt; SELECT NAME 2 FROM CLIENT_MASTER4 3 WHERE CITY='MUMBAI'; OUTPUT: NAME -------------------X.MANSFIELD</p> <p>3) Change the city of client no C0004 into Bangalore &amp; the pin 540010. SQL&gt; UPDATE CLIENT_MASTER4 2 SET CITY='BANGALORE',PIN=540010 3 WHERE CLIENT_NO='C0004';</p> <p>1 row updated.</p> <p>AFTER UPDATION:</p> <p>CLIENT</p> <p>NAME</p> <p>CITY</p> <p>PIN</p> <p>STATE</p> <p>BALDUE</p> <p>------ -------------------- --------------- --------- --------------- --------- --------- -----------------C0001 C0002 X.MANSFIELD P.BANERJEE MUMBAI KOLKATA 400042 MANHATAN 700061 WEST BENGAL 18000 14000</p> <p>_______________________________________________________ P age |4</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ C0003 C0004 M.UNNIKRISHNAN CHENNAI D.SHARMA BANGALORE 200039 TAMILNADU 540010 KARNATAKA 82000 10000</p> <p>4) Delete all products from product_master where the qty on hand DELETE 2 FROM PRODUCT_MASTER4 3 WHERE QTY_ON_HAND ALTER TABLE CLIENT_MASTER4 2 ADD (TELEPHONE NUMBER(10));</p> <p>Table altered.</p> <p>_______________________________________________________ P age |5</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ SQL&gt; UPDATE CLIENT_MASTER4 2 SET TELEPHONE='&amp;TELEPHONE' 3 WHERE CLIENT_NO='&amp;CLIENT_NO'; Enter value for telephone: 9901234560 old 2: SET TELEPHONE='&amp;TELEPHONE' new 2: SET TELEPHONE='9901234560' Enter value for client_no: C0001 old 3: WHERE CLIENT_NO='&amp;CLIENT_NO' new 3: WHERE CLIENT_NO='C0001'</p> <p>1 row updated.</p> <p>SQL&gt; / Enter value for telephone: 9874012345 old 2: SET TELEPHONE='&amp;TELEPHONE' new 2: SET TELEPHONE='9874012345' Enter value for client_no: C0002 old 3: WHERE CLIENT_NO='&amp;CLIENT_NO' new 3: WHERE CLIENT_NO='C0002'</p> <p>1 row updated.</p> <p>SQL&gt; / Enter value for telephone: 9432145600 _______________________________________________________ P age |6</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ old 2: SET TELEPHONE='&amp;TELEPHONE' new 2: SET TELEPHONE='9432145600' Enter value for client_no: C0003 old 3: WHERE CLIENT_NO='&amp;CLIENT_NO' new 3: WHERE CLIENT_NO='C0003'</p> <p>1 row updated.</p> <p>SQL&gt; / Enter value for telephone: 9731245655 old 2: SET TELEPHONE='&amp;TELEPHONE' new 2: SET TELEPHONE='9731245655' Enter value for client_no: C0004 old 3: WHERE CLIENT_NO='&amp;CLIENT_NO' new 3: WHERE CLIENT_NO='C0004' 1 row updated.</p> <p>AFTER ALTERATION AND UPDATION: CLIENT NAME CITY PIN STATE BALDUE TELEPHONE</p> <p>------ -------------------- --------------- --------- --------------- --------- --------- -------------- ------------------ C0001 X.MANSFIELD C0002 P.BANERJEE MUMBAI KOLKATA 400042 MANHATAN 700061 WEST BENGAL 200039 TAMILNADU 540010 KARNATAKA 18000 14000 82000 10000 9901234560 9874123410 9432145600 9732111100</p> <p>C0003 M.UNNIKRISHNAN CHENNAI C0004 D.SHARMA BANGALORE</p> <p>_______________________________________________________ P age |7</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>6) Change the city for all those salesman whose target is less than 200 to Pune. SQL&gt; UPDATE SALESMAN_MASTER4 2 SET CITY='PUNE' 3 WHERE TARGET SELECT S_NO,SNAME,CITY 2 FROM SALESMAN_MASTER4 3 WHERE YTDSALES&gt;100; OUTPUT: S_NO SNAME CITY</p> <p>------ -------------------- -------------------S0003 BANERJEE.R S0004 ASHISH.M MUMBAI CHENNAI</p> <p>8) Delete all products from Product_master where the quantity on hand is equal to 100. SQL&gt; DELETE FROM PRODUCT_MASTER4 2 WHERE QTY_ON_HAND=100;</p> <p>0 rows deleted.</p> <p>9) Change the size of the column sell price from product_master to (10,2). SQL&gt; ALTER TABLE PRODUCT_MASTER4 2 MODIFY SALES_PRICE NUMBER(10,2); Table altered.</p> <p>AFTER ALTERATION: Name Null? Type</p> <p>------------------------------- -------- ---PNO VARCHAR2(6</p> <p>_______________________________________________________ P age |9</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________ DESCRIP PROFIT UNITS QTY_ON_HAND REORDER SALES_PRICE COST_PRICE VARCHAR2(15) NUMBER(4,2) VARCHAR2(10) NUMBER(8) NUMBER(8) NUMBER(10,2) NUMBER(8,2)</p> <p>10) Change the name of the salesman_master table to s_man SQL&gt; RENAME SALESMAN_MASTER4 TO S_MAN; Table renamed. II&gt;</p> <p>SQL&gt; desc sup_sc4; Name Null? Type ------------------------------- -------- ---S_NO VARCHAR2(5) S_NAME VARCHAR2(10) STATUS NUMBER(4) CITY VARCHAR2(10) SQL&gt; select * from sup_sc4; S_NO S_NAME STATUS CITY ----- ---------- --------- ---------s1 sarala 20 mumbai s2 uma 10 chennai s3 nehru 30 delhi s4 priya 20 mumbai s5 anand 30 chennai SQL&gt; desc part_sc4; Name Null? Type ------------------------------- -------- ---P_NO VARCHAR2(5) P_NAME VARCHAR2(10) COLOR VARCHAR2(10)_______________________________________________________ P a g e | 10</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>WEIGHT CITY SQL&gt; select * from part_sc4;</p> <p>NUMBER(6,2) VARCHAR2(10)</p> <p>P_NO P_NAME COLOR WEIGHT CITY ----- ---------- ---------- --------- ---------p1 nut red 12 mumbai p2 bolt green 17 chennai p3 screw blue 17 bangalore p4 screw red 14 mumbai p5 cam blue 12 chennai p6 cog red 19 mumbai 6 rows selected. SQL&gt; desc prj_sc4; Name Null? Type ------------------------------- -------- ---PRJ_NO VARCHAR2(5) PRJ_NAME VARCHAR2(10) CITY VARCHAR2(10) SQL&gt; select * from prj_sc4; PRJ_NO PRJ_NAME CITY ----- ---------- ---------j1 sorter chennai j2 display nellore j3 ocr delhi j4 console delhi j5 raid mumbai j6 eds bangalore j7 tape mumbai 7 rows selected. SQL&gt; desc spj_sc4; Name Null? Type ------------------------------- -------- ---S_NO VARCHAR2(5) P_NO VARCHAR2(3) J_NO VARCHAR2(3) QTY NUMBER(3)_______________________________________________________ P a g e | 11</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>SQL&gt; select * from spj_sc4; S_NO P_NO J_NO ----- --- --- --------s1 p1 j1 s1 p1 j4 s2 p3 j1 s2 p3 j2 s2 p3 j3 s2 p3 j5 s2 p3 j6 s2 p3 j7 s3 p3 j1 s3 p4 j2 s4 p6 j3 s4 p6 j7 s5 p2 j2 s5 p5 j5 s5 p5 j7 s5 p6 j2 16 rows selected. 11. Get all part-colour combination for each given city. SQL&gt; select distinct city,p_name,color 2 from 3 part_sc4 4 order by city; OUTPUT: CITY P_NAME COLOR ---------- ---------- ---------bangalore screw blue chennai bolt green chennai cam blue mumbai cog red mumbai nut red mumbai screw red 6 rows selected._______________________________________________________ P a g e | 12</p> <p>QTY 200 700 400 200 200 600 400 800 200 500 300 300 200 500 100 200</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>12. Get all supplier no.|part no.|project no. triplets such that the indicated supplier, part and projects are colocated. SQL&gt; select sp.s_no,sp.p_no,sp.j_no,s.city as scity,p.city as pacity,pr.city as prcity 2 from 3 spj_sc4 sp,sup_sc4 s,part_sc4 p,prj_sc4 pr 4 where 5 sp.s_no=lower(s.s_no) 6 and 7 sp.p_no=p.p_no 8 and 9 sp.j_no=pr.prj_no 10 and 11 s.city=p.city 12 and 13 s.city=pr.city 14 ; OUTPUT: S_NO P_NO J_N SCITY PACITY PRCITY ----- --- --- ---------- ---------- ---------s4 p6 j7 mumbai mumbai mumbai</p> <p>13.Get all supplier no.,part no.,project no. triplets such that the indicated supplier,part and projects are not colocated. SQL&gt; select distinct sp.s_no,sp.p_no,sp.j_no,s.city as scity,p.city as pacity,pr.city as prcity 2 from 3 spj_sc4 sp,sup_sc4 s,part_sc4 p,prj_sc4 pr 4 where 5 sp.s_no=lower(s.s_no) 6 and 7 sp.p_no=p.p_no 8 and 9 sp.j_no=pr.prj_no 10 and 11 s.cityp.city 12 and 13 p.citypr.city 14 and 15 s.citypr.city;_______________________________________________________ P a g e | 13</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>OUTPUT: S_NO P_NO J_NO SCITY PACITY PRCITY ----- --- --- ---------- ---------- ---------s2 p3 j2 chennai bangalore nellore s2 p3 j3 chennai bangalore delhi s2 p3 j5 chennai bangalore mumbai s2 p3 j7 chennai bangalore mumbai s3 p3 j1 delhi bangalore chennai s3 p4 j2 delhi mumbai nellore s5 p6 j2 chennai mumbai nellore 7 rows selected. 14. Get part no. for parts supplied by a supplier in Mumbai to a project in Mumbai. SQL&gt; select sp.p_no 2 from spj_sc4 sp,sup_sc4 s,prj_sc4 pr 3 where 4 sp.s_no=lower(s.s_no) 5 and 6 sp.j_no=pr.prj_no 7 and 8 s.city='mumbai' 9 and 10 pr.city='mumbai' 11 ; OUTPUT: P_N --p6</p> <p>15. Get all pairs of the city names such that supplier from the first city supplies to a project in the second city. SQL&gt; select distinct s.city,pr.city 2 from spj_sc4 sp,sup_sc4 s,prj_sc4 pr 3 where 4 sp.s_no=lower(s.s_no) 5 and 6 sp.j_no=pr.prj_no_______________________________________________________ P a g e | 14</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>7 ; CITY CITY ---------- ---------chennai bangalore chennai chennai chennai delhi chennai mumbai chennai nellore delhi chennai delhi nellore mumbai chennai mumbai delhi mumbai mumbai 10 rows selected. 16. 6.Get part numbers for the parts supplied to any project by the supplier in the same city as the project. SQL&gt; select distinct sp.p_no 2 from spj_sc4 sp,sup_sc4 s,prj_sc4 pr 3 where 4 sp.s_no=lower(s.s_no) 5 and 6 sp.j_no=pr.prj_no 7 and 8 s.city=pr.city 9 ; OUTPUT: P_NO --p3 p6 2 rows selected.</p> <p>17. Get project numbers for projects supplied by at least one supplier not in the same city. SQL&gt; select distinct sp.j_no 2 from spj_sc4 sp,sup_sc4 s,prj_sc4 pr_______________________________________________________ P a g e | 15</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>3 4 5 6 7 8 9</p> <p>where sp.s_no=lower(s.s_no) and sp.j_no=pr.prj_no and s.citypr.city ;</p> <p>OUTPUT: J_NO --j1 j2 j3 j4 j5 j6 j7 7 rows selected.</p> <p>18. 8.Get all pairs of part numbers such that some supplier supplies both the indicated parts. SQL&gt; select distinct a.p_no,b.p_no 2 from spj_sc4 a,spj_sc4 b 3 where 4 a.s_no=b.s_no 5 and 6 a.p_no&gt;b.p_no 7 ; P_NO --- --p2 p2 p3 p5 P_NO p5 p6 p4 p6</p> <p>4 rows selected._______________________________________________________ P a g e | 16</p> <p>DBMS Lab Assignment:</p> <p>SRIJIT CHOWDHURY</p> <p>E-mail: src.dcs.dcs@gmail.com</p> <p>_____________________________________________________________________________________</p> <p>19.Get project numbers for projects using at least one part available from supplier 's1'. SQL&gt; select distinct sp1.j_no 2 from spj_sc4 sp1 3 where sp1.p_no in 4 (select sp2.p_no 5 from spj_sc4 sp2 6 where upper(sp2.s_no)='S1' 7 ); OUTPUT: J_NO --j1 j4</p> <p>20. Get supplier_no from supplier with satus lower than that of supplier s1 SQL&gt; select s.s_no 2 from sup_sc4 s 3 where s.states &lt; 4 (select s1.states 5 from sup_sc4 s1 6 where upper(s1.s_no)='S1' 7 ); OUTPUT: S_NO ----s2</p> <p>21. Get project numbers for projects whose city is first in the alphabetic list of such cities</p> <p>SQL&gt; select prj1.prj_no 2 from prj_sc4 prj1 3 where prj1.city &lt; all 4 (select prj2.city 5 from prj_sc4 prj2_______________________________________________________ P a...</p>