Sei sulla pagina 1di 92

SQL_Queries Queries solved by Somdutt Ganguly P april - 2002 (document printed) email: gangulysomdutt@yahoo.

com about me: i am right now a student of TY Bachelor in Computer application from cpica college, Gujarat University, Ahmedabad, Gujarat, India.. address: no 6, chandrodaya apt, bhaikaka nagar, thaltej, ahemdabad, gujarat, india. 'The following are the most twisted 150 queries given to us in 'our college......i think it will be very much useful and handy in case 'when u can't solve these queries.....offcourse!! it took to bit of brain 'to solve these...(may be one or 2 or 3 are doubtful!! but will work...sometimes 'different answers will come up if u r working with oracle on a network and 'on a personal edition...... ....I welcome feedbacks..and do vote for my hard work!! TABLES: 1) Table name - programmer Page 1

SQL_Queries name not null varchar2(8) name dob not null date date of birth doj not null date date of joining sex not null varchar2(1) male or female prof1 varchar2(8) known language 1 prof2 varchar2(8) known language 2 salary not null number(4) salary Entry eg: somdutt 21-apr-66 basic 3200 2) Table name

21-apr-92

pascal

- software name developed project language software cost development cost number of

name not null varchar2(8) title not null varchar2(20) name dev_in not null varchar2(8) developed scost number(7,2) dcost number(5) sold number(3) software sold Entry eg: somdutt parachutes

basic

399.95

6000 43

3) Table name - studies name not null varchar2(8) name splace not null varchar2(9) studies place Page 2

SQL_Queries course not null varchar2(5) course studied ccost not null number(5) course cost Entry example: somdutt sabhari pgdca 4500 devdutt bdps dcs 5000 QUERIES - I 1) Find out the SELLING COST AVERAGE for the packages developed in PASCAL? 2) Display the names and ages of all programmers. 3) Display the names and ages of all the programmers 4) What is the highest numbers of copies sold by a package? 5) Display the names and date of birth of all the programmer born in JANUARY. 6) Display lowest course fee. 7) How many programmer has done PGDCA course. 8) How much revenue has been earned through sales of packages in C. 9) Display the details of software developed by Ramesh? 10) How many programmers studied at SABHARI. 11) Display the details of PACKAGES whose sales crossed the 20000 mark. 12) Find out the number of copies which should be sold in order to recover the development cost of each package. 13) What is the price of the costliest software developed in BASIC? Page 3

SQL_Queries 14) Display the details of packages for which development cost has been recovered. 15) How many packages were developed in dbase? 16) How many programmers studies at paragathi? 17) How many programmers paid 5000 to 10000 for their course? 18) What is the average course fee? 19) Display the details of programmers knowing c? 20) How many programmers know either Cobol or Pascal? 21) How many programmers don't know Pascal & C? 22) How old is the oldest male programmers? 23) What is the average age of female programmers? 24) Calculate the experience in years for each programmers and display along with the names in descending order? 25) Who are the programmers who celebrate their birthday during the current month? 26) How many female programmers are there? 27) What are the languages known by the male programmers? 28) What is the Average salary? 29) How many people draw 2000 to 4000? 30) Display the details of those who don't know Clipper, Cobol or Pascal? 31) How many Female programmers knowing C are above 24 years of age? 32) Who are the programmers who will be celebrating their Birthday within a week? 33 Display the details of those with less than a year's experience? Page 4

SQL_Queries 34 Display the details of those who will be completing 2 years of service this year? 35 Calculate the amount to be recovered for those packages whose development cost has not been recovered? 36)ist the packages which have not been sold so far? 37) Find out the cost of the software developed by Mary? 38) Display the institutes names from the studies table without duplicates? 39) How many different courses are mentioned in the studies table? 40) Display the names of the programmers whose names contain 2 occurrences of the letter A? 41) Display the names of programmers whose names contain unto 5 characters? 42) How many female programmers knowing COBOL have more than 2 years experience? 43) What is the length of the shortest name in the programmer table? 44) What is the average development cost of a package developed in COBOL? 45) Display the name,sex,dob(DD/MM/YY format), doj for all the programmers without using conversion function? 46) Who are the programmers who were born on the last day of the month? 47) What is the amount paid in salaries of the male programmers who do not know Cobol? 48) Display the title, scost, dcost and difference between scost and dcost in descending order of difference? 49) Display the name, dob, doj of those month Page 5

SQL_Queries of birth and month of joining are same? 50) Display the names of the packages whose names contain more than 1 word? QUERIES - II 1) Display THE NUMBER OF packages developed in EACH language. 2) Display THE NUMBER OF packages developed by EACH person. 3) Display THE NUMBER OF male and female programmer. 4) Display THE COSTLIEST packages and HIGEST selling developed in EACH language. 5) Display THE NUMBER OF people BORN in EACH YEAR. 6) Display THE NUMBER OF people JOINED in EACH YEAR. 7) Display THE NUMBER OF people BORN in EACH MONTH. 8) Display THE NUMBER OF people JOINED in EACH MONTH. 9) Display the language wise COUNTS of prof1. 10) Display the language wise COUNTS of prof2. 11) Display THE NUMBER OF people in EACH salary group. 12) Display THE NUMBER OF people who studied in EACH institute. 13) Display THE NUMBER OF people who studied in EACH course. 14) Display the TOTAL development COST of the packages developed in EACH language. Page 6

SQL_Queries 15) Display the selling cost of the package developed in EACH language. 16) Display the cost of the package developed by EACH programmer. 17) Display the sales values of the package developed in EACH programmer. 18) Display the NUMBER of packages developed by EACH programmer. 19) Display the sales COST of packages developed by EACH programmer language wise. 20) Display EACH programmers name, costliest package and cheapest packages developed by Him/Her. 21) Display EACH language name with AVERAGE development cost, AVERAGE cost, selling cost and AVERAGE price per copy. 22) Display EACH institute name with NUMBER of courses, AVERAGE cost per course. 23) Display EACH institute name with NUMBER of students. 24) Display names of male and female programmers. 25) Display the programmer's name and their packages. 26) Display the NUMBER of packages in EACH language. 27) Display the NUMBER of packages in EACH language for which development cost is less than 1000. 28) Display the AVERAGE difference BETWEEN scost and dcost for EACH language. 29) Display the TOTAL scost, dcsot and amount TOBE recovered for EACH programmer for whose dcost HAS NOT YET BEEN recovered. Page 7

SQL_Queries 30) Display highest, lowest and average salaries for THOSE earning MORE than 2000. QUERIES - III 1) Who is the highest paid C programmer? 2) Who is the highest paid female cobol programmer? 3) Display the name of the HIGEST paid programmer for EACH language (prof1) 4) Who is the LEAST experienced programmer? 5) Who is the MOST experienced programmer? 6) Which language is known by ONLY ONE programmer? 7) Who is the YONGEST programmer knowing DBASE? 8) Which institute has MOST NUMBER of students? 9) Who is the above programmer? 10) Which female programmer earns MORE than 3000/- but DOES NOT know C, C++, Oracle or Dbase? 11) Which is the COSTLIEST course? 12) Which course has been done by MOST of the students? 13) Display name of the institute and course Which has below AVERAGE course fee? 14) Which institute conducts COSTLIEST course? 15) Which course has below AVERAGE number of students? 16) Which institute conducts the above course? 17) Display names of the course WHOSE fees are Page 8

SQL_Queries within 1000(+ or -) of the AVERAGE fee. 18) Which package has the HIGEST development cost? 19) Which package has the LOWEST selling cost? 20) Who developed the package, which has sold the LEAST number of copies? 21) Which language was used to develop the package WHICH has the HIGEST sales amount? 22) How many copies of the package that has the LEAST DIFFRENCE between development and selling cost were sold? 23) Which is the COSTLIEAST package developed in PASCAL? 24) Which language was used to develop the MOST NUMBER of package? 25) Which programmer has developed the HIGEST NUMBER of package? 26) Who is the author of the COSTLIEST package? 27) Display names of packages WHICH have been sold LESS THAN the AVERAGE number of copies? 28) Who are the female programmers earning MORE than the HIGEST paid male programmers? 29) Which language has been stated as prof1 by MOST of the programmers? 30) Who are the authors of packages, WHICH have recovered MORE THAN double the development cost? 31) Display programmer names and CHEAPEST package developed by them in EACH language? 32) Who is the YOUNGEST male programmer born in 1965? 33) Display language used by EACH programmer to develop the HIGEST selling and LOWEST Page 9

