SlideShare uma empresa Scribd logo
1 de 122
Baixar para ler offline
Database Management System
SQL
By:
Sharmila Chidaravalli
Asst. Prof.
Department of ISE
Global Academy of Technology
Dname location
civil Block B
CSE First Floor
ECE Second Floor
EEE Third Floor
ISE First Floor
Mech Block A
Department
USN name age semester Dname
CS025 Shilpa 22 7 CSE
EC006 Deepthi 20 3 ECE
IS001 Architha 20 5 ISE
IS025 Harsha 20 3 ISE
ME016 Akash 21 5 Mech
ME030 Nithin 21 5 Mech
ME045 Pramod 22 7 Mech
Student
Basic Retrieval Query : SELECT-FROM-WHERE SQL
select name
from student
where semester = 7;
Basic Retrieval Query : SELECT-FROM-WHERE SQL
select name
from student
where semester = 7;
Output:
+--------+
| name |
+--------+
| Shilpa |
| Pramod |
+--------+
select name ,Dname
from student
where semester = 5 and
Dname = ‘Mech’;
select name , Dname
from student
where semester = 5 and Dname = ‘Mech’;
Output:
| name | Dname |
+--------+-------+
| Akash | Mech |
| Nithin | Mech |
+--------+-------+
select name , Dname
from student
where semester = 5 or Dname = ‘Mech’;
select name , Dname
from student
where semester = 5 or Dname = ‘Mech’;
Ouput:
| name | Dname |
+----------+-------+
| Architha | ISE |
| Akash | Mech |
| Nithin | Mech |
| Pramod | Mech |
+----------+-------+
select USN, name, Dname, location
from student, Department
where Dname = 'Mech' and Dname = Dname;
?
select USN, name, Dname, location
from student, Department
where Dname = 'Mech' and Dname = Dname;
ERROR 1052 (23000): Column 'Dname' in field list is ambiguous
?
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;
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 |
+-------+--------+-------+----------+
select S.name, S.semester, D.Dname, D.location
from student S, Department D
where S.Dname = D.Dname;
select S.name, S.semester, D.Dname, D.location
from student S, Department D
where S.Dname = D.Dname;
+----------+----------+-------+--------------+
| name | semester | Dname | location |
+----------+----------+-------+--------------+
| Shilpa | 7 | CSE | First Floor |
| Deepthi | 3 | ECE | Second Floor |
| Architha | 5 | ISE | First Floor |
| Harsha | 3 | ISE | First Floor |
| Akash | 5 | Mech | Block A |
| Nithin | 5 | Mech | Block A |
| Pramod | 7 | Mech | Block A |
+----------+----------+-------+--------------+
select USN from student;
+-------+
| USN |
+-------+
| CS025 |
| EC006 |
| IS001 |
| IS025 |
| ME016 |
| ME030 |
| ME045 |
+-------+
Unspecified WHERE Clause and Use of Asterisk
+----------+--------------+
| name | location |
+----------+--------------+
| Shilpa | Block B |
| Shilpa | First Floor |
| Shilpa | Second Floor |
| Shilpa | Third Floor |
| Shilpa | First Floor |
| Shilpa | Block A |
| Deepthi | Block B |
| Deepthi | First Floor |
| Deepthi | Second Floor |
| Deepthi | Third Floor |
| Deepthi | First Floor |
| Deepthi | Block A |
| Architha | Block B |
| Architha | First Floor |
| Architha | Second Floor |
| Architha | Third Floor |
| Architha | First Floor |
| Architha | Block A |
| Harsha | Block B |
| Harsha | First Floor |
| Harsha | Second Floor |
| Harsha | Third Floor |
| Harsha | First Floor |
| Harsha | Block A |
| Akash | Block B |
| Akash | First Floor |
| Akash | Second Floor |
| Akash | Third Floor |
| Akash | First Floor |
| Akash | Block A |
| Nithin | Block B |
| Nithin | First Floor |
| Nithin | Second Floor |
| Nithin | Third Floor |
| Nithin | First Floor |
| Nithin | Block A |
| Pramod | Block B |
| Pramod | First Floor |
| Pramod | Second Floor |
| Pramod | Third Floor |
| Pramod | First Floor |
| Pramod | Block A |
+----------+--------------+
select name, location from student, Department;
select * from student, Department;
+-------+----------+------+----------+-------+-------+--------------+
| USN | name | age | semester | Dname | Dname | location |
+-------+----------+------+----------+-------+-------+--------------+
| CS025 | Shilpa | 22 | 7 | CSE | civil | Block B |
| CS025 | Shilpa | 22 | 7 | CSE | CSE | First Floor |
| CS025 | Shilpa | 22 | 7 | CSE | ECE | Second Floor |
| CS025 | Shilpa | 22 | 7 | CSE | EEE | Third Floor |
| CS025 | Shilpa | 22 | 7 | CSE | ISE | First Floor |
| CS025 | Shilpa | 22 | 7 | CSE | Mech | Block A |
| EC006 | Deepthi | 20 | 3 | ECE | civil | Block B |
| EC006 | Deepthi | 20 | 3 | ECE | CSE | First Floor |
| EC006 | Deepthi | 20 | 3 | ECE | ECE | Second Floor |
| EC006 | Deepthi | 20 | 3 | ECE | EEE | Third Floor |
| EC006 | Deepthi | 20 | 3 | ECE | ISE | First Floor |
| EC006 | Deepthi | 20 | 3 | ECE | Mech | Block A |
| IS001 | Architha | 20 | 5 | ISE | civil | Block B |
| IS001 | Architha | 20 | 5 | ISE | CSE | First Floor |
| IS001 | Architha | 20 | 5 | ISE | ECE | Second Floor |
| IS001 | Architha | 20 | 5 | ISE | EEE | Third Floor |
| IS001 | Architha | 20 | 5 | ISE | ISE | First Floor |
| IS001 | Architha | 20 | 5 | ISE | Mech | Block A |
| IS025 | Harsha | 20 | 3 | ISE | civil | Block B |
| IS025 | Harsha | 20 | 3 | ISE | CSE | First Floor |
| IS025 | Harsha | 20 | 3 | ISE | ECE | Second Floor |
| IS025 | Harsha | 20 | 3 | ISE | EEE | Third Floor |
| IS025 | Harsha | 20 | 3 | ISE | ISE | First Floor |
| IS025 | Harsha | 20 | 3 | ISE | Mech | Block A |
| ME016 | Akash | 21 | 5 | Mech | civil | Block B |
| ME016 | Akash | 21 | 5 | Mech | CSE | First Floor |
| ME016 | Akash | 21 | 5 | Mech | ECE | Second Floor |
| ME016 | Akash | 21 | 5 | Mech | EEE | Third Floor |
| ME016 | Akash | 21 | 5 | Mech | ISE | First Floor |
| ME016 | Akash | 21 | 5 | Mech | Mech | Block A |
| ME030 | Nithin | 21 | 5 | Mech | civil | Block B |
| ME030 | Nithin | 21 | 5 | Mech | CSE | First Floor |
| ME030 | Nithin | 21 | 5 | Mech | ECE | Second Floor |
| ME030 | Nithin | 21 | 5 | Mech | EEE | Third Floor |
| ME030 | Nithin | 21 | 5 | Mech | ISE | First Floor |
| ME030 | Nithin | 21 | 5 | Mech | Mech | Block A |
| ME045 | Pramod | 22 | 7 | Mech | civil | Block B |
| ME045 | Pramod | 22 | 7 | Mech | CSE | First Floor |
| ME045 | Pramod | 22 | 7 | Mech | ECE | Second Floor |
| ME045 | Pramod | 22 | 7 | Mech | EEE | Third Floor |
| ME045 | Pramod | 22 | 7 | Mech | ISE | First Floor |
| ME045 | Pramod | 22 | 7 | Mech | Mech | Block A |
+-------+----------+------+----------+-------+-------+--------------+
Substring Pattern matching and Arthimetic Operators
select USN, name, Dname from student where name like '%a';
Substring Pattern matching and Arthimetic Operators
select USN, name, Dname from student where name like '%a';
Output:
| USN | name | Dname |
+-------+----------+-------+
| CS025 | Shilpa | CSE |
| IS001 | Architha | ISE |
| IS025 | Harsha | ISE |
+-------+----------+-------+
select USN from student where USN like '_S_ _ _';
select USN from student where USN like '_S_ _ _';
Output:
| USN |
+-------+
| CS025 |
| IS001 |
| IS025 |
+-------+
select name from student where name like '____h%';
select name from student where name like '____h%';
Output:
| name |
+--------+
| Harsha |
| Akash |
+--------+
select USN,name,semester+1 as New_sem from student;
select USN, name, semester+1 as New_sem
from student;
Output:
| USN | name | New_sem |
+-------+----------+---------+
| CS025 | Shilpa | 8 |
| EC006 | Deepthi | 4 |
| IS001 | Architha | 6 |
| IS025 | Harsha | 4 |
| ME016 | Akash | 6 |
| ME030 | Nithin | 6 |
| ME045 | Pramod | 8 |
+-------+----------+---------+
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';
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 |
+-------+--------+---------+---------------------+
Admin_id Name Salary
AD001 Kiran 10000
AD004 Mary 20000
AD010 Sandesh 40000
AD025 Krishna 25000
AD045 Sahana 20000
Admin_Staff
FID FNAme Salary DName
AD010 Sandesh 40000 EEE
CSE10 Kavitha 50000 CSE
CSE35 Priyanka 50000 CSE
ECE05 Ramya 45000 ECE
ECE25 Bharathi 50000 ECE
ISE25 Ashwini 45000 ISE
Faculty
SELECT DISTINCT Salary
FROM Faculty;
Table as Sets
SELECT DISTINCT Salary
FROM Faculty;
Output:
| Salary |
+--------+
| 40000 |
| 50000 |
| 45000 |
+--------+
SELECT ALL Salary
FROM Faculty;
FID FNAme Salary DName
AD010 Sandesh 40000 EEE
CSE10 Kavitha 50000 CSE
CSE35 Priyanka 50000 CSE
ECE05 Ramya 45000 ECE
ECE25 Bharathi 50000 ECE
ISE25 Ashwini 45000 ISE
Faculty
FID FNAme Salary DName
AD010 Sandesh 40000 EEE
CSE10 Kavitha 50000 CSE
CSE35 Priyanka 50000 CSE
ECE05 Ramya 45000 ECE
ECE25 Bharathi 50000 ECE
ISE25 Ashwini 45000 ISE
Faculty
Output:
| Salary |
+--------+
| 40000 |
| 50000 |
| 50000 |
| 45000 |
| 50000 |
| 45000 |
+--------+
SELECT ALL Salary
FROM Faculty;
(select distinct Salary from Faculty)
UNION
(select distinct Salary from Admin_Staff);
(select distinct Salary from Faculty)
UNION
(select distinct Salary from Admin_Staff);
+--------+
| Salary |
+--------+
| 40000 |
| 50000 |
| 45000 |
| 10000 |
| 20000 |
| 25000 |
+--------+
Output :
| Salary |
+--------+
| 40000 |
| 50000 |
| 45000 |
| 10000 |
| 20000 |
| 25000 |
+--------+
(select Salary from Faculty)
UNION
(select Salary from Admin_Staff);
(select Salary from Faculty)
UNION ALL
(select Salary from Admin_Staff);
(select Salary from Faculty)
UNION ALL
(select Salary from Admin_Staff);
Output :
| Salary |
+--------+
| 40000 |
| 50000 |
| 50000 |
| 45000 |
| 50000 |
| 45000 |
| 10000 |
| 20000 |
| 40000 |
| 25000 |
| 20000 |
+--------+
(select Fname from Faculty)
UNION
(select Name from Admin_Staff);
(select Fname from Faculty)
UNION
(select Name from Admin_Staff);
+----------+
| Fname |
+----------+
| Sandesh |
| Kavitha |
| Priyanka |
| Ramya |
| Bharathi |
| Ashwini |
| Kiran |
| Mary |
| Krishna |
| Sahana |
+----------+
(select Name,Salary from Admin_Staff)
UNION
(select Fname,Salary from Faculty);
(select Name,Salary from Admin_Staff)
UNION
(select Fname,Salary from Faculty);
+----------+--------+
| Name | Salary |
+----------+--------+
| Kiran | 10000 |
| Mary | 20000 |
| Sandesh | 40000 |
| Krishna | 25000 |
| Sahana | 20000 |
| Kavitha | 50000 |
| Priyanka | 50000 |
| Ramya | 45000 |
| Bharathi | 50000 |
| Ashwini | 45000 |
+----------+--------+
select distinct Salary from Faculty inner join Admin_Staff
using(Salary);
+--------+
| Salary |
+--------+
| 40000 |
+--------+
select distinct Salary from Faculty left join Admin_Staff
using(Salary);
+--------+
| Salary |
+--------+
| 40000 |
| 50000 |
| 45000 |
+--------+
select Salary from Faculty left join Admin_Staff using(Salary);
+--------+
| Salary |
+--------+
| 40000 |
| 50000 |
| 50000 |
| 45000 |
| 50000 |
| 45000 |
+--------+
select Fname from Faculty order by FName DESC;
+----------+
| Fname |
+----------+
| Sandesh |
| Ramya |
| Priyanka |
| Kavitha |
| Bharathi |
| Ashwini |
+----------+
select Fname from Faculty order by FName ASC;
+----------+
| Fname |
+----------+
| Ashwini |
| Bharathi |
| Kavitha |
| Priyanka |
| Ramya |
| Sandesh |
+----------+
select Fname,Salary from Faculty order by Fname;
+----------+--------+
| Fname | Salary |
+----------+--------+
| Ashwini | 45000 |
| Bharathi | 50000 |
| Kavitha | 50000 |
| Priyanka | 50000 |
| Ramya | 45000 |
| Sandesh | 40000 |
+----------+--------+
select Fname,Salary from Faculty order by Salary ASC;
+----------+--------+
| Fname | Salary |
+----------+--------+
| Sandesh | 40000 |
| Ramya | 45000 |
| Ashwini | 45000 |
| Kavitha | 50000 |
| Priyanka | 50000 |
| Bharathi | 50000 |
+----------+--------+
Aggregate Functions in SQL
Select COUNT(FName) from Faculty;
+--------------+
| COUNT(FName) |
+--------------+
| 6 |
+--------------+
Aggregate Functions in SQL
Select COUNT(FName) from Faculty where Dname='ECE';
+--------------+
| COUNT(FName) |
+--------------+
| 2 |
+--------------+
Aggregate Functions in SQL
select SUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary)
-> from Faculty;
+-------------+-------------+-------------+-------------+
| SUM(Salary) | MAX(Salary) | MIN(Salary) | AVG(Salary) |
+-------------+-------------+-------------+-------------+
| 280000 | 50000 | 40000 | 46666.6667 |
+-------------+-------------+-------------+-------------+
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 |
+--------+---------+---------+------------+
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 |
+--------+---------+---------+------------+
Aggregate Functions in SQL
select Count(*) from Faculty;
+----------+
| Count(*) |
+----------+
| 6 |
+----------+
Aggregate Functions in SQL
select Count(*) as NO_of_Faculties from Faculty;
+-----------------+
| NO_of_Faculties |
+-----------------+
| 6 |
+-----------------+
Aggregate Functions in SQL
select Count(*) as No_of_Faculties,SUM(Salary) as SUM from
Faculty;
+-----------------+--------+
| No_of_Faculties | SUM |
+-----------------+--------+
| 6 | 280000 |
+-----------------+--------+
select Fname
from faculty
where(Salary between 35000 and 45000);
select Fname
from faculty
where(Salary between 35000 and 45000);
+---------+
| Fname |
+---------+
| Sandesh |
| Ramya |
| Ashwini |
+---------+
select Fname
from faculty
where(Salary not between 35000 and 45000);
select Fname
from faculty
where(Salary not between 35000 and 45000);
+----------+
| Fname |
+----------+
| Kavitha |
| Priyanka |
| Bharathi |
+----------+
Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname;
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 |
+-------+----------------+
select dname ,Count(Dname) as NO_of_Students
from student
where semester = 7
group by Dname;
Group By & Having Clauses in SQL
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 |
+-------+----------------+
Group By & Having Clauses in SQL
select dname , semester, Count(*) as NO_of_Students
from student
group by Dname, semester;
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 |
+-------+----------+----------------+
Group By & Having Clauses in SQL
select dname ,semester, Count(*) as NO_of_Students
from student
group by semester;
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 |
+-------+----------+----------------+
Group By & Having Clauses in SQL
select dname ,Count(Dname) as NO_of_Students
from student
group by Dname
having count(dname)>2;
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 |
+-------+----------------+
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;
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 |
+-------+----------------+
Group By & Having Clauses in SQL
select dname ,Count(*) as NO_of_Students
from student
where semester = 7
group by Dname
having count(*)>1;
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 |
+-------+----------------+
select DName,Count(*) as No_of_Faculties, AVG(salary)
from faculty
group by DName;
select DName,Count(*) as No_of_Faculties,AVG(salary)
from faculty
group by DName;
Output:
| DName | No_of_Faculties | AVG(salary) |
+-------+-----------------+-------------+
| CSE | 2 | 50000.0000 |
| ECE | 2 | 47500.0000 |
| EEE | 1 | 40000.0000 |
| ISE | 1 | 45000.0000 |
+-------+-----------------+-------------+
select DName, Count(*) as No_of_Faculties, AVG(salary)
from faculty
group by Dname
having avg(salary)< 50000;
select DName,Count(*) as No_of_Faculties,AVG(salary)
from faculty
group by DName
having avg(salary)< 50000;
Output:
| DName | No_of_Faculties | AVG(salary) |
+-------+-----------------+-------------+
| ECE | 2 | 47500.0000 |
| EEE | 1 | 40000.0000 |
| ISE | 1 | 45000.0000 |
+-------+-----------------+-------------+
Select DName,Count(*) as No_of_Faculties,AVG(salary)
from faculty
where salary >= 45000
group by DName
having avg(salary) < 50000;
Output:
| DName | No_of_Faculties | AVG(salary) |
+-------+-----------------+-------------+
| ECE | 2 | 47500.0000 |
| ISE | 1 | 45000.0000 |
+-------+-----------------+-------------+
create view CSE as select USN,name
from Student
where Dname ='CSE' or Dname = 'ISE';
Views (Virtual Tables) in SQL
create view CSE as
select USN,name
from Student
where Dname ='CSE' or Dname = 'ISE';
Views (Virtual Tables) in SQL
mysql> select * from CSE;
USN | name |
+-------+-----------+
| CS006 | Sharath |
| CS025 | Shilpa |
| CS045 | Rakshitha |
| IS001 | Architha |
| IS025 | Harsha |
+-------+-----------+
create view Sem_5 as
select USN ,name
from student
where semester = 5;
Views (Virtual Tables) in SQL
select * from Sem_5;
Output:
| USN | name |
+-------+-----------+
| CS045 | Rakshitha |
| EC020 | Sushma |
| IS001 | Architha |
| ME016 | Akash |
| ME030 | Nithin |
+-------+-----------+
select C.USN,C.name
from CSE C,Sem_5 S
where C.USN = S.USN;
Output:
| USN | name |
+-------+-----------+
| CS045 | Rakshitha |
| IS001 | Architha |
+-------+-----------+
Sailors Reserves
Boats
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 |
+---------+
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 |
+---------+
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 |
+---------+
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 |
+---------+
+---------+
| sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
Correlated Nested Queries
Nested Queries – Set Comparison Operators
Find sailors whose rating is better than some sailor called Horatio
-----+
| sid |
+-----+
| 31 |
| 32 |
| 58 |
| 71 |
| 74 |
+-----+
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 |
+-----+
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 |
+-----+
Nested Queries – Set Comparison Operators
Find the names of the sailors who have reserved all boats
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 |
+--------+
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 |
+--------+
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
Joins in SQL
select *
from Country C INNER JOIN State S
on C.CountryId = S.CountryId;
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 |
+-----------+-------------+---------+-----------+-----------+
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 |
+-------------+-----------+
Joins in SQL
select *
from Country C LEFT OUTER JOIN State S
on C.CountryId = S.CountryId;
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 |
+-----------+-------------+---------+-----------+-----------+
Joins in SQL
select *
from Country C RIGHT OUTER JOIN State S
on C.CountryId = S.CountryId;
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 |
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;
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 |
+-----------+-------------+---------+-----------+------------+
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
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;
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.
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;
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
insert into Sales_new values(102,10,10,180,0);
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
102 10 10 180 1800
Sales_new
select * from Sales_new;
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
102 8 10 90 900
create trigger t1
before insert
on Sales
for each row
begin
set new.total=new.qtysold*new.price;
update item
set qoh=qoh-new.qtysold
where item.itemid=new.itemid;
end;
Sales
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
102 8 10 90 900
Sales
insert into Sales values(103,10,15,180,0);
itemid iDesc qoh price category
8 perfume 59 380 Cosemetics
10 kb 44 180 toys
Item
sid itemid qtysold price total
101 8 10 90 0
102 8 10 90 900
103 10 15 180 2700
Sales
select * from Sales;
select * from Item;
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
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).
DBMS SQL
DBMS SQL
DBMS SQL
DBMS SQL
DBMS SQL
DBMS SQL

