6. select name , Dname
from student
where semester = 5 and Dname = ‘Mech’;
Output:
| name | Dname |
+--------+-------+
| Akash | Mech |
| Nithin | Mech |
+--------+-------+
7. select name , Dname
from student
where semester = 5 or Dname = ‘Mech’;
8. select name , Dname
from student
where semester = 5 or Dname = ‘Mech’;
Ouput:
| name | Dname |
+----------+-------+
| Architha | ISE |
| Akash | Mech |
| Nithin | Mech |
| Pramod | Mech |
+----------+-------+
9. select USN, name, Dname, location
from student, Department
where Dname = 'Mech' and Dname = Dname;
?
10. select USN, name, Dname, location
from student, Department
where Dname = 'Mech' and Dname = Dname;
ERROR 1052 (23000): Column 'Dname' in field list is ambiguous
?
11. Ambiguous Attribute Names, Aliasing, Renaming and tuple variable
select USN, name, student.Dname ,location
from student,Department
where student.Dname = 'Mech' and student.Dname = Department.Dname;
12. Ambiguous Attribute Names, Aliasing, Renaming and tuple variable
select USN, name, student.Dname ,location
from student,Department
where student.Dname = 'Mech' and student.Dname = Department.Dname;
Output :
| USN | name | Dname | location |
+-------+--------+-------+----------+
| ME016 | Akash | Mech | Block A |
| ME030 | Nithin | Mech | Block A |
| ME045 | Pramod | Mech | Block A |
+-------+--------+-------+----------+
26. select USN,name,semester+1 as New_sem, location as department_location
from student S, Department D
where S.Dname = D.Dname and D.location = 'Block A';
27. select USN,name,semester+1 as New_sem, location as department_location
from student S, Department D
where S.Dname = D.Dname and D.location = 'Block A';
+-------+--------+---------+---------------------+
| USN | name | New_sem | department_location |
+-------+--------+---------+---------------------+
| ME016 | Akash | 6 | Block A |
| ME030 | Nithin | 6 | Block A |
| ME045 | Pramod | 8 | Block A |
+-------+--------+---------+---------------------+
52. Aggregate Functions in SQL
select SUM(Salary) AS SUM,MAX(Salary)AS
MAX_SAL,MIN(Salary)AS MIN_SAL,AVG(Salary) AS AVG_SAL
from faculty;
+--------+---------+---------+------------+
| SUM | MAX_SAL | MIN_SAL | AVG_SAL |
+--------+---------+---------+------------+
| 280000 | 50000 | 40000 | 46666.6667 |
+--------+---------+---------+------------+
53. Aggregate Functions in SQL
select SUM(Salary) AS SUM,MAX(Salary)AS
MAX_SAL,MIN(Salary)AS MIN_SAL,AVG(Salary) AS AVG_SAL
from faculty where DName = 'CSE'
-> ;
+--------+---------+---------+------------+
| SUM | MAX_SAL | MIN_SAL | AVG_SAL |
+--------+---------+---------+------------+
| 100000 | 50000 | 50000 | 50000.0000 |
+--------+---------+---------+------------+
54. Aggregate Functions in SQL
select Count(*) from Faculty;
+----------+
| Count(*) |
+----------+
| 6 |
+----------+
55. Aggregate Functions in SQL
select Count(*) as NO_of_Faculties from Faculty;
+-----------------+
| NO_of_Faculties |
+-----------------+
| 6 |
+-----------------+
56. Aggregate Functions in SQL
select Count(*) as No_of_Faculties,SUM(Salary) as SUM from
Faculty;
+-----------------+--------+
| No_of_Faculties | SUM |
+-----------------+--------+
| 6 | 280000 |
+-----------------+--------+
60. select Fname
from faculty
where(Salary not between 35000 and 45000);
+----------+
| Fname |
+----------+
| Kavitha |
| Priyanka |
| Bharathi |
+----------+
61. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname;
62. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student group by Dname;
| dname | NO_of_Students |
+-------+----------------+
| CSE | 3 |
| ECE | 2 |
| ISE | 2 |
| Mech | 3 |
+-------+----------------+
63. select dname ,Count(Dname) as NO_of_Students
from student
where semester = 7
group by Dname;
Group By & Having Clauses in SQL
64. select dname ,Count(Dname) as NO_of_Students
from student
where semester = 7
group by Dname;
Group By & Having Clauses in SQL
Output:
| dname | NO_of_Students |
+-------+----------------+
| CSE | 2 |
| Mech | 1 |
+-------+----------------+
65. Group By & Having Clauses in SQL
select dname , semester, Count(*) as NO_of_Students
from student
group by Dname, semester;
66. Group By & Having Clauses in SQL
select dname , semester, Count(*) as NO_of_Students
from student
group by Dname, semester;
Output:
| dname | semester | NO_of_Students |
+-------+----------+----------------+
| CSE | 5 | 1 |
| CSE | 7 | 2 |
| ECE | 3 | 1 |
| ECE | 5 | 1 |
| ISE | 3 | 1 |
| ISE | 5 | 1 |
| Mech | 5 | 2 |
| Mech | 7 | 1 |
+-------+----------+----------------+
67. Group By & Having Clauses in SQL
select dname ,semester, Count(*) as NO_of_Students
from student
group by semester;
68. Group By & Having Clauses in SQL
select dname ,semester, Count(*) as NO_of_Students
from student
group by semester;
Output:
| dname | semester | NO_of_Students |
+-------+----------+----------------+
| ECE | 3 | 2 |
| CSE | 5 | 5 |
| CSE | 7 | 3 |
+-------+----------+----------------+
69. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname
having count(dname)>2;
70. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname
having count(dname)>2;
Output:
| dname | NO_of_Students |
+-------+----------------+
| CSE | 3 |
| Mech | 3 |
+-------+----------------+
71. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname
having count(dname)>2
order by Dname DESC;
72. Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname
having count(dname)>2
order by Dname DESC;
Output:
| dname | NO_of_Students |
+-------+----------------+
| Mech | 3 |
| CSE | 3 |
+-------+----------------+
73. Group By & Having Clauses in SQL
select dname ,Count(*) as NO_of_Students
from student
where semester = 7
group by Dname
having count(*)>1;
74. Group By & Having Clauses in SQL
select dname ,Count(*) as NO_of_Students
from student
where semester = 7
group by Dname
having count(*)>1;
Output :
| dname | NO_of_Students |
+-------+----------------+
| CSE | 2 |
+-------+----------------+
85. Nested Queries
select S.sname
from sailors S
where S.sid IN (select R.sid
from reserves R
where R.bid = 103);
Output:
| sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
86. select S.sname
from sailors S
where S.sid NOT IN (select R.sid
from reserves R
where R.bid = 103);
Output:
| sname |
+---------+
| Brutus |
| Andy |
| Rusty |
| Horatio |
| Zorba |
| Art |
| Bob |
+---------+
87. select B.bid from boats B where B.color ='red';
+-----+
| bid |
+-----+
| 102 |
| 104 |
+-----+
select R.sid from reserves R where R.bid IN(102,104);
+-----+
| sid |
+-----+
| 22 |
| 31 |
| 64 |
| 22 |
+-----+
select S.sname from sailors S where S.sid NOT IN(22,31,64,22);
+---------+
| sname |
+---------+
| Brutus |
| Andy |
| Rusty |
| Zorba |
| Horatio |
| Art |
| Bob |
+---------+
88. select S.sname
from sailors S
where S.sid NOT IN (select R.sid
from reserves R
where R.bid IN (select B.bid
from boats B
where B.color = 'red'));
102
104
22
31
64
22
+---------+
| sname |
+---------+
| Brutus |
| Andy |
| Rusty |
| Zorba |
| Horatio |
| Art |
| Bob |
+---------+
90. Nested Queries – Set Comparison Operators
Find sailors whose rating is better than some sailor called Horatio
-----+
| sid |
+-----+
| 31 |
| 32 |
| 58 |
| 71 |
| 74 |
+-----+
91. Nested Queries – Set Comparison Operators
Find sailors whose rating is better than every sailor called Horatio
select S.sid
from sailors S
where S.rating > ALL(select S2.rating
from sailors S2
where S2.sname = 'Horatio');
OUTPUT:
| sid |
+-----+
| 58 |
| 71 |
+-----+
92. Nested Queries – Set Comparison Operators
Find the sailors with the highest rating
select S.sid
from sailors S
where S.rating >= ALL(select S2.rating
from sailors S2 );
+-----+
| sid |
+-----+
| 58 |
| 71 |
+-----+
93. Nested Queries – Set Comparison Operators
Find the names of the sailors who have reserved all boats
94. Nested Queries – Set Comparison Operators
Find the names of the sailors who have reserved all boats
select S.sname
from sailors S
where NOT EXISTS (select B.bid
from boats B
where NOT Exists (select R.bid
from reserves R
where R.bid=B.bid and R.sid=S.sid));
--------+
| sname |
+--------+
| Dustin |
+--------+
95. Nested Queries – Set Comparison Operators
Find the names of sailors who are older than the oldest
sailor with a rating of 10.
SELECT S.sname
FROM Sailors S
WHERE S.age > ( SELECT MAX( S2.age )
FROM Sailors S2
WHERE S2.rating = 10 );
| sname |
+--------+
| Dustin |
| Lubber |
| Bob |
+--------+
96. CountryId CountryName
1 INDIA
2 Nepal
3 Srilanka
Country
StateID CountryId StateName
1 1 Karnataka
2 1 Mumbai
3 2 Katmandu
4 Null California
State
Joins in SQL
INNER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
97. Joins in SQL
select *
from Country C INNER JOIN State S
on C.CountryId = S.CountryId;
98. Joins in SQL
select *
from Country C INNER JOIN State S
on C.CountryId = S.CountryId;
OUTPUT:
| CountryId | CountryName | StateID | CountryId | StateName |
+-----------+-------------+---------+-----------+-----------+
| 1 | INDIA | 1 | 1 | Karnataka |
| 1 | INDIA | 2 | 1 | Mumbai |
| 2 | Nepal | 3 | 2 | Katmandu |
+-----------+-------------+---------+-----------+-----------+
99. Joins in SQL
select C.CountryName, S.StateName
from Country C INNER JOIN State S
on C.CountryId = S.CountryId;
OUTPUT
| CountryName | StateName |
+-------------+-----------+
| INDIA | Karnataka |
| INDIA | Mumbai |
| Nepal | Katmandu |
+-------------+-----------+
100. Joins in SQL
select *
from Country C LEFT OUTER JOIN State S
on C.CountryId = S.CountryId;
101. Joins in SQL
select *
from Country C LEFT OUTER JOIN State S
on C.CountryId = S.CountryId;
OUTPUT:
| CountryId | CountryName | StateID | CountryId | StateName |
+-----------+-------------+---------+-----------+-----------+
| 1 | INDIA | 1 | 1 | Karnataka |
| 1 | INDIA | 2 | 1 | Mumbai |
| 2 | Nepal | 3 | 2 | Katmandu |
| 3 | Srilanka | NULL | NULL | NULL |
+-----------+-------------+---------+-----------+-----------+
102. Joins in SQL
select *
from Country C RIGHT OUTER JOIN State S
on C.CountryId = S.CountryId;
103. Joins in SQL
select *
from Country C RIGHT OUTER JOIN State S
on C.CountryId = S.CountryId;
OUTPUT:
-----------+-------------+---------+-----------+------------+
| CountryId | CountryName | StateID | CountryId | StateName |
+-----------+-------------+---------+-----------+------------+
| 1 | INDIA | 1 | 1 | Karnataka |
| 1 | INDIA | 2 | 1 | Mumbai |
| 2 | Nepal | 3 | 2 | Katmandu |
| NULL | NULL | 4 | NULL | California |
104. Joins in SQL
select *
from Country C LEFT OUTER JOIN State S
on C.CountryId = S.CountryId
UNION
select *
from Country C RIGHT OUTER JOIN State S
on C.CountryId = S.CountryId;
105. Joins in SQL
select *
from Country C LEFT OUTER JOIN State S
on C.CountryId = S.CountryId
UNION
select *
from Country C RIGHT OUTER JOIN State S
on C.CountryId = S.CountryId;
OUTPUT:
| CountryId | CountryName | StateID | CountryId | StateName |
+-----------+-------------+---------+-----------+------------+
| 1 | INDIA | 1 | 1 | Karnataka |
| 1 | INDIA | 2 | 1 | Mumbai |
| 2 | Nepal | 3 | 2 | Katmandu |
| 3 | Srilanka | NULL | NULL | NULL |
| NULL | NULL | 4 | NULL | California |
+-----------+-------------+---------+-----------+------------+
106. In MySQL, a trigger is a set of SQL statements that is invoked automatically when a change is
made to the data on the associated table.
A trigger can be defined to be invoked either before or after the data is changed by INSERT,
UPDATE or DELETE statement.
BEFORE INSERT - activated before data is inserted into the table.
AFTER INSERT - activated after data is inserted into the table.
BEFORE UPDATE - activated before data in the table is updated.
AFTER UPDATE - activated after data in the table is updated.
BEFORE DELETE - activated before data is removed from the table.
AFTER DELETE - activated after data is removed from the table.
TRIGGERS
107. When you use a statement that does not use INSERT, DELETE or UPDATE
statement to change data in a table, the triggers associated with the table are not
invoked.
MySQL Trigger Syntax
CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name
FOR EACH ROW
BEGIN
.........
END;
108. Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a
trigger. You use the BEFORE keyword if you want to process action prior to the change is made on the table
and AFTER if you need to process action after the change is made.
The trigger event can be INSERT, UPDATE or DELETE. This event causes the trigger to be invoked. A
trigger only can be invoked by one event. To define a trigger that is invoked by multiple events, you have to
define multiple triggers, one for each event.
A trigger must be associated with a specific table. Without a table trigger would not exist therefore you have
to specify the table name after the ON keyword.
You place the SQL statements between BEGIN and END block. This is where you define the logic for the
trigger.
109. itemid iDesc qoh price category
8 perfume 59 380 Cosemetics
10 kb 59 180 toys
Item
sid itemid qtysold price total
101 8 10 90 0
Sales_new
create trigger t2
before insert
on Sales_new
for each row
begin
set new.total = new.qtysold * new.price;
end;
115. Assignment
Consider the following relations:
Student(snum: integer, sname: string, major: string, level: string, age: integer)
Class(name: string, meets_at: time, room: string, fid: integer)
Enrolled(snum: integer, cname: string)
Faculty(fid: integer, fnarne: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair
such that the student is enrolled in the class.
Write the following queries in SQL. No duplicates should be printed in any of the answers.
1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.John Teach.
2. Find the age of the oldest student who is either a History major or enrolled in a course taught by 1. Teach.
3. Find the names of all classes that either meet in room R128 or have five or more students enrolled.
4. Find the names of all students who are enrolled in two classes that meet at the same time.
Submission Date : 10-9-2019
Time : 1.00 PM
116. 5. Find the names of faculty members who teach in every room in which some class is taught.
6. Find the names of faculty members for whom the combined enrollment of the courses that they teach is
less than five.
7. Print the level and the average age of students for that level, for each level.
8. Print the level and the average age of students for that level, for all levels except JR.
9. For each faculty member that has taught classes only in room R128, print the faculty member's name and
the total number of classes she or he has taught.
10. Find the names of students enrolled in the maximum number of classes.
11. Find the names of students not enrolled in any class.
12. For each age value that appears in Students, find the level value that appears most often. For example, if
there are more FR level students aged 18 than SR, JR, or SO students aged 18, you should print the pair (18,
FR).