selling package. 34) Who is the OLDEST female programmer WHO joined in 1992 35) In WHICH year where the MOST NUMBER of programmer born? 36) In WHICH month did MOST NUMBRER of programmer join? 37) In WHICH language are MOST of the programmer's proficient? 38) Who are the male programmers earning BELOW the AVERAGE salary of female programmers? QUERY - IV 1) Display the details of THOSE WHO are drawing the same salary. 2) Display the details of software developed by male programmers earing MORE than 3000. 3) Display details of packages developed in PASCAL by female programmers. 4) Display the details of these programmer WHO joined BEFORE 1990. 5)Display details of software developed in C by female programmers of PRAGATHI. 6) Display NUMBER of packages NUMBER of copies sold and sales value of EACH programmer Institute-wise. 7) Display details of software developed in DBASE by male programmers WHO belong to the institute on which MOST NUMBER OF programmers studies. 8) Display the details of the software that was developed by male programmers born BEFORE Page 10

SQL_Queries

SQL_Queries 1965 and female programmers born AFTER 1975. 9) Display the details of the software that was developed in the language that is NOT the programmers first proficiency. 10) Display details of software that was developed in the language which is NITHER first NOR second proficiency of the programmer. 11) Display details of software developed by male students of SABHARI. 12) Display the names of programmers WHO HAVE NOT developed any package. 13) What is the total cost of the software developed by the programmers by APPLE? 14) Who are the programmers WHO JOINED in the same day? 15) Who are the programmers WHO HAVE THE SAME PROF2? 16) Display the total sales values of software, institutes-wise. 17) In which institutes did the person who developed the COSTLIEST package study? 18) Which language listed in prof1 and prof2 HAS NOT BEEN used to develop any package? 19) How much does the person WHO developed the HIGHEST selling package earn and WHAT course did he/she undergo? 20) How many months will it take for each programmer to recover the cost of the course underwent? 21) Which is the COSTLIEST package developed by a person with under 3 years expenence? 22) What is the AVERAGE salary for those WHOSE software's sales value is more than 50,000? Page 11

SQL_Queries 23) How many packages were developed by the students WHO studied in the institute that Charge the LOWEST course fee? 24) How many packages were developed by the person WHO developed the CHEAPEST package. Where did he\she study? 25) How many packages were developed by female programmers earning MORE than the HIGHEST paid male programmer? 26) How many packages were developed by the MOST experienced programmers from BDPS. 27) List the programmers (from software table) and institutes they studied, including those WHO DIDN'T develop any package. 28) List each profit with the number of programmers having that prof1 and the number of packages developed in that prof1. 29) List programmer names (from programmer table) and number of packages EACH developed. 30) List all the details of programmers who has done a course at S.S.I.L. QUERIES - I 1) Find out the SELLING COST AVERAGE for the packages developed in PASCAL? SELECT AVG(SCOST) FROM SOFTWARE WHERE DEV_IN LIKE 'PASCAL' Output:

Page 12

AVG(SCOST) ---------2362.48

SQL_Queries

2) Display the names and ages of all programmers. SELECT NAME AS NAME,FLOOR((SYSDATE-DOB)/365) AS AGE FROM PROGRAMMER OR SELECT 100 + MONTHS_BETWEEN(SYSDATE,DOB)/12 FROM PROGRAMMER; Output: NAME AGE -------- ---------ANAND 34 ALTAF 36 JAGADESH 30 JULIANA 32 KAMALA 32 MARY 30 NELSON 35 PATRICK 35 QADIR 35 RAMESH 33 REBECCA 33 REMITHA 30 REVATHI 31 VIJAYA 35

Page 13

SQL_Queries 3) Display the names and ages of all the programmers. SELECT NAME FROM STUDIES WHERE COURSE LIKE 'DAP' Output: NAME -------NELSON REVATHI 4) What is the highest numbers of copies sold by a package? SELECT MAX(SOLD) FROM SOFTWARE Output: SOLD ---------114

5) Display the names and date of birth of all the programmer born in JANUARY. SELECT DOB,NAME FROM PROGRAMMER

Page 14

SQL_Queries WHERE TO_CHAR(DOB,'MON') LIKE 'JAN' Output: DOB --------31-JAN-68 01-JAN-67 NAME -------JULIANA REBECCA

6) Display lowest course fee. SELECT MIN(CCOST) FROM STUDIES Output: MIN(CCOST) ---------3500 7) How many programmer has done PGDCA course. SELECT COUNT(NAME) FROM STUDIES WHERE COURSE LIKE 'PGDCA' Output: COUNT(NAME) ----------3 8) How much revenue has been earned through sales of packages in C. SELECT SUM(SOLD*SCOST) FROM SOFTWARE WHERE DEV_IN LIKE 'C' Page 15

SQL_Queries Output: SUM(SOLD*SCOST) --------------113775 9) Display the details of software developed by Ramesh? SELECT * FROM SOFTWARE WHERE NAME='RAMESH' Output: NAME TITLE DEV_IN SCOST DCOST SOLD -------- -------------------- ----------------- ---------- ---------RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4 RAMESH DEAD LEE PASCAL 99.95 4500 73 10) How many programmers studied at SABHARI. SELECT COUNT(NAME) AS NOPROGRAMMERS FROM STUDIES WHERE SPLACE='SABHARI' Output: NOPROGRAMMERS ------------4 Page 16

SQL_Queries 11) Display the details of PACKAGES whose sales crossed the 20000 mark. SELECT * FROM SOFTWARE WHERE (SOLD*SCOST)>20000 Output: NAME TITLE DEV_IN SCOST DCOST SOLD -------- -------------------- ----------------- ---------- ---------ANAND VIDEO TITLING PACK PASCAL 7500 16000 9 JAGADESH SHARES MANAGEMENT ORACLE 3000 12000 14 KAMALA PAYROLL PACKAGE DBASE 9000 20000 7 MARY FINANCILA ACC S/W ORACLE 18000 85000 4 MARY CODE GENERATOR C 4500 20000 23 MARY READ ME C++ 300 1200 84 QADIR BOMBS AWAY ASSEMBLY 499.95 530 114 QADIR VACCINES C 1900 3400 21 RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4 REMITHA PC UTILITIES C 725 5000 51 REVATHI QUIZ MASTER BASIC 3200 2100 15 Page 17

SQL_Queries 12) Find out the number of copies which should be sold in order to recover the development cost of each package. SELECT ROUND(DCOST/SCOST) FROM SOFTWARE WHERE SCOST*SOLD<DCOST Output: ROUND(DCOST/SCOST) -----------------1 5 68 13) What is the price of the costliest software developed in BASIC? SELECT MAX(SCOST) FROM SOFTWARE WHERE DEV_IN LIKE 'BASIC' Output: MAX(SCOST) ---------3200

Page 18

SQL_Queries

14) Display the details of packages for which development cost has been recovered. SELECT * FROM SOFTWARE WHERE (SOLD*SCOST)>DCOST Output: NAME TITLE DEV_IN SCOST DCOST SOLD -------- -------------------- ----------------- ---------- ---------ANAND PARACHUTES BASIC 399.95 6000 43 ANAND VIDEO TITLING PACK PASCAL 7500 16000 9 JAGADESH SERIAL LINK UTILITY JAVA 800 7500 10 JAGADESH SHARES MANAGEMENT ORACLE 3000 12000 14 KAMALA PAYROLL PACKAGE DBASE 9000 20000 7 MARY CODE GENERATOR C 4500 20000 23 Page 19

MARY 300 PATRICK 750 QADIR 499.95 QADIR 1900 RAMESH 12000 RAMESH 99.95 REMITHA 725 REMITHA 2500 REVATHI 3200 VIJAYA 900

READ ME 1200 84 GRAPHIC EDITOR 5000 11 BOMBS AWAY 530 114 VACCINES 3400 21 HOTEL MANAGEMENT 35000 4 DEAD LEE 4500 73 PC UTILITIES 5000 51 TSR HELP PACKAGE 6000 6 QUIZ MASTER 2100 15 ISK EDITOR 700 6

SQL_Queries

C++ PASCAL ASSEMBLY C DBASE PASCAL C ASSEMBLY BASIC C

15) How many packages were developed in dbase? SELECT COUNT(TITLE) AS TOTAL FROM SOFTWARE WHERE DEV_IN='DBASE' Output: TOTAL ---------2 16) How many programmers studies at paragathi? Page 20

SQL_Queries SELECT COUNT(NAME) FROM STUDIES WHERE SPLACE='PRAGATHI' Output: COUNT(NAME) ----------3 17) How many programmers paid 5000 to 10000 for their course? SELECT COUNT(NAME) AS NOOFPROGRAMMERS FROM STUDIES WHERE CCOST>=5000 AND CCOST<=10000 Output: NOOFPROGRAMMERS --------------6

18) What is the average course fee? SELECT AVG(CCOST) AS AVERAGECOST FROM STUDIES Output: AVERAGE_COST

Page 21

-----------10471.429

SQL_Queries

