Cs2258 Database Management Systems Lab
Cs2258 Database Management Systems Lab
Cs2258 Database Management Systems Lab
LIST OF EXPERIMENTS
10. Reports.
11.Database Design and implementation (Mini Project).
using strings
System Configuration
b. Find the course id and titles of all courses taught by an instructor named
'Srinivasan'
mysql> select course.course_id,course.title from course where
course.course_id=some(select teaches.course_id from teaches where
teaches.id=(select instructor.id from instructor where instructor.name='Srinivasan'));
+-----------+----------------------------+
| course_id | title |
+-----------+----------------------------+
| CS-315 | Robotics |
+-----------+----------------------------+
+-------+-------+
| id | name |
+-------+-------+
| 12312 | Dhaya |
+-------+-------+
d. Find IDs of instructors who have never taught any course. If the result of
your query is empty, add appropriate data (and include corresponding insert
statements) to ensure the result is not empty.
mysql> select id from instructor where id not in (select id from teaches);
+-------+
| id |
+-------+
| 33456 |
| 58583 |
| 76543 |
+-------+
+-----------+----------------------------+
| course_id | title |
+-----------+----------------------------+
+-----------+----------------------------+
+-----------+----------------------------+
| course_id | title |
+-----------+----------------------------+
| CS-555 | DSP |
+-----------+----------------------------+
count(s_id)>2
from advisor group by s_id);
8. Write SQL queries for the following:
1. Delete all information in the database, which is more than 10 years old.
Add data as necessary to verify your query.
delete from takes where year < (year(CURRENT_DATE) –
10);
delete from teaches where year < (year(CURRENT_DATE) – 10);
delete from section where year < (year(CURRENT_DATE) – 10);
2. Delete the course CS 101. All courses which have this as a prereq
should remove this from its prereq set. Create a cascade constraint and
verify.
DELETE FROM course WHERE course_id='CS-101'
3. Nested Queries & Join Queries
1. Find the maximum number of teachers for any single course section.
2. Find all departments that have the minimum number of
instructors, using a subquery; order the result by department
name in descending order.
select dept_name,count(*) as num from instructor group by
(dept_name) order by num desc ;
3. For each student, compute the total credits they have
successfully completed, i.e. total credits of courses they have
taken, for which they have a non-null grade other than 'F'. Do
NOT use the tot_creds attribute of student.
mysql> select student.id, name, sum(course.credits) from
takes, student, course where grade <> 'F' and grade is not
null and takes.course_id = course.course_id and
student.id=takes.id group by id, name;
4. Find the number of students who have been taught (at any
time) by an instructor named 'Srinivasan'. Make sure you
count a student only once even if the student has taken more
than one course from Srinivasan.
mysql> select count(distinct takes.ID) from
instructor,takes,teaches,section where
takes.course_id=teaches.course_id and takes.sec_id =
teaches.sec_id and takes.semester = teaches.semester and
takes.year = teaches.year and
section.course_id=teaches.course_id and
section.semester=teaches.semester and
section.sec_id=teaches.sec_id and section.year=teaches.year
and teaches.ID=instructor.ID and
instructor.name='Srinivasan';
5. Find the name of all instructors who get the highest salary in
their department.
mysql> select name from instructor I1 , (select max(salary) as
maxsal,dept_name from instructor I2 group by dept_name ) I2
where (I2.maxsal = I1.salary and I1.dept_name =
I2.dept_name);
6. Find all students who have taken all courses taken by
instructor 'Srinivasan'. (This is the division operation of
relational algebra.) You can implement it by counting the
number of courses taught by Srinivasan, and for each student
(i.e. group by student), find the number of courses taken by
that student, which were taught by Srinivasan. Make sure to
count each course ID only once.
1. Find the IDs of all students who were taught by an instructor named Einstein; make
sure there are no duplicates in the result.
2. Find the highest salary of any instructor.
3. Find all instructors earning the highest salary (there may be more than one with the same
salary).
4. Find the enrollment of each section that was offered in Autumn 2009.
5. Find the maximum enrollment, across all sections, in Autumn 2009.
6. Find the sections that had the maximum enrollment in Autumn 2009.
7. Suppose you are given a relation grade points(grade, points), which provides a conversion
from letter grades in the takes relation to numeric scores; for example an “A” grade could be
specified to correspond to 4 points, an “A−” to 3.7 points, a “B+” to 3.3 points, a “B” to 3
points, and so on. The grade points earned by a student for a course offering (section) is
defined as the number of credits for the course multiplied by the numeric points for the grade
that the student received.
Given the above relation, and our university schema, write each of the following
queries in SQL. You can assume for simplicity that no takes tuple has the null value for grade.
a. Find the total gradepoints earned by the student with ID 12345, across all courses
taken by the student.
b. Find the gradepoint average (GPA) for the above student, that is, the total
gradepoints divided by the total credits for the associated courses.
c. Find the ID and the gradepoint average of every student.
9. Find the maximum number of teachers for any single course section.
10. Find all departments that have the minimum number of instructors, using a
subquery; order the result by department name in descending order.
11. For each student, compute the total credits they have successfully completed, i.e.
total credits of courses they have taken, for which they have a non-null grade
other than 'F'. Do NOT use the tot_creds attribute of student.
12. Find the number of students who have been taught (at any time) by an instructor
named 'Srinivasan'. Make sure you count a student only once even if the student
has taken more than one course from Srinivasan.
13. Find the name of all instructors who get the highest salary in their department.
14. Find all students who have taken all courses taken by instructor 'Srinivasan'. (This
is the division operation of relational algebra.) You can implement it by counting
the number of courses taught by Srinivasan, and for each student (i.e. group by
student), find the number of courses taken by that student, which were taught by
Srinivasan. Make sure to count each course ID only once.
15. Display a list of all instructors, showing their ID, name, and the number of sections
that they have taught. Make sure to show the number of sections as 0 for instructors who
have not taught any section. Your query should use an outerjoin, and should not use
scalar subqueries.
16. Write the same query as above, but using a scalar subquery, with out outerjoin.
17. Display the list of all course sections offered in Spring 2010, along with the names of
the instructors teaching the section. If a section has more than one instructor, it should
appear as many times in the result as it has instructors. If it does not have any instructor,
it should still appear in the result with the instructor name set to “ —”.
18. Display the list of all departments, with the total number of in structors in each
department, without using scalar subqueries. Make sure to correctly handle departments
with no instructors.
5. High level programming language extensions
1. Write a SQL procedure, which accepts a name and prints welcome message for
the given name.
2. Write a SQL procedure to accept the details of a student and print in on the screen
using procedure.
3. Write a SQL procedure to demonstrate swapping of two numbers.
4. Write a SQL procedure that will accept an integer and find out its factorial value
and also print its Fibonacci series.
5. Write a SQL function, which returns the highest paid staff
6. Write a SQL function, which returns the average salary of given department.
7. Write a SQL function, which returns number of student who got above 60 marks
in a given subject.
8. Write a SQL function to find whether the given string is palindrome or not.