Mais conteúdo relacionado

Último

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 

Último (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

DBMS SQL

  • 1. Database Management System SQL By: Sharmila Chidaravalli Asst. Prof. Department of ISE Global Academy of Technology
  • 2. Dname location civil Block B CSE First Floor ECE Second Floor EEE Third Floor ISE First Floor Mech Block A Department USN name age semester Dname CS025 Shilpa 22 7 CSE EC006 Deepthi 20 3 ECE IS001 Architha 20 5 ISE IS025 Harsha 20 3 ISE ME016 Akash 21 5 Mech ME030 Nithin 21 5 Mech ME045 Pramod 22 7 Mech Student
  • 3. Basic Retrieval Query : SELECT-FROM-WHERE SQL select name from student where semester = 7;
  • 4. Basic Retrieval Query : SELECT-FROM-WHERE SQL select name from student where semester = 7; Output: +--------+ | name | +--------+ | Shilpa | | Pramod | +--------+
  • 5. select name ,Dname from student where semester = 5 and Dname = ‘Mech’;
  • 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 | +-------+--------+-------+----------+
  • 13. select S.name, S.semester, D.Dname, D.location from student S, Department D where S.Dname = D.Dname;
  • 14. select S.name, S.semester, D.Dname, D.location from student S, Department D where S.Dname = D.Dname; +----------+----------+-------+--------------+ | name | semester | Dname | location | +----------+----------+-------+--------------+ | Shilpa | 7 | CSE | First Floor | | Deepthi | 3 | ECE | Second Floor | | Architha | 5 | ISE | First Floor | | Harsha | 3 | ISE | First Floor | | Akash | 5 | Mech | Block A | | Nithin | 5 | Mech | Block A | | Pramod | 7 | Mech | Block A | +----------+----------+-------+--------------+
  • 15. select USN from student; +-------+ | USN | +-------+ | CS025 | | EC006 | | IS001 | | IS025 | | ME016 | | ME030 | | ME045 | +-------+ Unspecified WHERE Clause and Use of Asterisk
  • 16. +----------+--------------+ | name | location | +----------+--------------+ | Shilpa | Block B | | Shilpa | First Floor | | Shilpa | Second Floor | | Shilpa | Third Floor | | Shilpa | First Floor | | Shilpa | Block A | | Deepthi | Block B | | Deepthi | First Floor | | Deepthi | Second Floor | | Deepthi | Third Floor | | Deepthi | First Floor | | Deepthi | Block A | | Architha | Block B | | Architha | First Floor | | Architha | Second Floor | | Architha | Third Floor | | Architha | First Floor | | Architha | Block A | | Harsha | Block B | | Harsha | First Floor | | Harsha | Second Floor | | Harsha | Third Floor | | Harsha | First Floor | | Harsha | Block A | | Akash | Block B | | Akash | First Floor | | Akash | Second Floor | | Akash | Third Floor | | Akash | First Floor | | Akash | Block A | | Nithin | Block B | | Nithin | First Floor | | Nithin | Second Floor | | Nithin | Third Floor | | Nithin | First Floor | | Nithin | Block A | | Pramod | Block B | | Pramod | First Floor | | Pramod | Second Floor | | Pramod | Third Floor | | Pramod | First Floor | | Pramod | Block A | +----------+--------------+ select name, location from student, Department;
  • 17. select * from student, Department; +-------+----------+------+----------+-------+-------+--------------+ | USN | name | age | semester | Dname | Dname | location | +-------+----------+------+----------+-------+-------+--------------+ | CS025 | Shilpa | 22 | 7 | CSE | civil | Block B | | CS025 | Shilpa | 22 | 7 | CSE | CSE | First Floor | | CS025 | Shilpa | 22 | 7 | CSE | ECE | Second Floor | | CS025 | Shilpa | 22 | 7 | CSE | EEE | Third Floor | | CS025 | Shilpa | 22 | 7 | CSE | ISE | First Floor | | CS025 | Shilpa | 22 | 7 | CSE | Mech | Block A | | EC006 | Deepthi | 20 | 3 | ECE | civil | Block B | | EC006 | Deepthi | 20 | 3 | ECE | CSE | First Floor | | EC006 | Deepthi | 20 | 3 | ECE | ECE | Second Floor | | EC006 | Deepthi | 20 | 3 | ECE | EEE | Third Floor | | EC006 | Deepthi | 20 | 3 | ECE | ISE | First Floor | | EC006 | Deepthi | 20 | 3 | ECE | Mech | Block A | | IS001 | Architha | 20 | 5 | ISE | civil | Block B | | IS001 | Architha | 20 | 5 | ISE | CSE | First Floor | | IS001 | Architha | 20 | 5 | ISE | ECE | Second Floor | | IS001 | Architha | 20 | 5 | ISE | EEE | Third Floor | | IS001 | Architha | 20 | 5 | ISE | ISE | First Floor | | IS001 | Architha | 20 | 5 | ISE | Mech | Block A | | IS025 | Harsha | 20 | 3 | ISE | civil | Block B | | IS025 | Harsha | 20 | 3 | ISE | CSE | First Floor | | IS025 | Harsha | 20 | 3 | ISE | ECE | Second Floor | | IS025 | Harsha | 20 | 3 | ISE | EEE | Third Floor | | IS025 | Harsha | 20 | 3 | ISE | ISE | First Floor | | IS025 | Harsha | 20 | 3 | ISE | Mech | Block A | | ME016 | Akash | 21 | 5 | Mech | civil | Block B | | ME016 | Akash | 21 | 5 | Mech | CSE | First Floor | | ME016 | Akash | 21 | 5 | Mech | ECE | Second Floor | | ME016 | Akash | 21 | 5 | Mech | EEE | Third Floor | | ME016 | Akash | 21 | 5 | Mech | ISE | First Floor | | ME016 | Akash | 21 | 5 | Mech | Mech | Block A | | ME030 | Nithin | 21 | 5 | Mech | civil | Block B | | ME030 | Nithin | 21 | 5 | Mech | CSE | First Floor | | ME030 | Nithin | 21 | 5 | Mech | ECE | Second Floor | | ME030 | Nithin | 21 | 5 | Mech | EEE | Third Floor | | ME030 | Nithin | 21 | 5 | Mech | ISE | First Floor | | ME030 | Nithin | 21 | 5 | Mech | Mech | Block A | | ME045 | Pramod | 22 | 7 | Mech | civil | Block B | | ME045 | Pramod | 22 | 7 | Mech | CSE | First Floor | | ME045 | Pramod | 22 | 7 | Mech | ECE | Second Floor | | ME045 | Pramod | 22 | 7 | Mech | EEE | Third Floor | | ME045 | Pramod | 22 | 7 | Mech | ISE | First Floor | | ME045 | Pramod | 22 | 7 | Mech | Mech | Block A | +-------+----------+------+----------+-------+-------+--------------+
  • 18. Substring Pattern matching and Arthimetic Operators select USN, name, Dname from student where name like '%a';
  • 19. Substring Pattern matching and Arthimetic Operators select USN, name, Dname from student where name like '%a'; Output: | USN | name | Dname | +-------+----------+-------+ | CS025 | Shilpa | CSE | | IS001 | Architha | ISE | | IS025 | Harsha | ISE | +-------+----------+-------+
  • 20. select USN from student where USN like '_S_ _ _';
  • 21. select USN from student where USN like '_S_ _ _'; Output: | USN | +-------+ | CS025 | | IS001 | | IS025 | +-------+
  • 22. select name from student where name like '____h%';
  • 23. select name from student where name like '____h%'; Output: | name | +--------+ | Harsha | | Akash | +--------+
  • 24. select USN,name,semester+1 as New_sem from student;
  • 25. select USN, name, semester+1 as New_sem from student; Output: | USN | name | New_sem | +-------+----------+---------+ | CS025 | Shilpa | 8 | | EC006 | Deepthi | 4 | | IS001 | Architha | 6 | | IS025 | Harsha | 4 | | ME016 | Akash | 6 | | ME030 | Nithin | 6 | | ME045 | Pramod | 8 | +-------+----------+---------+
  • 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 | +-------+--------+---------+---------------------+
  • 28. Admin_id Name Salary AD001 Kiran 10000 AD004 Mary 20000 AD010 Sandesh 40000 AD025 Krishna 25000 AD045 Sahana 20000 Admin_Staff FID FNAme Salary DName AD010 Sandesh 40000 EEE CSE10 Kavitha 50000 CSE CSE35 Priyanka 50000 CSE ECE05 Ramya 45000 ECE ECE25 Bharathi 50000 ECE ISE25 Ashwini 45000 ISE Faculty
  • 29. SELECT DISTINCT Salary FROM Faculty; Table as Sets
  • 30. SELECT DISTINCT Salary FROM Faculty; Output: | Salary | +--------+ | 40000 | | 50000 | | 45000 | +--------+
  • 31. SELECT ALL Salary FROM Faculty; FID FNAme Salary DName AD010 Sandesh 40000 EEE CSE10 Kavitha 50000 CSE CSE35 Priyanka 50000 CSE ECE05 Ramya 45000 ECE ECE25 Bharathi 50000 ECE ISE25 Ashwini 45000 ISE Faculty
  • 32. FID FNAme Salary DName AD010 Sandesh 40000 EEE CSE10 Kavitha 50000 CSE CSE35 Priyanka 50000 CSE ECE05 Ramya 45000 ECE ECE25 Bharathi 50000 ECE ISE25 Ashwini 45000 ISE Faculty Output: | Salary | +--------+ | 40000 | | 50000 | | 50000 | | 45000 | | 50000 | | 45000 | +--------+ SELECT ALL Salary FROM Faculty;
  • 33. (select distinct Salary from Faculty) UNION (select distinct Salary from Admin_Staff);
  • 34. (select distinct Salary from Faculty) UNION (select distinct Salary from Admin_Staff); +--------+ | Salary | +--------+ | 40000 | | 50000 | | 45000 | | 10000 | | 20000 | | 25000 | +--------+
  • 35. Output : | Salary | +--------+ | 40000 | | 50000 | | 45000 | | 10000 | | 20000 | | 25000 | +--------+ (select Salary from Faculty) UNION (select Salary from Admin_Staff);
  • 36. (select Salary from Faculty) UNION ALL (select Salary from Admin_Staff);
  • 37. (select Salary from Faculty) UNION ALL (select Salary from Admin_Staff); Output : | Salary | +--------+ | 40000 | | 50000 | | 50000 | | 45000 | | 50000 | | 45000 | | 10000 | | 20000 | | 40000 | | 25000 | | 20000 | +--------+
  • 38. (select Fname from Faculty) UNION (select Name from Admin_Staff);
  • 39. (select Fname from Faculty) UNION (select Name from Admin_Staff); +----------+ | Fname | +----------+ | Sandesh | | Kavitha | | Priyanka | | Ramya | | Bharathi | | Ashwini | | Kiran | | Mary | | Krishna | | Sahana | +----------+
  • 40. (select Name,Salary from Admin_Staff) UNION (select Fname,Salary from Faculty);
  • 41. (select Name,Salary from Admin_Staff) UNION (select Fname,Salary from Faculty); +----------+--------+ | Name | Salary | +----------+--------+ | Kiran | 10000 | | Mary | 20000 | | Sandesh | 40000 | | Krishna | 25000 | | Sahana | 20000 | | Kavitha | 50000 | | Priyanka | 50000 | | Ramya | 45000 | | Bharathi | 50000 | | Ashwini | 45000 | +----------+--------+
  • 42. select distinct Salary from Faculty inner join Admin_Staff using(Salary); +--------+ | Salary | +--------+ | 40000 | +--------+
  • 43. select distinct Salary from Faculty left join Admin_Staff using(Salary); +--------+ | Salary | +--------+ | 40000 | | 50000 | | 45000 | +--------+
  • 44. select Salary from Faculty left join Admin_Staff using(Salary); +--------+ | Salary | +--------+ | 40000 | | 50000 | | 50000 | | 45000 | | 50000 | | 45000 | +--------+
  • 45. select Fname from Faculty order by FName DESC; +----------+ | Fname | +----------+ | Sandesh | | Ramya | | Priyanka | | Kavitha | | Bharathi | | Ashwini | +----------+
  • 46. select Fname from Faculty order by FName ASC; +----------+ | Fname | +----------+ | Ashwini | | Bharathi | | Kavitha | | Priyanka | | Ramya | | Sandesh | +----------+
  • 47. select Fname,Salary from Faculty order by Fname; +----------+--------+ | Fname | Salary | +----------+--------+ | Ashwini | 45000 | | Bharathi | 50000 | | Kavitha | 50000 | | Priyanka | 50000 | | Ramya | 45000 | | Sandesh | 40000 | +----------+--------+
  • 48. select Fname,Salary from Faculty order by Salary ASC; +----------+--------+ | Fname | Salary | +----------+--------+ | Sandesh | 40000 | | Ramya | 45000 | | Ashwini | 45000 | | Kavitha | 50000 | | Priyanka | 50000 | | Bharathi | 50000 | +----------+--------+
  • 49. Aggregate Functions in SQL Select COUNT(FName) from Faculty; +--------------+ | COUNT(FName) | +--------------+ | 6 | +--------------+
  • 50. Aggregate Functions in SQL Select COUNT(FName) from Faculty where Dname='ECE'; +--------------+ | COUNT(FName) | +--------------+ | 2 | +--------------+
  • 51. Aggregate Functions in SQL select SUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary) -> from Faculty; +-------------+-------------+-------------+-------------+ | SUM(Salary) | MAX(Salary) | MIN(Salary) | AVG(Salary) | +-------------+-------------+-------------+-------------+ | 280000 | 50000 | 40000 | 46666.6667 | +-------------+-------------+-------------+-------------+
  • 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 | +-----------------+--------+
  • 57. select Fname from faculty where(Salary between 35000 and 45000);
  • 58. select Fname from faculty where(Salary between 35000 and 45000); +---------+ | Fname | +---------+ | Sandesh | | Ramya | | Ashwini | +---------+
  • 59. select Fname from faculty where(Salary not between 35000 and 45000);
  • 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 | +-------+----------------+
  • 75. select DName,Count(*) as No_of_Faculties, AVG(salary) from faculty group by DName;
  • 76. select DName,Count(*) as No_of_Faculties,AVG(salary) from faculty group by DName; Output: | DName | No_of_Faculties | AVG(salary) | +-------+-----------------+-------------+ | CSE | 2 | 50000.0000 | | ECE | 2 | 47500.0000 | | EEE | 1 | 40000.0000 | | ISE | 1 | 45000.0000 | +-------+-----------------+-------------+
  • 77. select DName, Count(*) as No_of_Faculties, AVG(salary) from faculty group by Dname having avg(salary)< 50000;
  • 78. select DName,Count(*) as No_of_Faculties,AVG(salary) from faculty group by DName having avg(salary)< 50000; Output: | DName | No_of_Faculties | AVG(salary) | +-------+-----------------+-------------+ | ECE | 2 | 47500.0000 | | EEE | 1 | 40000.0000 | | ISE | 1 | 45000.0000 | +-------+-----------------+-------------+
  • 79. Select DName,Count(*) as No_of_Faculties,AVG(salary) from faculty where salary >= 45000 group by DName having avg(salary) < 50000; Output: | DName | No_of_Faculties | AVG(salary) | +-------+-----------------+-------------+ | ECE | 2 | 47500.0000 | | ISE | 1 | 45000.0000 | +-------+-----------------+-------------+
  • 80. create view CSE as select USN,name from Student where Dname ='CSE' or Dname = 'ISE'; Views (Virtual Tables) in SQL
  • 81. create view CSE as select USN,name from Student where Dname ='CSE' or Dname = 'ISE'; Views (Virtual Tables) in SQL mysql> select * from CSE; USN | name | +-------+-----------+ | CS006 | Sharath | | CS025 | Shilpa | | CS045 | Rakshitha | | IS001 | Architha | | IS025 | Harsha | +-------+-----------+
  • 82. create view Sem_5 as select USN ,name from student where semester = 5; Views (Virtual Tables) in SQL select * from Sem_5; Output: | USN | name | +-------+-----------+ | CS045 | Rakshitha | | EC020 | Sushma | | IS001 | Architha | | ME016 | Akash | | ME030 | Nithin | +-------+-----------+
  • 83. select C.USN,C.name from CSE C,Sem_5 S where C.USN = S.USN; Output: | USN | name | +-------+-----------+ | CS045 | Rakshitha | | IS001 | Architha | +-------+-----------+
  • 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 | +---------+
  • 89. +---------+ | sname | +---------+ | Dustin | | Lubber | | Horatio | +---------+ Correlated Nested Queries
  • 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;
  • 110. 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 insert into Sales_new values(102,10,10,180,0);
  • 111. 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 102 10 10 180 1800 Sales_new select * from Sales_new;
  • 112. 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 102 8 10 90 900 create trigger t1 before insert on Sales for each row begin set new.total=new.qtysold*new.price; update item set qoh=qoh-new.qtysold where item.itemid=new.itemid; end; Sales
  • 113. 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 102 8 10 90 900 Sales insert into Sales values(103,10,15,180,0);
  • 114. itemid iDesc qoh price category 8 perfume 59 380 Cosemetics 10 kb 44 180 toys Item sid itemid qtysold price total 101 8 10 90 0 102 8 10 90 900 103 10 15 180 2700 Sales select * from Sales; select * from Item;
  • 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).