19) Display the details of programmers knowing c? SELECT * FROM PROGRAMMER WHERE PROF1='C' OR PROF2='C' Output: NAME DOB DOJ SALARY -------- --------- ---------------- ---------KAMALA 30-OCT-68 02-JAN-92 2900 QADIR 31-AUG-65 21-APR-93 3000 REMITHA 19-APR-70 20-APR-93 ASSEMBLY 3600 VIJAYA 14-DEC-65 02-MAY-92 3500 S PROF1 - -------F C DBASE PROF2

M ASSEMBLY C F C F FOXPRO C

20) How many programmers know either Cobol or Pascal? SELECT COUNT(NAME) AS PROGRAMMERS FROM PROGRAMMER WHERE PROF1='COBOL' OR PROF1='PASCAL' OR PROF2='COBOL' OR PROF2='PASCAL' Page 22

SQL_Queries Output: PROGRAMMERS ----------8 21) How many programmers don't know Pascal & C? SELECT COUNT(NAME) AS PROGRAMMER FROM PROGRAMMER WHERE PROF1!='C' AND PROF1!='PASCAL' AND PROF2!='C' AND PROF2!='PASCAL' Output: PROGRAMMER ---------6 22) How old is the oldest male programmers? SELECT MAX(FLOOR((SYSDATE - DOB)/365)) AS OLDESTMALEPROGRAMMERAGE FROM PROGRAMMER Output: OLDESTMALEPROGRAMMERAGE ----------------------36

Page 23

SQL_Queries 23) What is the average age of female programmers? SELECT FLOOR(AVG(FLOOR((SYSDATE - DOB)/365))) AS AVERAGEFEMALEAGE FROM PROGRAMMER Output: AVERAGEFEMALEAGE ---------------32 24) Calculate the experience in years for each programmers and display along with the names in descending order? SELECT NAME AS PRNAME,FLOOR((SYSDATE DOJ)/365) AS EXPERIENCE FROM PROGRAMMER ORDER BY NAME DESC Output: PR_NAME EXPERIENCE -------- ---------VIJAYA 8 REVATHI 9 REMITHA 7 REBECCA 10 RAMESH 10 QADIR 7 PATRICK 10 NELSON 11

Page 24

MARY KAMALA JULIANA JAGADISH ANAND ALTAF

SQL_Queries 10 9 10 6 8 10

25) Who are the programmers who celebrate their birthday during the current month? SELECT NAME FROM PROGRAMMER WHERE TO_CHAR(DOB,'MM')=TO_CHAR(SYSDATE,'MM') Output: NAME -------ANAND REMITHA 26) How many female programmers are there? SELECT COUNT(NAME) AS NOFPROG FROM PROGRAMMER WHERE SEX='F' Output: NO_OF_FEMALE_PROGRAMMERS -----------------------7

Page 25

SQL_Queries 27) What are the languages known by the male programmers? SELECT DISTINCT PROF1 AS LANGUAGES FROM PROGRAMMER WHERE SEX='M' UNION SELECT DISTINCT PROF2 AS LANGUAGES FROM PROGRAMMER WHERE SEX='M' Output: LANGUAGE -------ASSEMBLY BASIC C CLIPPER COBOL DBASE JAVA ORACLE PASCAL 28) What is the Average salary? SELECT AVG(SALARY) AS AVGSAL FROM PROGRAMMER Output: AVERAGE_SALARY -------------3235.7143

Page 26

SQL_Queries 29) How many people draw 2000 to 4000? SELECT NAME AS PRNAME FROM PROGRAMMER WHERE SALARY>= 2000 OR SALARY <=4000 Output: NAME -------ANAND ALTAF JULIANA KAMALA NELSON PATRICK QADIR RAMESH REBECCA REMITHA REVATHI VIJAYA

30) Display the details of those who don't Page 27

SQL_Queries know Clipper, Cobol or Pascal? SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('CLIPPER','COBOL','PASCAL') AND PROF2 NOT IN ('CLIPPER','COBOL','PASCAL') Output: NAME DOB DOJ SALARY -------- --------- ---------------- ---------JAGADESH 06-OCT-70 04-OCT-94 4100 KAMALA 30-OCT-68 02-JAN-92 2900 MARY 24-JUN-70 01-FEB-91 4500 QADIR 31-AUG-65 21-APR-93 3000 REMITHA 19-APR-70 20-APR-93 ASSEMBLY 3600 VIJAYA 14-DEC-65 02-MAY-92 3500 S PROF1 - -------M ORACLE F C F C++ JAVA DBASE ORACLE PROF2

M ASSEMBLY C F C F FOXPRO C

31) How many Female programmers knowing C are above 24 years of age? SELECT COUNT(NAME) AS NOOFFPRG FROM PROGRAMMER WHERE SEX='F' AND (PROF1='C' OR PROF2='C') AND ((SYSDATE-DOB)/365) > 24 Page 28

SQL_Queries Output: NOOFFPRG ---------3 32) Who are the programmers who will be celebrating their Birthday within a week? SELECT NAME FROM PROGRAMMER WHERE TO_CHAR(DOB,'WW') LIKE TO_CHAR(SYSDATE,'WW') OR select * from programmer where to_char(dob,'dd') between to_char(sysdate,'dd') and to_char(next_day(sysdate,(to_char(sysdate,'day '))),'dd') and to_char(dob,'mon') between to_char(sysdate,'mon') and to_char(next_day(sysdate,(to_char(sysdate,'day '))),'mon') Output: no rows selected 33 Display the details of those with less than a year's experience? SELECT * FROM PROGRAMMER

Page 29

SQL_Queries WHERE FLOOR((SYSDATE - DOJ)/365)<1 Output: NAME SALARY --------------ANAND 3200 ALTAF 2800 JAGADISH 4100 JULIANA 3000 KAMALA 2900 MARY 4500 NELSON 2500 PATRICK CLIPPER QADIR 3000 RAMESH 3200 REBECCA 2500 REMITHA ASSEMBLY REVATHI 3700 VIJAYA DOB DOJ S PROF1 PROF2

--------- --------- - --------------21-APR-66 21-APR-92 M PASCAL BASIC 02-JUL-64 13-NOV-90 M CLIPPER 06-OCT-70 04-OCT-94 M ORACLE 31-JAN-68 21-APR-90 F COBOL 30-OCT-68 02-JAN-92 F C 24-JUN-70 01-FEB-91 F C++ 11-SEP-65 11-OCT-89 M COBOL COBOL JAVA DBASE DBASE ORACLE DBASE

19-NOV-65 21-APR-90 M PASCAL 2800 31-AUG-65 21-APR-93 M ASSEMBLY C 03-MAY-67 28-FEB-91 M PASCAL 01-JAN-67 01-DEC-90 F BASIC 19-APR-70 20-APR-93 F C 3600 02-DEC-69 02-JAN-92 F PASCAL 14-DEC-65 02-MAY-92 F FOXPRO Page 30 DBASE COBOL

BASIC C

3500 14 rows selected.

SQL_Queries

34 Display the details of those who will be completing 2 years of service this year? SELECT NAME AS PRNAME FROM PROGRAMMER WHERE FLOOR((SYSDATE-DOJ)/365)=2; OR select * from programmer where (to_number(to_char(sysdate,'yyyy'))-to_number( to_char(doj,'yyyy')))=2; OR SELECT * FROM PROGRAMMER WHERE (100 + MONTHS_BETWEEN('31-DEC-00',DOJ)/12)>=2 AND (100 + MONTHS_BETWEEN(SYSDATE,DOJ)/12) != 2; Output: no rows selected 35 Calculate the amount to be recovered for those packages whose development cost has not been recovered? SELECT (DCOST-(SCOST*SOLD)),TITLE Page 31

FROM SOFTWARE WHERE (SCOST*SOLD)<DCOST Output: (DCOST-(SCOST*SOLD)) -------------------3500 13000 72800

SQL_Queries

TITLE -------------------INVENTORY CONTROL FINANCILA ACC S/W HOTEL MANAGEMENT

36)ist the packages which have not been sold so far? SELECT TITLE AS SOFTNAME FROM SOFTWARE WHERE SOLD=0 Output: SOFTNAME -------------------INVENTORY CONTROL 37) Find out the cost of the software developed by Mary? SELECT TITLE AS SOFTNAME,SCOST AS SOFTCOST FROM SOFTWARE WHERE NAME='MARY' Output: SOFTNAME SOFTCOST -------------------- ---------FINANCILA ACC S/W 18000 CODE GENERATOR 4500 Page 32

READ ME

SQL_Queries 300

38) Display the institutes names from the studies table without duplicates? SELECT DISTINCT SPLACE FROM STUDIES Output: SPLACE --------APPLE BDPS BITS BRILLIANT CCIT PRAGATHI S.S.I.L SABHARI

