0% found this document useful (0 votes)
114 views6 pages

SQL Database and Query Examples

The document contains SQL statements that create and populate tables with data about studies, software, and programmers. The tables are then queried to analyze and summarize the data. Some key points extracted: - Tables are created to store data about studies, software projects, and programmer details. - Various select statements are used to query the tables and analyze relationships between the data, such as highest sales, languages used, courses taken, etc. - Joins are performed to combine data from the tables to analyze things like what courses programmers took and packages developed by each programmer.

Uploaded by

Prashant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views6 pages

SQL Database and Query Examples

The document contains SQL statements that create and populate tables with data about studies, software, and programmers. The tables are then queried to analyze and summarize the data. Some key points extracted: - Tables are created to store data about studies, software projects, and programmer details. - Various select statements are used to query the tables and analyze relationships between the data, such as highest sales, languages used, courses taken, etc. - Joins are performed to combine data from the tables to analyze things like what courses programmers took and packages developed by each programmer.

Uploaded by

Prashant Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

CREATE TABLE studies (PNAME varchar(20), INSTITUTE varchar(20), COURSE varchar(20),

COURSE_FEE int )
GO

SELECT * FROM studies

INSERT INTO studies


SELECT 'ANAND','SABHARI','PGDCA',4500 UNION ALL
SELECT 'ALTAF','COIT','DCA',7200 UNION ALL
SELECT 'JULIANA','BDPS','MCA',22000 UNION ALL
SELECT 'KAMALA','PRAGATHI','DCA',5000 UNION ALL
SELECT 'MARY','SABHARI','PGDCA ',4500 UNION ALL
SELECT 'NELSON','PRAGATHI','DAP',6200 UNION ALL
SELECT 'PATRICK','PRAGATHI','DCAP',5200 UNION ALL
SELECT 'QADIR','APPLE','HDCA',14000 UNION ALL
SELECT 'RAMESH','SABHARI','PGDCA',4500 UNION ALL
SELECT 'REBECCA','BRILLIANT','DCAP',11000 UNION ALL
SELECT 'REMITHA','BDPS','DCS',6000 UNION ALL
SELECT 'REVATHI','SABHARI','DAP',5000 UNION ALL
SELECT 'VIJAYA','BDPS','DCA',48000
GO

CREATE TABLE software (PNAME varchar(20), TITLE varchar(20), DEVELOPIN varchar(20),


SCOST decimal(10,2), DCOST int, SOLD int)
GO

SELECT * FROM software

INSERT INTO software


SELECT 'MARY','README','CPP',300, 1200, 84 UNION ALL
SELECT 'ANAND','PARACHUTES','BASIC',399.95, 6000, 43 UNION ALL
SELECT 'ANAND','VIDEO TITLING','PASCAL',7500, 16000, 9 UNION ALL
SELECT 'JULIANA','INVENTORY','COBOL',3000, 3500, 0 UNION ALL
SELECT 'KAMALA','PAYROLL PKG.','DBASE',9000, 20000, 7 UNION ALL
SELECT 'MARY','FINANCIAL ACCT.','ORACLE',18000, 85000, 4 UNION ALL
SELECT 'MARY','CODE GENERATOR','C',4500, 20000, 23 UNION ALL
SELECT 'PATTRICK','README','CPP',300, 1200, 84 UNION ALL
SELECT 'QADIR','BOMBS AWAY','ASSEMBLY',750, 3000, 11 UNION ALL
SELECT 'QADIR','VACCINES','C',1900, 3100, 21 UNION ALL
SELECT 'RAMESH','HOTEL MGMT.','DBASE',13000, 35000, 4 UNION ALL
SELECT 'RAMESH','DEAD LEE','PASCAL',599.95, 4500, 73 UNION ALL
SELECT 'REMITHA','PC UTILITIES','C',725, 5000, 51 UNION ALL
SELECT 'REMITHA','TSR HELP PKG.','ASSEMBLY',2500, 6000, 7 UNION ALL
SELECT 'REVATHI','HOSPITAL MGMT.','PASCAL',1100, 75000, 2 UNION ALL
SELECT 'VIJAYA','TSR EDITOR','C',900, 700, 6
Go

CREATE TABLE programmer (PNAME varchar(20), DOB date, DOJ date, GENDER varchar(2),
PROF1 varchar(20), PROF2 varchar(20), SALARY int)
GO

SELECT * FROM programmer

INSERT INTO programmer


SELECT 'ANAND','12-Apr-66','21-Apr-92','M','PASCAL','BASIC',3200 UNION ALL
SELECT 'ALTAF','02-Jul-64','13-Nov-90','M','CLIPPER','COBOL',2800 UNION ALL
SELECT 'JULIANA','31-Jan-60','21-Apr-90','F','COBOL','DBASE',3000 UNION ALL
SELECT 'KAMALA','30-Oct-68','02-Jan-92','F','C','DBASE',2900 UNION ALL
SELECT 'MARY','24-Jun-70','01-Feb-91','F','CPP','ORACLE',4500 UNION ALL
SELECT 'NELSON','11-Sep-85','11-Oct-89','M','COBOL','DBASE',2500 UNION ALL
SELECT 'PATTRICK','10-Nov-65','21-Apr-90','M','PASCAL','CLIPPER',2800 UNION ALL
SELECT 'QADIR','31-Aug-65','21-Apr-91','M','ASSEMBLY','C',3000 UNION ALL
SELECT 'RAMESH','03-May-67','28-Feb-91','M','PASCAL','DBASE',3200 UNION ALL
SELECT 'REBECCA','01-Jan-67','01-Dec-90','F','BASIC','COBOL',2500 UNION ALL
SELECT 'REMITHA','19-Apr-70','20-Apr-93','F','C','ASSEMBLY',3600 UNION ALL
SELECT 'REVATHI','02-Dec-69','02-Jan-92','F','PASCAL','BASIC',3700 UNION ALL
SELECT 'VIJAYA','14-Dec-65','02-May-92','F','FOXPRO','C',3500

