The document provides examples of SQL functions, queries using aggregation functions like SUM, COUNT, AVG, MIN, MAX. It also provides examples of queries using clauses like GROUP BY, HAVING and ORDER BY. The questions cover concepts like aggregation, sorting, filtering and retrieving data from multiple tables using joins. Sample table structures and sample data is provided for tables like schoolbus, graduate, teacher, product, flight, store, gym and salesperson. SQL queries are written to retrieve, filter, group and aggregate data from these tables.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
468 views
Practical File 3
The document provides examples of SQL functions, queries using aggregation functions like SUM, COUNT, AVG, MIN, MAX. It also provides examples of queries using clauses like GROUP BY, HAVING and ORDER BY. The questions cover concepts like aggregation, sorting, filtering and retrieving data from multiple tables using joins. Sample table structures and sample data is provided for tables like schoolbus, graduate, teacher, product, flight, store, gym and salesperson. SQL queries are written to retrieve, filter, group and aggregate data from these tables.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12
SQL FUNCTIONS
ASSIGNMENTS Q.1
a. Give the output of following queries based on schoolbus
table: (i) select sum(distance) from schoolbus where transporter= “Yadav travels”; (ii) select min(noofstudents) from schoolbus; (iii) select avg(charges) from schoolbus where transporter= “Anand travels”; (iv) select distinct transporter from schoolbus; b. Write sql query for following (i) Display area_covered in uppercase along with distance (ii) Display area_covered with largest length(number of characters) (iii) Display area_covered with largest distance (iv) Display the list of schoolbus where rightmost part of transporter is travels (v) Display the total distance covered by ‘Anand travels’ (vi)Display the transporter which charges maximum with regards to all area covered taken together. Answer a) i)sum(distance) 180000 ii)min(noofstudents) 40 iii)avg(charges) 81,666.666666667 iv)6 b) i) SELECT UPPER(Area_Covered), Distance from SchoolBus; ii) SELECT MAX(LENGTH(Area_Covered)) from SchoolBus; iii) SELECT Area_Covered from SchoolBus where Distance = MAX(DISTANCE); iv) SELECT * from SchoolBus where right(Transporter) = ‘Transport’; v) SELECT SUM(Transporters) from SchoolBus where Transporters = ‘ANAND TRAVELS’; vi) SELECT MAX(Charges) from SchoolBus group by Area_Covered; Q2.
a. Give the output of following sql statement based on table
GRADUATE: (i) Select MIN(AVERAGE) from GRADUATE where SUBJECT=‖PHYSICS‖; (ii) Select SUM(STIPEND) from GRADUATE WHERE div=2; (iii) Select AVG(STIPEND) from GRADUATE where AVERAGE>=65; (iv) Select COUNT(distinct SUBDJECT) from GRADUATE; b. Write sql query for following (i) Display the name of the students in lower case (ii) Display the average stipend of COMP.SC. subject (iii) Display the name , stipend of student where subject is either physics or maths (iv) Display the subject with maximum stipend (v) Display the subject with maximum stipend(sum of stipend of all student for each subject) Ans A i) 63 ii) 800 iii) 450 iv) 4 b i) SELECT LOWER(Name) from GRADUATE; ii) SELECT AVG(Stipend) from GRADUATE where Subject = Comp sc; iii) SELECT Name,Stipend from GRADUATE where Subject = “Maths” or Subject = “Physics”; iv) SELECT SUBJECT,MAX(Stipend) from GRADUATE; v) SELECT SUBJECT,MAX(Stipend) from GRADUATE group by Subject; Q3. a. Give the output of the following SQL statements. i. Select COUNT(distinct department) from TEACHER; ii. Select MAX(Age) from TEACHER where SEX=‘F‘; iii. Select AVG(Salary) from TEACHER where SEX=‘M‘; iv. Select SUM(Salary) from TEACHER where DATOFJOIN<{12/07/96}; b. Write SQL query for following i. Display the teacher name with maximum age ii. Display the teacher name with minimum salary iii. Display the department where maximum salary is being given iv. Display the name of the teacher where year of dateofadm is 98 v. Display the name of the teacher whose day of dateofadm is Monday vi. Display how many males and females are there in teacher table Ans A i) 3 ii) 35 iii) 23600 iv)0 b i) SELECT Name from TEACHER where Age = MAX(Age); ii) SELECT Name from TEACHER where Salary = MIN(Salary); iii) SELECT Department, MAX(SALARY) from TEACHER group by Department; iv) SELECT Name from TEACHER where year(dateofadm) = 1998; v) SELECT Name from TEACHER where DAYNAME(dateofadm) = “Monday”; vi) SELECT COUNT(Gender) from TEACHER group by Gender;
Q4.
a. Give the output of following statement.
(i) Select COUNT(distinct company) from PRODUCT. (ii) Select MAX(price)from PRODUCT where WARRANTY<=3 (iii) select avg(price) from PRODUCT (IV) SELECT MAX(STOCK),PNAME FROM PRODUCT b. Write SQL query for following i. Display the product with oldest manufacturing date ii. Display the product with maximum warranty iii. Display the recent product iv. Display the costliest TV company v. Display manufacturing year of all products vi. Display the product manufactured in MAR month vii. display the product name with maximum length. Ans A i) 4 ii) 39000 iii) 23400 iv) 250 HANDYCAM B i) SELECT pname, MIN(Manufacture) from PRODUCT; ii) SELECT pname, MAX(Warranty) from PRODUCT; iii) SELECT pname, MIN(Manufacture) from PRODUCT; iv) SELECT pname, MAX(Price) from PRODUCT group by pname having pname = “TV”; v) SELECT Pname, YEAR(Manufacture) from PRODUCT; vi) SELECT Pname from PRODUCT where MONTHNAME(Manufacture) = “March”;vii) SELECT Pname from PRODUCT where LENGTH(Pname) = MAX(LENGTH(Pname));
SQL ORDER BY,GROUP BY ,HAVING CLAUSE
QUERIES Q5. Write SQL queries w/r to FLIGHT table data i. Display flight codes, starting place, destination, number of flights in descending order of number of flights. Select flcode,start,destination,no_flights from flight order by no_flights desc ii. Count and display number of flights starting from each city(START). Select start,count(start) from flight group by start iii. Count and display number of flights starting from each city(START) where number of stops more than 2. Select start,count(start) from flight group by start Having no_stops>2 iv. Display flight data in ascending order of no of stops select * from flight order by no_stops v. Count and display number of flights reaching at each destination. Select destination,count(destination) from flight group by destination
vi. Count and display number of flights without stops
Select count(no_stops) from flight group by no_stops having no_stops=0
Q6.
Write SQL queries w/r to store table data
i. To display name, location, city, SalesAmount of stores in descending order of SalesAmount. ii. To display total SalesAmount of each city along with city name. iii. To display total number of employees of each city along with city name. iv. To display total number of employees of each city along with city name where total employee of a city >15. v. To display total sales amount of each city along with city name. i. select Name, Location, City, SalesAmount from Store order by SalesAmount desc; ii. select sum(SalesAmount), City from Store group by City; iii. select sum(NoOfEmployees), City from Store group by City; iv. select sum(NoOfEmployees), City from Store group by City having NoOfEmployees > 15; v. select sum(SalesAmount), City from Store group by City;
Q7.
Write SQL queries w/r to Gym table data
i. To display Mcode, Mname, Age of all female members of the Gym with age in descending order. Select Mcode, Mname, Age from gym where gender=”female” ii. To count the number of members of the Gym of each type. Select type,count(type) from gym group by type iii. To display the type alongwith maximum and minimum fees of each type. Select max(feegiven),min(feegiven),type from gym group by type iv. To display types of memberships available. Duplicate values should not be displayed. Select distinct count(type) from gym v. To display total fee given by each gender in Gym. Select sum(feegiven),gender from gym group by gender vi. To display the average age of each gender in Gym Select avg(age),gender from gym group by gender
vii. To display sum of fee given in each membership type
Select sum(feegiven),type from gym group by type
Q8.
Write SQL queries w/r to Salesperson table data
i. To display Names and Salaries of Salespersons in descending order of salary. Select name,salary from salesperson order by salary desc ii. To display areas in which Salespersons are working. Duplicate Areas should not be displayed. Select distinct count(area) from salesperson iii. To display Area along with number of Salespersons working in that area. Select area,count(area) from salesperson group by area iv. To display total salary paid in each area Select sum(salary) from salesperson group by area v. To display name of salesperson from the area where no of salesperson more than 1 Select name from salesperson where count(area)>1 group by area