39) How many different courses are mentioned in the studies table? SELECT DISTINCT COURSE AS COURSES FROM STUDIES Output: COURS ----DAP DCA DCA/P

Page 33

DCAP DCP DCS HDCP PGDCA

SQL_Queries

40) Display the names of the programmers whose names contain 2 occurrences of the letter A? SELECT NAME FROM PROGRAMMER WHERE NAME LIKE '%A%A%' Output: NAME -------ANAND ALTAF JAGADESH JULIANA KAMALA VIJAYA 41) Display the names of programmers whose names contain unto 5 characters? SELECT NAME FROM PROGRAMMER WHERE LENGTH(NAME)=5 Output: NAME -------ANAND

Page 34

ALTAF QADIR

SQL_Queries

42) How many female programmers knowing COBOL have more than 2 years experience? SELECT NAME AS PRNAME FROM PROGRAMMER WHERE FLOOR((SYSDATE-DOJ)/365)>2 AND SEX='F' AND (PROF1='COBOL' OR PROF2='COBOL') Output: PRNAME -------JULIANA REBECCA

43) What is the length of the shortest name in the programmer table? SELECT MIN(LENGTH(NAME)) AS SHORTNAME FROM PROGRAMMER Output: SHORTNAME ---------4 44) What is the average development cost of a Page 35

SQL_Queries package developed in COBOL? SELECT AVG(DCOST) AS AVGCOST FROM SOFTWARE WHERE DEV_IN='COBOL' Output: AVGCOST ---------3500 45) Display the name,sex,dob(DD/MM/YY format), doj for all the programmers without using conversion function? SELECT NAME AS PRNAME, SEX AS SEX, SUBSTR(DOB,1,2)||'/'||SUBSTR(DOB,4,3)||'/'||SU BSTR(DOB,8,2) AS DATEOFBIRTH, SUBSTR(DOJ,1,2)||'/'||SUBSTR(DOJ,4,3)||'/'||SU BSTR(DOJ,8,2) AS DATEOFJOIN FROM PROGRAMMER

Output: PRNAME -------ANAND ALTAF JAGADESH JULIANA KAMALA MARY NELSON PATRICK

S M M M F F F M M

DATEOFBIR DATEOFJOI --------- --------21/APR/66 21/APR/92 02/JUL/64 13/NOV/90 06/OCT/70 04/OCT/94 31/JAN/68 21/APR/90 30/OCT/68 02/JAN/92 24/JUN/70 01/FEB/91 11/SEP/65 11/OCT/89 19/NOV/65 21/APR/90 Page 36

QADIR RAMESH REBECCA REMITHA REVATHI VIJAYA

M M F F F F

SQL_Queries 31/AUG/65 21/APR/93 03/MAY/67 28/FEB/91 01/JAN/67 01/DEC/90 19/APR/70 20/APR/93 02/DEC/69 02/JAN/92 14/DEC/65 02/MAY/92

46) Who are the programmers who were born on the last day of the month? SELECT NAME AS PRNAME FROM PROGRAMMER WHERE LAST_DAY(DOB) LIKE DOB Output: PRNAME -------JULIANA QADIR

47) What is the amount paid in salaries of the male programmers who do not know Cobol? SELECT SALARY AS SALOFPR FROM PROGRAMMER WHERE SEX='M' AND (PROF1!='COBOL' OR PROF2!='COBOL) Page 37

SQL_Queries Output: SALOFPR ---------3200 2800 4100 2500 2800 3000 3200 48) Display the title, scost, dcost and difference between scost and dcost in descending order of difference? SELECT TITLE AS SOFTNAME,SCOST AS SOFTCOST,DCOST AS DEVCOST,DCOST - SCOST DIFF FROM SOFTWARE ORDER BY 4 DESC Output: SOFTNAME SOFTCOST DEVCOST DIFF -------------------- ---------- ------------------HOTEL MANAGEMENT 1100 75000 73900 FINANCILA ACC S/W 18000 85000 67000 HOTEL MANAGEMENT 12000 35000 23000 CODE GENERATOR 4500 20000 15500 Page 38

PAYROLL PACKAGE 11000 SHARES MANAGEMENT 9000 VIDEO TITLING PACK 8500 SERIAL LINK UTILITY 6700 PARACHUTES 5600.05 DEAD LEE 4400.05 PC UTILITIES 4275 GRAPHIC EDITOR 4250 TSR HELP PACKAGE 3500 VACCINES 1500 READ ME 900 INVENTORY CONTROL 500 BOMBS AWAY 30.05 ISK EDITOR -200 QUIZ MASTER -1100

SQL_Queries 9000 3000 7500 800 399.95 99.95 725 750 2500 1900 300 3000 499.95 900 3200

20000 12000 16000 7500 6000 4500 5000 5000 6000 3400 1200 3500 530 700 2100

49) Display the name, dob, doj of those month of birth and month of joining are same? Page 39

SQL_Queries SELECT NAME AS PRNAME FROM PROGRAMMER WHERE TO_CHAR(DOB,'MM')=TO_CHAR(DOJ,'MM') Output: PRNAME -------ANAND JAGADESH REMITHA

50) Display the names of the packages whose names contain more than 1 word? SELECT TITLE AS PACKAGES FROM SOFTWARE WHERE TITLE LIKE '% %' Output: PACKAGES -------------------VIDEO TITLING PACK SERIAL LINK UTILITY SHARES MANAGEMENT INVENTORY CONTROL PAYROLL PACKAGE FINANCILA ACC S/W CODE GENERATOR READ ME GRAPHIC EDITOR Page 40

BOMBS AWAY HOTEL MANAGEMENT DEAD LEE PC UTILITIES TSR HELP PACKAGE HOTEL MANAGEMENT QUIZ MASTER ISK EDITOR

SQL_Queries

QUERIES - II 1) Display THE NUMBER OF packages developed in EACH language. SELECT DEV_IN AS LANGUAGE,COUNT(TITLE) AS NOOFPACK FROM SOFTWARE GROUP BY DEV_IN Output: LANGUAGE NOOFPACK -------- ---------ASSEMBLY 2 BASIC 2 C 4 C++ 1 COBOL 1 DBASE 2 JAVA 1 ORACLE 2

Page 41

PASCAL

SQL_Queries 4

2) Display THE NUMBER OF packages developed by EACH person. SELECT NAME AS PRNAME,COUNT(TITLE)AS NOOFPACK FROM SOFTWARE GROUP BY NAME Output: PRNAME NOOFPACK -------- ---------ANAND 2 JAGADESH 2 JULIANA 1 KAMALA 1 MARY 3 PATRICK 1 QADIR 2 RAMESH 2 REMITHA 2 REVATHI 2 VIJAYA 1 3) Display THE NUMBER OF male and female programmer. SELECT SEX,COUNT(NAME) AS NAME FROM PROGRAMMER GROUP BY SEX Output: S NAME - --------F 7 Page 42

SQL_Queries

4) Display THE COSTLIEST packages and HIGEST selling developed in EACH language. SELECT DEV_IN AS LANGAUGE,MAX(SCOST) AS COSTPACK,MAX(SOLD) AS HIGHPACK SFROM SOFTWARE GROUP BY DEV_IN Output: LANGAUGE COSTPACK HIGHPACK -------- ---------- ---------ASSEMBLY 2500 114 BASIC 3200 43 C 4500 51 C++ 300 84 COBOL 3000 0 DBASE 12000 7 JAVA 800 10 ORACLE 18000 14 PASCAL 7500 73 5) Display THE NUMBER OF people BORN in EACH YEAR. Page 43

SQL_Queries SELECT TO_CHAR(DOB,'YY') AS YEAR,COUNT(NAME) AS PRNO FROM PROGRAMMER GROUP BY TO_CHAR(DOB,'YY') Output: YE PRNO -- ---------64 1 65 4 66 1 67 2 68 2 69 1 70 3 6) Display THE NUMBER OF people JOINED in EACH YEAR. SELECT TO_CHAR(DOJ,'YY') AS YEAR,COUNT(NAME) AS PRNO FROM PROGRAMMER GROUP BY TO_CHAR(DOJ,'YY') Output: YE PRNO -- ---------89 1 90 4 91 2 92 4 93 2 94 1

Page 44

SQL_Queries

7) Display THE NUMBER OF people BORN in EACH MONTH. SELECT SUBSTR(DOB,4,3) AS MONTHOFBIRTH,COUNT(NAME) AS PRNO FROM PROGRAMMER GROUP BY SUBSTR(DOB,4,3) Output: MON PRNO --- ---------APR 2 AUG 1 DEC 2 JAN 2 JUL 1 JUN 1 MAY 1 NOV 1 OCT 2 SEP 1

Page 45