select * from programmer


select * from software
select * from studies

1. Find out the selling cost AVG for packages developed in Pascal.

select avg(scost) from software where DEVELOPIN = 'PASCAL'

2. Display Names, Ages of all Programmers.

select pname,datediff(YY,DOB,GETDATE()) as Age from programmer

3. Display the Names of those who have done the DAP Course.

select pname from studies where course = 'DAP'

4. Display the Names and Date of Births of all Programmers Born in January.

select pname,dob from programmer where MONTH(DOB)=1

5. What is the Highest Number of copies sold by a Package?

select max(sold) from software

select pname,title,sold from software where sold = (select max(sold) from software)

6. Display lowest course Fee.

select min(course_fee) from studies

7. How many programmers done the PGDCA Course?

select count(pname) from studies where course='PGDCA'

8. How much revenue has been earned thru sales of Packages Developed in C.

select sum(scost*sold) from software where DEVELOPIN='c'


9. Display the Details of the Software Developed by Ramesh.

select * from software where pname='ramesh'

10. How many Programmers Studied at Sabhari?

select count(institute) from studies where INSTITUTE='sabhari'

11. Display details of Packages whose sales crossed the 2000 Mark.

select * from software where (sold*scost)>=2000

12. Display the Details of Packages for which Development Cost have been recovered.

select * from software where (scost*sold)>=dcost

13. What is the cost of the costliest software development in Basic?

select max(dcost) from software where DEVELOPIN='basic'

14. How many Packages Developed in DBASE?

select count(TITLE) from software where developin='dbase'

15. How many programmers studied in Pragathi?

select count(PNAME) from studies where INSTITUTE='pragathi'

16. How many Programmers Paid 5000 to 10000 for their course?

select count(pname) from studies where COURSE_FEE between 5000 and 10000

17. What is AVG Course Fee

select avg(course_fee) from studies

18. Display the details of the Programmers Knowing C.

select * from programmer where PROF1='c' or PROF2='c'

19. How many Programmers know either COBOL or PASCAL.

select count(pname) from programmer where PROF1 IN('Cobol','Pascal') or Prof2


IN('cobol','pascal')

20. How many Programmers Don’t know PASCAL and C


select * from programmer where PROF1 NOT IN('pascal','c') AND PROF2 NOT
IN('pascal','c')

21. How old is the Oldest Male Programmer.

select MAX(DATEDIFF(YY,DOB,GETDATE())) from programmer where gender='M'

22. What is the AVG age of Female Programmers?

select AVG(DATEDIFF(YY,DOB,GETDATE())) from programmer where gender='F'

23. Calculate the Experience in Years for each Programmer and Display with their
names in Descending order.

select pname,datediff(yy,doj,getdate()) AS experience from programmer order by


Experience desc

24. Who are the Programmers who celebrate their Birthday’s During the Current
Month?

select pname from programmer where month(dob)=month(getdate())

25. How many Female Programmers are there?

select count(pname) from programmer where gender='f'

26. What are the Languages studied by Male Programmers.

select p.pname,s.course from programmer as p


left join
studies as s
on p.pname=s.pname
where gender='M'

27. What is the AVG Salary?

select max(salary) as Max_Salary from programmer

28. How many people draw salary 2000 to 4000?

select count(pname) from programmer where salary between 2000 and 4000

29. Display the details of those who don’t know Clipper, COBOL or PASCAL.

select * from programmer where prof1 NOT IN('clipper','cobol','pascal') AND prof2


NOT IN('clipper','cobol','pascal')

30. Display the Cost of Package Developed By each Programmer.


select pname, sum(dcost) from software group by pname

31. Display the sales values of the Packages Developed by the each Programmer.

select pname,sum(scost*sold) from software group by pname

32. Display the Number of Packages sold by Each Programmer.

select pname,sum(sold) from software group by pname

33. Display the sales cost of the packages Developed by each Programmer Language
wise.

select DEVELOPIN,sum(scost) from software group by DEVELOPIN

34. Display each language name with AVG Development Cost, AVG Selling Cost and
AVG Price per Copy.

select developin, avg(dcost) as Average_Development_Cost,avg(scost) as


Average_Selling_Cost,
IIF(sum(sold)<>0,(sum(scost)/sum(sold)),0) as Average_Price_Per_Copy
from software group by developin

35. Display each programmer’s name, costliest and cheapest Packages Developed by
him or
her.

select pname,min(dcost) AS Cheapest, max(dcost) AS Costliest from software group by


pname

36. Display each institute name with number of Courses, Average Cost per Course.

--select course,avg(course_fee) from studies group by course

37. Display each institute Name with Number of Students.

select institute,count(pname) from studies group by institute

38. Display Names of Male and Female Programmers. Gender also.

select pname,gender from programmer where Gender='M'


union all
select pname,gender from programmer where gender='F'

39. Display the Name of Programmers and Their Packages.

select pname,title from software order by pname

40. Display the Number of Packages in Each Language Except C and C++.
select developin, count(title) from software where developin not in ('C','C++')
group by developin

You might also like