SQL Practice Questions 2 Chapter No 9 SQL
SQL Practice Questions 2 Chapter No 9 SQL
a)
i
ii
iii
iv
Based on these tables write SQL statements for the following queries:
Display the lowest and the highest classes from the table STUDENTS.
mysql> select max(class) as "highest", min(class) as "lowest" from students;
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;
Display the number of students in class 10.
mysql> select count(class) from students where class = 10;
Display details of the students of Cricket team
mysql> select student.* from students,sports where students.admno=sports.admno and
game='Cricket';
mysql> select student.* from students join sports using(admno) where game='Cricket';
Display the Admission number, name, class, section, and roll number of the students
where grade='A';
Display the name and phone number of the students of class 12 who play some game.
mysql> select name,phone from students,sports where students.admno=sports.admno and
class=12 and game is not null;
mysql> select name,phone from students join sports using(admno) where class=12 and
vii
vii
i
b)
coachname='Narendra';
Identify the Foreign Keys (if any) of these tables.
Justify your choices.
Admno is the Foreign Key in Sports table, because its the only column/field in common
c)
ii
| Game
| COUNT(*) |
+-------------+----------+
| Basket Ball |
5|
| Cricket
|
4|
| Volleball |
2|
+-------------+----------+
iii
iv
BILLS
+--------+------------+--------+------+
| BillNo | Datee
| I_Code | Qty |
+--------+------------+--------+------+
|
1 | 2010-04-01 | 1002 | 2 |
|
1 | 2010-04-01 | 3001 | 1 |
|
2 | 2010-04-01 | 1001 | 3 |
|
2 | 2010-04-01 | 1002 | 1 |
|
2 | 2010-04-01 | 2003 | 2 |
|
3 | 2010-04-02 | 2002 | 1 |
|
4 | 2010-04-02 | 2002 | 4 |
|
4 | 2010-04-02 | 2003 | 2 |
|
5 | 2010-04-03 | 2003 | 2 |
|
5 | 2010-04-03 | 3001 | 1 |
|
5 | 2010-04-03 | 3002 | 3 |
+--------+------------+--------+------+
a)
i
Based on these tables write SQL statements for the following queries:
Display the average rate of a South Indian
ii
item.
mysql> select avg(rate) from items where category="South Indian";
Display the number of items in each
category.
mysql> select category,count(name) from items group by category;
iii
iv
item.
mysql> select I_code,sum(qty) from bills group by I_code;
Display total quantity of each item sold but don't display this data for the items whose
vi
vii
vii
i
b)
c)
ii
iii
What can be the possible uses of Bills table? Can it be used for some analysis purpose?
Yes, using the Bills table, we can calculate the amount of sales for each day and for each
item.
Do you find any columns in these tables which can be NULL? Is there any column which
must not be NULL?
No, no column can be null.
CHALLAN
+------------+--------+----+---+
|Field
|Type |Null|Key|
+------------+--------+----+---+
|Challan_No |int(11) |NO |PRI|
|Ch_Date
|date |YES | |
|RegNo
|char(10)|YES | |
|Offence_Code|int(3) |YES | |
+------------+--------+----+---+
OFFENCE
+------------+-----------+----+---+-------+
|Field
|Type
|Null|Key|Default|
+------------+-----------+----+---+-------+
|Offence_Code|int(3)
|NO |PRI|0
|
|Off_Desc |varchar(30)|YES | |NULL |
|Challan_Amt |int(4)
|YES | |NULL |
+------------+-----------+----+---+-------+
iv
'6C'.
mysql> select count(RegNo) from vehicle where RegNo like '_ _6C%';
Display the total value of challans issued for which the Off_Desc is 'Driving without
a)
i
ii
iii
License'.
mysql> select count(challan_no) from challan where offence_code=(select
vi
04-03';
mysql> select Challan_No,Ch_date,RegNo,challan.Offence_code,Offence_Desc from
challan join offence
vii
viii
count(Challan_No)>1;
Display details of each challan alongwith vehicle details, Off_desc, and Challan_Amt.
mysql> select * from vehicle,challan,offence where vehicle.RegNo=challan.RegNo and
b)
c)
challan.Offence_code=offence.offence_code ;
Identify the Foreign Keys (if any) of these tables. Justify your choices.
RegNo, Offence_Code are the two foreign keys in the table Challan.
Should any of these tables have some more column(s)? Think, discuss in peer
groups, and discuss with your teacher.
No, Not Required
a)
i
ii
iii
EMPLOYEE
+----+---------+--------+--------+------+-------+------+
| No | Name | Salary | Zone | Age | Grade | Dept |
+----+---------+--------+--------+------+-------+------+
| 1 | Mukul | 30000 | West | 28 | A
| 10 |
| 2 | Kritika | 35000 | Centre | 30 | A
| 10 |
| 3 | Naveen | 32000 | West | 40 | NULL | 20 |
| 4 | Uday | 38000 | North | 38 | C
| 30 |
| 5 | Nupur | 32000 | East | 26 | NULL | 20 |
| 6 | Moksh | 37000 | South | 28 | B
| 10 |
| 7 | Shelly | 36000 | North | 26 | A
| 30 |
+----+---------+--------+--------+------+-------+------+
Based on these tables write SQL statements for the following queries:
Display the details of all the employees who work in Sales department.
mysql> select * from employee where dept=(select dept from department where
dname='Sales');
Display the Salary, Zone, and Grade of all the employees whose HOD is Nupur.
mysql> select salary,zone,grade from employee where dept=(select dept from
department where hod=(select no from employee where name='Nupur'));
Display the Name and Department Name of
all the employees.
mysql> select name,dname from employee,department where
iv
employee.dept=department.dept;
Display the names of all the employees whose salary is not within the specified range
for the corresponding department.
mysql> select name from employee,department where employee.dept=department.dept
b)