SQL_Queries 8) Display THE NUMBER OF people JOINED in EACH MONTH. SELECT SUBSTR(DOJ,4,3) AS MONTHOFJOIN,COUNT(NAME) AS PRNO FROM PROGRAMMER GROUP BY SUBSTR(DOJ,4,3) Output: MON PRNO --- ---------APR 5 DEC 1 FEB 2 JAN 2 MAY 1 NOV 1 OCT 2 9) Display the language wise COUNTS of prof1. SELECT PROF1 AS LANGUAGE, COUNT(PROF1) PROF1COUNT FROM PROGRAMMER GROUP BY PROF1 Output: LANGUAGE PROF1COUNT -------- ---------Assembly 1 Basic 1 C 2 C++ 1 AS

Page 46

Clipper Cobol Foxpro Oracle Pascal

SQL_Queries 1 2 1 1 4

10) Display the language wise COUNTS of prof2. SELECT PROF2 AS LANGUAGE, COUNT(PROF2) PROF2COUNT FROM PROGRAMMER GROUP BY PROF2 Output: LANGUAGE PROF2COUNT -------- ---------Assembly 1 Basic 2 C 2 Clipper 1 Cobol 2 Dbase 4 Java 1 Oracle 1 Page 47 AS

SQL_Queries 11) Display THE NUMBER OF people in EACH salary group. SELECT SALARY,COUNT(NAME) AS PEOPLE FROM PROGRAMMER GROUP BY SALARY Output: SALARY PEOPLE ---------- ---------2500 2 2800 2 2900 1 3000 2 3200 2 3500 1 3600 1 3700 1 4100 1 4500 1 12) Display THE NUMBER OF people who studied in EACH institute. SELECT SPLACE AS INSTITUTE,COUNT(NAME) AS PEOPLE FROM STUDIES GROUP BY SPLACE Output: INSTITUTE PEOPLE --------- ---------APPLE 1 BDPS 2 Page 48

BITS BRILLIANT CCIT PRAGATHI S.S.I.L SABHARI

SQL_Queries 1 1 1 3 1 4

13) Display THE NUMBER OF people who studied in EACH course. SELECT COURSE AS STUDY,COUNT(NAME) AS PEOPLE FROM STUDIES GROUP BY COURSE Output: STUDY PEOPLE ----- ---------DAP 2 DCA 4 DCA/P 1 DCAP 1 DCP 1 DCS 1 HDCP 1 PGDCA 3 14) Display the TOTAL development COST of the Page 49

SQL_Queries packages developed in EACH language. SELECT DEV_IN AS LANGUAGE,SUM(DCOST) AS TOTCOST FROM SOFTWARE GROUP BY DEV_IN Output: LANGUAGE TOTCOST -------- ---------ASSEMBLY 6530 BASIC 8100 C 29100 C++ 1200 COBOL 3500 DBASE 55000 JAVA 7500 ORACLE 97000 PASCAL 100500 15) Display the selling cost of the package developed in EACH language. SELECT DEV_IN AS LANGUAGE,SUM(SCOST) AS SELLCOST FROM SOFTWARE GROUP BY DEV_IN Output: LANGUAGE SELLCOST -------- ---------ASSEMBLY 2999.95 BASIC 3599.95 C 8025

Page 50

C++ COBOL DBASE JAVA ORACLE PASCAL

SQL_Queries 300 3000 21000 800 21000 9449.95

16) Display the cost of the package developed by EACH programmer. SELECT NAME AS PRNAME,SUM(DCOST) AS TOTCOST FROM SOFTWARE GROUP BY NAME Output: PRNAME TOTCOST -------- ---------ANAND 22000 JAGADESH 19500 JULIANA 3500 KAMALA 20000 MARY 106200 PATRICK 5000 QADIR 3930 RAMESH 39500

Page 51

REMITHA REVATHI VIJAYA

SQL_Queries 11000 77100 700

17) Display the sales values of the package developed in EACH programmer. SELECT NAME AS PRNAME, SUM(SCOST*SOLD) AS SALESVAL FROM SOFTWARE GROUP BY NAME Output: PRNAME SALESVAL -------- ---------ANAND 84697.85 JAGADESH 50000 JULIANA 0 KAMALA 63000 MARY 200700 PATRICK 8250 QADIR 96894.3 RAMESH 55296.35 REMITHA 51975 REVATHI 50200 SOMDUTT 5400 18) Display the NUMBER of packages developed by EACH programmer. SELECT NAME AS PRNAME,COUNT(TITLE) AS TOTPACK FROM SOFTWARE GROUP BY NAME Page 52

Output: PRNAME TOTPACK -------- ---------ANAND 2 JAGADESH 2 JULIANA 1 KAMALA 1 MARY 3 SOMDUTT 1 NIHAR 2 RAMESH 2 REMITHA 2 REVATHI 2 VIJAYA 1

SQL_Queries

19) Display the sales COST of packages developed by EACH programmer language wise. SELECT SUM(SCOST) AS SELLCOST FROM SOFTWARE GROUP BY DEV_IN Output: SELLCOST ---------2999.95 3599.95 8025 300 3000 21000

Page 53

800 21000 9449.92

SQL_Queries

20) Display EACH programmers name, costliest package and cheapest packages developed by Him/Her. SELECT NAME PRNAME,MIN(DCOST) CHEAPEST,MAX(DCOST) COSTLIEST FROM SOFTWARE GROUP BY NAME Output: PRNAME CHEAPEST COSTLIEST -------- ---------- ---------ANAND 6000 16000 JAGADESH 7500 12000 JULIANA 3500 3500 KAMALA 20000 20000 MARY 1200 85000 PATRICK 5000 5000 QADIR 530 3400 RAMESH 4500 35000 REMITHA 5000 6000 REVATHI 2100 75000 VIJAYA 700 700 21) Display EACH language name with AVERAGE development cost, AVERAGE cost, selling cost and AVERAGE price per copy. SELECT DEV_IN AS LANGUAGE,AVG(DCOST) AS AVGDEVCOST,AVG(SCOST) AS Page 54

SQL_Queries AVGSELLCOST,AVG(SCOST) AS PRICEPERCPY FROM SOFTWARE GROUP BY DEV_IN Output: LANGUAGE AVGDEVCOST AVGSELLCOST PRICEPERCPY -------- ---------- ----------- ----------ASSEMBLY 3265 1499.975 1499.975 BASIC 4050 1799.975 1799.975 C 7275 2006.25 2006.25 C++ 1200 300 300 COBOL 3500 3000 3000 DBASE 27500 10500 10500 JAVA 7500 800 800 ORACLE 48500 10500 10500 PASCAL 25125 2362.4875 2362.4875

22) Display EACH institute name with NUMBER of courses, AVERAGE cost per course. SELECT SPLACE AS INSTITUTE,COUNT(COURSE) AS NOOFCOURS,AVG(CCOST) AS AVGCOSTPERCOUR FROM STUDIES GROUP BY SPLACE Output: INSTITUTE NOOFCOURS AVGCOSTPERCOUR --------- ---------- -------------APPLE 1 14000 BDPS 2 27000 Page 55

BITS BRILLIANT CCIT PRAGATHI S.S.I.L SABHARI

SQL_Queries 1 22000 1 11000 1 7200 3 5466.66667 1 3500 4 4625

23) Display EACH institute name with NUMBER of students. SELECT SPLACE AS INSTITUTE,COUNT(NAME) AS NOOFSTUD FROM STUDIES GROUP BY SPLACE Output: INSTITUTE NOOFSTUD --------- ---------APPLE 1 BDPS 2 BITS 1 BRILLIANT 1 CCIT 1 PRAGATHI 3 S.S.I.L 1 SABHARI 4 24) Display names of male and female programmers. SELECT NAME AS PRNAME,SEX AS SEX FROM PROGRAMMER ORDER BY SEX Page 56

Output: PRNAME -------JULIANA KAMALA MARY REMITHA VIJAYA REVATHI REBECCA ANAND PATRICK RAMESH QADIR NELSON ALTAF JAGADESH

SQL_Queries S F F F F F F F M M M M M M M

25) Display the programmer's name and their packages. SELECT NAME AS PRNAME,TITLE AS PACKAGE FROM SOFTWARE ORDER BY NAME Output: PRNAME -------ANAND ANAND PACKAGE -------------------PARACHUTES VIDEO TITLING PACK Page 57

SQL_Queries JAGADESH SERIAL LINK UTILITY JAGADESH SHARES MANAGEMENT JULIANA INVENTORY CONTROL KAMALA PAYROLL PACKAGE MARY FINANCILA ACC S/W MARY CODE GENERATOR MARY READ ME PATRICK GRAPHIC EDITOR QADIR BOMBS AWAY QADIR VACCINES RAMESH HOTEL MANAGEMENT RAMESH DEAD LEE REMITHA PC UTILITIES REMITHA TSR HELP PACKAGE REVATHI HOTEL MANAGEMENT REVATHI QUIZ MASTER VIJAYA ISK EDITOR 26) Display the NUMBER of packages in EACH language. SELECT COUNT(TITLE) AS NOOFPACK,DEV_IN AS LANGUAGE FROM SOFTWARE GROUP BY DEV_IN Output: NOOFPACK ---------2 2 4 1 1 LANGUAGE -------ASSEMBLY BASIC C C++ COBOL

