You are on page 1of 4

Table 1 : student

Table 2 : sports

a).Based on the tables given above write sql statements for the following queries:
1. Display the lowest and the highest classes from the table student.
mysql> select max(class) as 'highest', min(class) as 'lowest' from student;
2. Display the number of students in each class from the table students.
mysql> select class, count(class) as 'No. of Students' from student group by class;
3. Display the number of students in class 10.
mysql> select count(class) from student where class = 10;
4. Display details of the students of cricket team.
mysql> select st.* from student st, sports sp where st.admno = sp.admno and game =
'cricket';
5. Display the admission number, name, class, section and roll number of the students
whose grade in sports table is 'A'.
mysql> select student.admno, name, class, sec, rollno from student, sports where
student.admno = sports.admno and grade = 'A';
6. Display the name and phone number of the students of class 12 who play some game.
mysql> select name, phone from student, sports where student.admno = sports.admno and
class = 12 and game is not null;
7. Display the number of students with each coach.
mysql> select coachname, count(admno) from sports group by coachname;
8. Display the names and phone numbers of the students whose grade is 'A' and whose coach is
Narendra.
mysql> select name, phone from student, sports where student.admno = sports.admno and
grade = 'A' and coachname = 'Narendra';
b). Identify the Foreign Keys (if any) of these tables. Justify your answer.
Ans. Admno is the Foreign Key in Sports table, because it contains corresponding values of
regno of student table.
Table 1 : items

Table 2 : bills

a).Based on the tables given above write sql statements for the following queries:
1. Display the average rate of a South Indian item.
mysql> select avg(rate) from items where category = "South Indian";
2. Display the number of items in each category.
mysql> select category, count(name) from items group by category;
3. Display the total quantity sold for each item.
mysql> select i_code, sum(qty) from bills group by i_code;
4. Display total quantity of each item sold but don't display this data for the items whose
total quantity sold is less than 3.
mysql> select i_code, sum(qty) from bills group by i_code having sum(qty)>=3;
5. Display the details of bill records along with name of each corresponding item.
mysql> select b.*, name from items i, bills b where i.i_code = b.i_code;
6. Display the details of the bill records for which the item is Dosa.
mysql> select b.*, name from items i, bills b where i.i_code = b.i_code and name like
'%dosa';
7. Display the bill records for each Italian item sold.
mysql> select b.* from items i, bills b where i.i_code = b.i_code and category = 'Italian';
8. Display the total value of items sold for each bill.
mysql> select billno, sum(qty*rate) from items i, bills b where i.i_code = b.i_code group
by billno;
b). Identify the Foreign Keys (if any) of these tables. Justify your answer.
Ans. I_code is the foreign key in bills table, because it's the only column/field in common
to join the two given tables.
c). Answer with justifiction (More than one answers may be correct. It all depends on your
logical thinking):
i). Is it easy to remember the category of item with a given item code? Do you find any kind
of pattern in the items code?
What could be the item code of another South Indian item?
Ans. Yes, The I_code of south indian starts with 100, chinese starts with 200 and italian
starts with 300,
The item code of another south indian item would be 100_.
ii). What can be the possible uses of bills table? Can it be used for some analysis purpose?
Ans. Yes, using the bills table, we can calculate the amount of sales for each day and for
each item.
iii). Do you find any columns in these tables which can be null? Is there any column which
must not be null?
Ans. No, no column can be null.

Table 1 : vehicle

Table 2 : challan

Table 3 : offence

a).Based on the tables given above write sql statements for the following queries:
1. Display the dates of first registration and last registration from the table vehicle.
mysql> select min(regdate) as 'first registration', max(regdate) as 'last registration'
from vehicle;
2. Display the number of challans issued on each date.
mysql> select ch_date, count(challan_no) from challan group by ch_date;
3. Display the total number of challans issued for each offence.
mysql> select offence, count(challan_no) from challan group by offence;
4. Display the total number of vehicles for which the 3rd and 4th characters of regno are
'6C'.
mysql> select count(regno) from vehicle where regno like '__6C%';
5. Display the total value of challans issued for which the off_desc is 'Driving without
Licence'.
mysql> select sum(challan_amt) from challan, offence where offence = offence_code and
off_desc = 'Driving without Licence';
6. Display details of the challans issued on '2010-04-03' along with off_desc for each
challan.
mysql> select challan.*, off_desc from challan, offence where offence = offence_code and
ch_date = '2010-04-03';
7. Display the regno of all vehicles which have been challaned more than once.
mysql> select regno, count(challan_no) from challan group by regno having
count(challan_no) > 1;
8. Display details of each challan alongwith vehicle details, off_desc and challan
amount.
mysql> select challan.*, vehicle.*, off_desc, challan_amt from challan, vehicle, offence
where offence = offence_code and vehicle.regno = challan.regno;
b). Identify the Foreign Keys (if any) of these tables. Justify your choices.
Ans. Regno, offence are the two foreign keys in the table challan because they contain
corresponding values of regno of vehicle table and offence_code of offence table
respectively.
Table 1: Employee

Table 2: Department

A). Based on these tables write SQL statements for the following queries:
1. Display the details of all the employees who work in Sales department.
mysql> select employee.* from employee e, department d where e.dept = d.dept and dname =
‘sales’;
2. Display the Salary, Zone and Grade of all the employees whose HOD is Nupur.
mysql> select salary, zone, grade from employee e, department d where e.dept = d.dept and
hod = (select no from employee where name = ‘Nupur ’);
3. Display the Name and Department Name of all the employees.
mysql> select name, dname from employee e, department d where e.dept = d.dept;
4. Display the names of all the employees whose salary is not within the specified range
for the corresponding department.
mysql> select name from employee e, department d where e.dept = d.dept and salary not
between minsal and maxsal;
5. Display the name of the department and the name of the corresponding HOD for all the
departments.
mysql> select dname, name from employee , department where hod = no;
B). Identify the Foreign Keys (if any) of these tables. Justify your choices.
Ans. Dept is the Foreign key in employee table and hod is the foreign key in department
table, because they contains values corresponding to the primary key in another table.

You might also like