Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

COMPUTER ENGINEERING

Database Systems(DS) Assignment

Name:-Nishant Sharma
Roll No.:-11912090
Section:-CS-B(6)
EXPERIMENT Number=7

#Write the following queries in the SQL using set operations union, intersect, except, etc.
(for the above mentioned database of a university)

1.Find courses taught in Fall 2015 or in Spring 2016. (use union operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.
mysql> select c.course_id,title
-> from section s,course c
-> where c.course_id=s.course_id and year=1
-> union
-> select c.course_id,title
-> from section s,course c
-> where c.course_id=s.course_id and year=2;

2.Find courses taught in Fall 2015 and in Spring 2016. (use intersect operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.
mysql> select distinct c.course_id,title
-> from section s,course c
-> where c.course_id=s.course_id and year=1 and c.course_id in(select distinct c.course_id
-> from course c,section s
-> where c.course_id=s.course_id and year=2);

3.Find courses taught in Fall 2015 but not in Spring 2016. (use except operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id, title


-> from section s,course c
-> where c.course_id = s.course_id and year = 1 and c.course_id not in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2);
4.Find courses taught/offered by Computer Engg. department in Fall 2015 or mechanical
department in Spring 2016. (use union operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id,title,dept_name


-> from course c, section s
-> where c.course_id = s.course_id and year = 1 and dept_name = 'cs'
-> union
-> select c.course_id,title,dept_name
-> from course c, section s
-> where c.course_id = s.course_id and year = 2 and dept_name = 'mechanical';

5.Find courses taught/offered by Computer Engg. department in Fall 2015 and ECE
department in Spring 2016. (use intersect operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id,title,dept_name


-> from course c, section s
-> where c.course_id = s.course_id and year = 1 and dept_name = 'cs' and c.course_id in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2 and dept_name = 'ece');

6.Find courses taught/offered by Computer Engg. department in Fall 2015 but not by ECE
department in Spring 2016. (use except operation)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id,title,dept_name


-> from course c, section s
-> where c.course_id = s.course_id and year = 1 and dept_name = 'cs' and c.course_id not in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2 and dept_name = 'ece');
7.List all instructors of the Physics department in alphabetic order.

mysql> select*from instructor


-> where dept_name='physics'
-> order by name;

8.List all coursed offered by Physics department in alphabetic order.


I have used cs department instead of Physics department.

mysql> select*from course where dept_name='cs' order by title;

Making use of sub-queries and "in", "not in" connectives, write queries for the following:

9.Find courses taught in Fall 2015 and in Spring 2016. (sub-query, in)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select distinct c.course_id,title


-> from section s,course c
-> where c.course_id=s.course_id and year=1 and c.course_id in(select distinct c.course_id
-> from course c,section s
-> where c.course_id=s.course_id and year=2);

10.Find courses taught in Fall 2015 but not in Spring 2016. (sub-query, not in)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id, title


-> from section s,course c
-> where c.course_id = s.course_id and year = 1 and c.course_id not in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2);
11.Find courses taught/offered by Computer Engg. department in Fall 2015 and ECE
department in Spring 2016. (sub-query, in)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id,title,dept_name


-> from course c, section s
-> where c.course_id = s.course_id and year = 1 and dept_name = 'cs' and c.course_id in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2 and dept_name = 'ece');

12.Find courses taught/offered by Computer Engg. department in Fall 2015 but not by ECE
department in Spring 2016. (sub-query, not in)
I have used Fall 2015 as 1 and Spring 2016 as 2.

mysql> select c.course_id,title,dept_name


-> from course c, section s
-> where c.course_id = s.course_id and year = 1 and dept_name = 'cs' and c.course_id not in(select c.course_id
-> from course c, section s
-> where c.course_id = s.course_id and year = 2 and dept_name = 'ece');

13.Find total no. of distinct students who have taken courses taught in different sections by
an instructor. (Take a valid instructor ID or name)

mysql> select count(*) as'No. of distinct students'


-> from takes
-> where(course_id,sec_id,semester,year) in(select course_id,sec_id,semester,year
-> from teaches
-> where I_ID=21202004);
14.Find names of instructors with salary greater than that of some (at least one) instructors
of the Physics department.
mysql> select name
-> from instructor
-> where salary>(select min(salary)
-> from instructor
-> where dept_name='physics');

15.Implement the following query and observe the results


select name
from instructor
where salary > some (select salary
from instructor
where Dept name ="Physics")

Based on the observations of the result, write the query in English language. Give your
opinion on any different version of the above query.

Ans=Observation=Same result as of 14th query.


Query in English= Names of instructors with salary greater than that of some (at least one) instructors of the Physics
department.
16.Find names of all the instructors whose salaries are greater than that of the salaries of
all instructors belonging to the Physics department. (sub-query, "all")

mysql> select name as 'name of instructors whose salaries are greater than salaries of all the instuctors of physics
department'
-> from instructor
-> where salary>all(select salary
-> from instructor
-> where dept_name='physics');

17.Using update statement and set keyword


(i)."increase salaries of the instructors whose salaries are greater than 50000" by 15%"
(ii)."decrease salaries of the instructors whose salaries are greater than 50000" by 5%"
Before:-

(i). mysql> update instructor


-> set salary=salary*(1.15)
-> where salary>50000;
Query OK, 10 rows affected (0.01 sec)
Rows matched: 10 Changed: 10 Warnings: 0
(ii). mysql> update instructor
-> set salary=salary*(0.95)
-> where salary>50000;
Query OK, 10 rows affected (0.00 sec)
Rows matched: 10 Changed: 10 Warnings: 0

18.List all departments along with no. of instructors in each department.


mysql> select i.dept_name, count(*) as 'No. of instructors'
-> from department d,instructor i
-> where d.dept_name=i.dept_name
-> group by dept_name;

19.List all departments along with no. of students in each department.

mysql> select s.dept_name, count(*) as 'No. of students'


-> from department d,student s
-> where d.dept_name=s.dept_name
-> group by dept_name;
20.Use a sub-query to derive a relation in "from" clause and write query for the following:

"Find department names along with average salaries of the instructors of that department
where average salaries are greater than Rs. 45000"

mysql> select i.dept_name,i.Average_salary


-> from(select dept_name,avg(salary) as Average_salary
-> from instructor
-> group by dept_name)i
-> where i.Average_salary>45000;

"Retrieve name of department along with no. of students who have earned total credits
more than 7"

mysql> select a.dept_name,a.total_credit


-> from(select d.dept_name,sum(s.tot_credit) as 'total_credit'
-> from department d,student s
-> where d.dept_name=s.dept_name
-> group by d.dept_name)a
-> where a.total_credit>7;
"Find name of department along with no. of instructors belonging to the department"

mysql> select count(*) as 'No. of Instructors',dept_name


-> from instructor
-> group by dept_name;

21.List instructor name, dept name, and no. of courses taught by the instructor in Spring
2016.
I have used Spring 2016 as 2.

mysql> select name as 'Instructor name',dept_name,count(*) as 'No. of courses'


-> from instructor i,teaches t
-> where i.i_id=t.i_id and year=2
-> group by i.i_id;

22.Retrieve departments that have budget amount more than 5 lacs.

mysql> select*from department


-> where budget>500000;
23.Find the names of all students who have taken at least one course taught by Computer
Engg. department; make sure there are no duplicate names in the result.
mysql> select distinct name as 'Student name'
-> from student,takes t,course c
-> where id=s_id and c.course_id=t.course_id and c.dept_name='cs'
-> group by s_id;

THANK YOU

You might also like