Page 58

2 1 2 4

DBASE JAVA ORACLE PASCAL

SQL_Queries

27) Display the NUMBER of packages in EACH language for which development cost is less than 1000. SELECT COUNT(TITLE) AS NOOFPACK,DEV_IN AS LANGUAGE FROM SOFTWARE WHERE DCOST<1000 GROUP BY DEV_IN Output: NOOFPACK ---------1 1 LANGUAGE -------ASSEMBLY C

28) Display the AVERAGE difference BETWEEN scost and dcost for EACH language. SELECT DEV_IN AS LANGUAGE,AVG(DCOST - SCOST) AS DIFF FROM SOFTWARE GROUP BY DEV_IN Output: Page 59

AVG(DCOST-SCOST) ---------------1765.025 2250.025 5268.75 900 500 17000 6700 38000 22762.52

SQL_Queries

29) Display the TOTAL scost, dcsot and amount TOBE recovered for EACH programmer for whose dcost HAS NOT YET BEEN recovered. SELECT SUM(SCOST), SUM(DCOST), SUM(DCOST-(SOLD*SCOST)) FROM SOFTWARE GROUP BY NAME HAVING SUM(DCOST)>SUM(SOLD*SCOST) Output: SUM(SCOST) SUM(DCOST) SUM(DCOST-(SOLD*SCOST)) ---------- ---------- ----------------------3000 3500 3500 4300 77100 26900 30) Display highest, lowest and average salaries for THOSE earning MORE than 2000. SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM PROGRAMMER WHERE SALARY > 2000 Page 60

SQL_Queries Output: MAX(SALARY) MIN(SALARY) AVG(SALARY) ----------- ----------- ----------4500 2500 3235.7143 QUERIES - III 1) Who is the highest paid C programmer? SELECT * FROM PROGRAMMER WHERE SALARY=(SELECT MAX(SALARY) FROM PROGRAMMER WHERE PROF1 LIKE 'C' OR PROF2 LIKE 'C') Output: NAME DOB DOJ PROF2 S PROF1 SALARY -------- --------- --------- - --------------- --------REMITHA 19-APR-70 20-APR-93 F C ASSEMBLY 3600 2) Who is the highest paid female cobol programmer? SELECT * FROM PROGRAMMER WHERE SALARY=(SELECT MAX(SALARY) FROM PROGRAMMER WHERE (PROF1 LIKE 'COBOL' OR PROF2 LIKE 'COBOL')) AND SEX LIKE 'F' Page 61

SQL_Queries Output: NAME DOB DOJ PROF2 S PROF1 SALARY -------- --------- --------- - --------------- --------JULIANA 31-JAN-68 21-APR-90 F COBOL DBASE 3000 3) Display the name of the HIGEST paid programmer for EACH language (prof1) SELECT DISTINCT NAME, SALARY, PROF1 FROM PROGRAMMER WHERE (SALARY,PROF1) IN (SELECT MAX(SALARY),PROF1 FROM PROGRAMMER GROUP BY PROF1) Output: NAME SALARY PROF1 -------- --------- -------REBECCA 2500 BASIC ALTAF 2800 CLIPPER DADIR 3000 ASSEMBLY JULIANA 3000 COBOL VIJAYA 3500 FOXPRO REMITHA 3600 C REVATHI 3700 PASCAL JAGADESH 4100 ORACLE MARY 4500 C++ 4) Who is the LEAST experienced programmer? Page 62

SQL_Queries SELECT FLOOR((SYSDATE-DOJ)/365) EXP,NAME FROM PROGRAMMER WHERE FLOOR((SYSDATE-DOJ)/365) = (SELECT MIN(FLOOR((SYSDATE-DOJ)/365)) FROM PROGRAMMER) Output: EXP NAME --------- -------5 JAGADESH

5) Who is the MOST experienced programmer? SELECT FLOOR((SYSDATE-DOJ)/365) EXP,NAME,PROF1,PROF2 FROM PROGRAMMER WHERE FLOOR((SYSDATE-DOJ)/365) = (SELECT MAX(FLOOR((SYSDATE-DOJ)/365)) FROM PROGRAMMER) AND (PROF1 LIKE 'COBOL' OR PROF2 LIKE 'COBOL') Output: EXP NAME PROF1 PROF2 --------- -------- -------- -------10 NELSON COBOL DBASE 6) Which language is known by ONLY ONE programmer? SELECT PROF1 Page 63

FROM PROGRAMMER GROUP BY PROF1 HAVING PROF1 NOT IN (SELECT PROF2 FROM PROGRAMMER) AND COUNT(PROF1)=1 UNION SELECT PROF2 FROM PROGRAMMER GROUP BY PROF2 HAVING PROF2 NOT IN (SELECT PROF1 FROM PROGRAMMER) AND COUNT(PROF2)=1; Output: PROF1 -------C++ Foxpro Java 7) Who is the YONGEST programmer knowing DBASE? SELECT FLOOR((SYSDATE-DOB)/365) AGE, NAME, PROF1, PROF2 FROM PROGRAMMER WHERE FLOOR((SYSDATE-DOB)/365) = (SELECT MIN(FLOOR((SYSDATE-DOB)/365)) FROM PROGRAMMER WHERE PROF1 LIKE 'DBASE' OR PROF2 LIKE 'DBASE') Output: AGE NAME PROF1 PROF2 Page 64

SQL_Queries

SQL_Queries ---- -------- -------- -------DBASE 31 JULIANA COBOL 31 KAMALA C DBASE 8) Which institute has MOST NUMBER of students? SELECT SPLACE FROM STUDIES GROUP BY SPLACE HAVING COUNT(SPLACE)= (SELECT MAX(COUNT(SPLACE)) FROM STUDIES GROUP BY SPLACE) Output: SPLACE --------SABHARI

9) Who is the above programmer? SELECT NAME FROM PROGRAMMER WHERE PROF1 IN (SELECT PROF1 FROM PROGRAMMER GROUP BY PROF1 HAVING PROF1 NOT IN (SELECT PROF2 FROM PROGRAMMER) AND COUNT(PROF1)=1 UNION SELECT PROF2 FROM PROGRAMMER Page 65

GROUP BY PROF2 HAVING PROF2 NOT IN (SELECT PROF1 FROM PROGRAMMER) AND COUNT(PROF2)=1)) UNION SELECT NAME FROM PROGRAMMER WHERE PROF2 IN (SELECT PROF1 FROM PROGRAMMER GROUP BY PROF1 HAVING PROF1 NOT IN (SELECT PROF2 FROM PROGRAMMER) AND COUNT(PROF1)=1 UNION SELECT PROF2 FROM PROGRAMMER GROUP BY PROF2 HAVING PROF2 NOT IN (SELECT PROF1 FROM PROGRAMMER) AND COUNT(PROF2)=1)) Output: NAME -------JAGADESH MARY VIJAYA 10) Which female programmer earns MORE than 3000/- but DOES NOT know C, C++, Oracle or Dbase? SELECT * FROM PROGRAMMER Page 66

SQL_Queries

SQL_Queries WHERE SEX LIKE 'F' AND SALARY >3000 AND (PROF1 NOT IN('C','C++','ORACLE','DBASE') OR PROF2 NOT IN('C','C++','ORACLE','DBASE')) Output: NAME DOB SALARY -------- ---------------- --------REMITHA 19-APR-70 ASSEMBLY 3600 REVATHI 02-DEC-69 3700 VIJAYA 14-DEC-65 3500 DOJ S PROF1 PROF2

--------- - -------20-APR-93 F C 12-JAN-92 F PASCAL 12-MAY-92 F FOXPRO BASIC C

11) Which is the COSTLIEST course? SELECT COURSE FROM STUDIES WHERE CCOST = (SELECT MAX(CCOST) FROM STUDIES) Output: COURS ----DCA

12) Which course has been done by MOST of the students? SELECT COURSE Page 67

FROM STUDIES GROUP BY COURSE HAVING COUNT(COURSE)= (SELECT MAX(COUNT(COURSE)) FROM STUDIES GROUP BY COURSE) Output: COURS ----DCA 13) Display name of the institute and course Which has below AVERAGE course fee? SELECT SPLACE,COURSE FROM STUDIES WHERE CCOST < (SELECT AVG(CCOST) FROM STUDIES) Output: SPLACE --------SABHARI SABHARI CCIT S.S.I.L. PRAGATHI SABHARI PRAGATHI PRAGATHI SABHARI BDPS SABHARI COURS ----PGDCA PGDCA DCA DCA DCP PGDCA DAP DCAP PGDCA DCS DAP Page 68

SQL_Queries

SQL_Queries 14) Which institute conducts COSTLIEST course? SELECT SPLACE FROM STUDIES WHERE CCOST = (SELECT MAX(CCOST) FROM STUDIES) Output: SPLACE --------BDPS 15) Which course has below AVERAGE number of students? SELECT COURSE FROM STUDIES HAVING COUNT(NAME)<(SELECT AVG(COUNT(NAME)) FROM STUDIES GROUP BY COURSE) GROUP BY COURSE; Output: COURS ----DCA/P DCAP DCP DCS HDCP

16) Which institute conducts the above course? Page 69

SELECT SPLACE FROM STUDIES WHERE COURSE IN (SELECT COURSE FROM STUDIES HAVING COUNT(NAME) < (SELECT AVG(COUNT(NAME)) FROM STUDIES GROUP BY COURSE) GROUP BY COURSE); Output: SPLACE --------BRILLIANT PRAGATHI PRAGATHI BDPS APPLE 17) Display names of the course WHOSE fees are within 1000(+ or -) of the AVERAGE fee. SELECT COURSE FROM STUDIES WHERE CCOST < (SELECT AVG(CCOST)+1000 FROM STUDIES) AND CCOST > (SELECT AVG(CCOST)-1000 FROM STUDIES) Output: COURS ----DCA/P Page 70

SQL_Queries

SQL_Queries 18) Which package has the HIGEST development cost? SELECT TITLE,DCOST FROM SOFTWARE WHERE DCOST = (SELECT MAX(DCOST) FROM SOFTWARE) Output: TITLE DCOST -------------------- --------FINANCILA ACC S/W 85000 19) Which package has the LOWEST selling cost? SELECT TITLE,SCOST FROM SOFTWARE WHERE SCOST = (SELECT MIN(SCOST) FROM SOFTWARE) Output: TITLE SCOST -------------------- --------DEAD LEE 99.92 20) Who developed the package, which has sold the LEAST number of copies? SELECT NAME,SOLD FROM SOFTWARE WHERE SOLD = (SELECT MIN(SOLD) FROM SOFTWARE) Output: Page 71

SQL_Queries NAME SOLD -------- --------JULIANA 0 21) Which language was used to develop the package WHICH has the HIGEST sales amount? SELECT DEV_IN,SCOST FROM SOFTWARE WHERE SCOST = (SELECT MAX(SCOST) FROM SOFTWARE) Output: DEV_IN SCOST -------- --------ORACLE 18000 22) How many copies of the package that has the LEAST DIFFRENCE between development and selling cost were sold? SELECT SOLD,TITLE FROM SOFTWARE WHERE TITLE = (SELECT TITLE FROM SOFTWARE WHERE (DCOST-SCOST)=(SELECT MIN(DCOST-SCOST) FROM SOFTWARE)) Output: SOLD TITLE ---- -------------------15 QUIZ MASTER 23) Which is the COSTLIEAST package developed Page 72

in PASCAL?

SQL_Queries

SELECT TITLE FROM SOFTWARE WHERE DCOST = (SELECT MAX(DCOST) FROM SOFTWARE WHERE DEV_IN LIKE 'PASCAL') Output: TITLE -------------------HOTEL MANAGEMENT 24) Which language was used to develop the MOST NUMBER of package? SELECT DEV_IN FROM SOFTWARE GROUP BY DEV_IN HAVING MAX(DEV_IN) = (SELECT MAX(DEV_IN) FROM SOFTWARE) Output: DEV_IN -------PASCAL 25) Which programmer has developed the HIGEST NUMBER of package? SELECT NAME FROM SOFTWARE GROUP BY NAME HAVING MAX(NAME) = (SELECT MAX(NAME) FROM SOFTWARE) Page 73

Output: NAME -------VIJAYA

SQL_Queries

26) Who is the author of the COSTLIEST package? SELECT NAME,DCOST FROM SOFTWARE WHERE DCOST = (SELECT MAX(DCOST) FROM SOFTWARE) Output: NAME DCOST -------- --------MARY 85000 27) Display names of packages WHICH have been sold LESS THAN the AVERAGE number of copies? SELECT TITLE FROM SOFTWARE WHERE SOLD < (SELECT AVG(SOLD) FROM SOFTWARE) Output: TITLE -------------------VIDEO TITLING PACK SERIAL LINK UTILITY Page 74

SHARES MANAGMENT INVENTORY CONTROL PAYROLL PACKAGE FINANCILA ACC S/W CODE GENERATOR GRAPHIC EDITOR VACCINES HOTEL MANAGEMENT TSR HELP PACKAGE HOTEL MANAGEMENT QUIZ MASTER ISK EDITOR

SQL_Queries

28) Who are the female programmers earning MORE than the HIGEST paid male programmers? SELECT NAME FROM PROGRAMMER WHERE SEX LIKE 'F' AND SALARY > (SELECT(MAX(SALARY)) FROM PROGRAMMER WHERE SEX LIKE 'M') Output: NAME -------MARY 29) Which language has been stated as prof1 by MOST of the programmers? SELECT PROF1 FROM PROGRAMMER GROUP BY PROF1

Page 75

SQL_Queries HAVING PROF1 = (SELECT MAX(PROF1) FROM PROGRAMMER) Output: PROF1 -------PASCAL

30) Who are the authors of packages, WHICH have recovered MORE THAN double the development cost? SELECT NAME distinct FROM SOFTWARE WHERE SOLD*SCOST > 2*DCOST Output: NAME -------ANAND JAGDESH KAMALA MARY QADIR REMITHA REVATHI VIJAYA 31) Display programmer names and CHEAPEST package developed by them in EACH language? Page 76

SELECT NAME,TITLE FROM SOFTWARE WHERE DCOST IN (SELECT MIN(DCOST) FROM SOFTWARE GROUP BY DEV_IN) Output: NAME -------QADIR VIJAYA MARY REVATHI JULIANA RAMESH JAGADESH JAGDESH KAMALA MARY TITLE -------------------BOMBOS AWAY ISK EDITOR READ ME QUIZ MASTER INVENTORY CONTROL DEAD LEE SERIAL LINK UTILITY SHARES MANAGMENT PAYROLL PACKAGE CODE GENERATOR

SQL_Queries

32) Who is the YOUNGEST male programmer born in 1965? SELECT NAME FROM PROGRAMMER WHERE DOB=(SELECT (MAX(DOB)) FROM PROGRAMMER WHERE TO_CHAR(DOB,'YYYY') LIKE '1965') Output: NAME -------VIJAYA

Page 77

SQL_Queries

33) Display language used by EACH programmer to develop the HIGEST selling and LOWEST selling package. SELECT NAME, DEV_IN FROM SOFTWARE WHERE SOLD IN (SELECT MAX(SOLD) FROM SOFTWARE GROUP BY NAME) UNION SELECT NAME, DEV_IN FROM SOFTWARE WHERE SOLD IN (SELECT MIN(SOLD) FROM SOFTWARE GROUP BY NAME) Output: NAME -------ANAND ANAND JAGADESH JAGDESH DEV_IN -------BASIC PASCAL JAVA ORACLE

Page 78

JULIANA KAMALA MARY MARY PATRICK QADIR QADIR RAMESH RAMESH REMITHA REMITHA REVATHI REVATHI VIJAYA

COBOL DBASE C++ ORACLE PASCAL ASSEMBLY C DBASE PASCAL ASSEMBLY C BASIC PASCAL C

SQL_Queries

34) Who is the OLDEST female programmer WHO joined in 1992 SELECT NAME FROM PROGRAMMER WHERE DOJ=(SELECT (MIN(DOJ)) FROM PROGRAMMER WHERE TO_CHAR(DOJ,'YYYY') LIKE '1992') Output: NAME -------KAMALA REVATHI 35) In WHICH year where the MOST NUMBER of programmer born? SELECT DISTINCT TO_CHAR(DOB,'YYYY') Page 79

FROM PROGRAMMER WHERE TO_CHAR(DOJ,'YYYY') = (SELECT MIN(TO_CHAR(DOJ,'YYYY')) FROM PROGRAMMER) Output: TO_C ---1965

SQL_Queries

36) In WHICH month did MOST NUMBRER of programmer join? SELECT DISTINCT TO_CHAR(DOJ,'MONTH') FROM PROGRAMMER WHERE TO_CHAR(DOJ,'MON') = (SELECT MIN(TO_CHAR(DOJ,'MON')) FROM PROGRAMMER) Output: TO_CHAR(D --------APRIL 37) In WHICH language are MOST of the programmer's proficient? SELECT PROF1 FROM PROGRAMMER GROUP BY PROF1 HAVING COUNT(PROF1)=(SELECT MAX(COUNT(PROF1)) Page 80

FROM PROGRAMMER GROUP BY PROF1) OR COUNT(PROF2)=(SELECT MAX(COUNT(PROF2)) FROM PROGRAMMER GROUP BY PROF2) UNION SELECT PROF2 FROM PROGRAMMER GROUP BY PROF2 HAVING COUNT(PROF1)=(SELECT MAX(COUNT(PROF1)) FROM PROGRAMMER GROUP BY PROF1) OR COUNT(PROF2)=(SELECT MAX(COUNT(PROF2)) FROM PROGRAMMER GROUP BY PROF2) Output: PROF1 -------DBASE PASCAL 38) Who are the male programmers earning BELOW the AVERAGE salary of female programmers? SELECT NAME FROM PROGRAMMER WHERE SEX LIKE 'M' AND SALARY < (SELECT(AVG(SALARY)) FROM PROGRAMMER WHERE SEX LIKE 'F') Output: NAME

SQL_Queries

Page 81

-------ANAND ALTAF NELSON PATRICK DADIR RAMESH

SQL_Queries

QUERY - IV 1) Display the details of THOSE WHO are drawing the same salary. select a.name,a.salary from programmer a,programmer b where a.salary=b.salary and a.name <> b.name OR where salary = any(select salary from programmer p group by salary having salary=p.salary and count(*)>1) 2) Display the details of software developed by male programmers earing MORE than 3000. select software.* from programmer p,software s where p.name=s.name and salary>3000 Page 82 select name, salary from programmer

and sex='m';

SQL_Queries

3) Display details of packages developed in PASCAL by female programmers. select s.* from programmer p,software s where p.name=s.name and sex='f' and dev_in='pascal'; 4) Display the details of these programmer WHO joined BEFORE 1990. select * from programmer where to_char(doj,'yy')<90; 5)Display details of software developed in C by female programmers of PRAGATHI. select s.* from software s,studies st,programmer p where s.name=st.name and p.name=s.name and sex='f' and splace='pragathi'; 6) Display NUMBER of packages NUMBER of copies sold and sales value of EACH programmer Institute-wise. Select studies.splace, count(software.dev_in), count(software.sold), sum(software.sold*software.scost) from software,studies where software.name=studies.name group by studies.splace; Page 83

SQL_Queries

7) Display details of software developed in DBASE by male programmers WHO belong to the institute on which MOST NUMBER OF programmers studies. select software.* from programmer,software,studies where programmer.name=software.name and software.name=studies.name and programmer.name=studies.name and sex='m' and dev_in='dbase' and splace= (select splace from studies group by splace having count(splace) =(select max(count(splace)) from studies group by splace)); 8) Display the details of the software that was developed by male programmers born BEFORE 1965 and female programmers born AFTER 1975. select software.* from programmer p,software s where s.name=p.name and sex='m' and to_char(dob,'yy')<64 or sex='f' and To_char(dob,'yy')>75); 9) Display the details of the software that was developed in the language that is NOT the programmers first proficiency. select * from software where dev_in in(select unique(prof2) Page 84

SQL_Queries from programmer where prof2 not in(select prof1 from programmer)); or select distinct x.* from software x, programmer y where y.prof1 <> x.dev_in and x.name = y.name 10) Display details of software developed in the language which first NOR second proficiency of programmer. select s.* from programmer p,software where s.name=p.name and (dev_in dev_in <> prof2); that was is NITHER the s <> prof1 and

11) Display details of software developed by male students of SABHARI. select s.* from programmer p,software s,studies st where p.name=s.name and s.name=st.name and sex='m' and splace='sabhari'; 12) Display the names of programmers WHO HAVE NOT developed any package. select name from programmer where name not in(select name from software); Page 85

or

SQL_Queries

select distinct name from programmer minus select distinct name from software; 13) What is the total cost of the software developed by the programmers by APPLE? select sum(scost) from software s,studies st where s.name=st.name and splace='apple'; or select sum(x.scost) from software x, studies y where x.name=y.name group by y.splace having y.splace = 'APPLE' 14) Who are the programmers WHO JOINED in the same day? select a.name,a.doj from programmer a,programmer b where a.doj=b.doj and a.name <> b.name; or select name from programmer where to_char(doj,'dd')= any(select to_char(doj,'dd') from programmer group by to_Char(doj,'dd') having count(*)>1) Page 86

SQL_Queries 15) Who are the programmers WHO HAVE THE SAME PROF2? select unique(a.name),a.prof2 from programmer a,programmer b where a.prof2=b.prof2 and a.name <> b.name; or select name from programmer where prof2 = any( select prof2 from programmer group by prof2 having count(*) >1); 16) Display the total sales values of software, institutes-wise. select studies.splace,sum(software.sold*software.scos t) from software,studies where studies.name=software.name group by studies.splace; 17) In which institutes did the person who developed the COSTLIEST package study? select splace from software st,studies s where s.name=st.name group by splace,dcost having max(dcost)=(select max(dcost) from software); or select x.splace from studies x, software y Page 87

where y.scost = ( select max(y.scost) from software y) and x.name=y.name; 18) Which language listed in prof1 and prof2 HAS NOT BEEN used to develop any package? select prof1 from programmer where prof1 not in(select dev_in from software) union select prof2 from programmer where prof2 not in(select dev_in from software); or (select distinct prof1 from prgrammer union select distinct prof2 from programmer) minus select distinct dev_in from software; 19) How much does the person WHO developed the HIGHEST selling package earn and WHAT course did he/she undergo? select p1.salary,s2.course from programmer p1,software s1,studies s2 where p1.name=s1.name and s1.name=s2.name and scost=(select max(scost) from software); 20) How many months will it take for each Page 88

SQL_Queries

SQL_Queries programmer to recover the cost of the course underwent? select p.name,ceil(ccost/salary) from programmer p,studies s where s.name=p.name; 21) Which is the COSTLIEST package developed by a person with under 3 years expenence? select dev_in from programmer p,software s where p.name=s.name and dcost= (select max(software.dcost) from programmer p, software s where p.name=s.name and to_char(round(((sysdatedoj)/365)+100))<3); or select x.title from software x, programmer y where (months_between(sysdate, y.doj)/12) > 3 and x.name=y.name; 22) What is the AVERAGE salary for those WHOSE software's sales value is more than 50,000? select avg(salary) from programmer p,software s where p .name=s.name and sold*scost>50000; 23) How many packages were developed by the students WHO studied in the institute that Page 89

SQL_Queries Charge the LOWEST course fee? select count(s.name) from software s,studies st where s.name=st.name group by s.name,ccost having min(ccost)=(select min(ccost) from studies); 24) How many packages were developed by the person WHO developed the CHEAPEST package. Where did he\she study? select count(*) from programmer p,software s where s .name=p.name group by dev_in having min(dcost)=(select min(dcost) from software); 25) How many packages were developed by female programmers earning MORE than the HIGHEST paid male programmer? select count(dev_in) from programmer p,software s where s.name=p.name and sex='f' and salary>(select max(salary) from programmer p,software s where s.name=p.name and sex='m'); 26) How many packages were developed by the MOST experienced programmers from BDPS. select count(*) from software s,programmer p where p.name=s.name group by doj having max(doj)=(select max(doj) from studies st,programmer p, software s Page 90

SQL_Queries where p.name=s.name and st.name=p.name and (splace='bdps')); or select count(x.name) from software x, programmer y, studies x where months_between(sysdate, y.doj)/12) = (select max(months_between(sysdate,y.doj)/12) from programmer y, studies = where x.splace = 'BDPS' and y.name = z.name) and x.name=y.name and z.splace='BDPS' 27) List the programmers (from software table) and institutes they studied, including those WHO DIDN'T develop any package. select name,splace from studies where name not in(select name from software); or (select distinct x.name, z.splace from programmer x, software y, studies z where x.name not in (select y.name from software y) and x.name = z.name) union (select distinct y.name, z.splace from software y, studies z where y.name=z.name); 28) List each profit with the number of Page 91

SQL_Queries programmers having that prof1 and the number of packages developed in that prof1. select count(*),sum(scost*sold-dcost) "PROFIT" from software where dev_in in (select prof1 from programmer) group by dev_in; 29) List programmer names (from programmer table) and number of packages EACH developed. select s.name,count(dev_in) from programmer p1,software s where p1.name=s.name group by s.name; or select programmer name, count(title) from programmer , software where programmer name = software.name(+) group by programmer.name; 30) List all the details of programmers who has done a course at S.S.I.L. select programmer.* from programmer,studies where splace='SSIL' and programmer.name=software.name and programmer.name=studies.name and studies.splace='s.s.i.l.';

Page 92

Potrebbero piacerti anche