0% found this document useful (0 votes)
19 views12 pages

Dbms Lab Assignment 5 Sujal Kumar 2023ugcs076

Uploaded by

Sujal Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Dbms Lab Assignment 5 Sujal Kumar 2023ugcs076

Uploaded by

Sujal Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

DBMS Assignment 5

NAME :- SUJAL KUMAR


ROLL NO. :- 2023UGCS076

Using the university schema, write the following queries. In some cases you
need to insert extra data to show the effect of a particular feature .This is
indicated with the question. You should then show not only the query, but
also the insert statements to add the required extra data.
1. Find the maximum and minimum enrollment across all sections, considering
only sections that had some enrollment, don't worry about those that had no
students taking that section.

CODE :-
SELECT MAX(ENROLL) AS max_enrolled , MIN(ENROLL) as min_enrolled

FROM (SELECT course_id , sec_id , year , count(id) AS ENROLL FROM takes

group by course_id , sec_id , year) as section;

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-
2. Find all sections that had the maximum enrollment (along with the
enrollment), using a subquery.

CODE :-
SELECT course_id, sec_id, semester, year, count(id) AS enrolled

FROM takes

GROUP BY course_id, sec_id, semester, year

HAVING count(id) = (SELECT max(enrollment)

FROM (SELECT count(id) AS enrollment

FROM takes

GROUP BY course_id, sec_id, semester, year) AS max_enroll);

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-

__________________________________________________
3. As in in Q1, but now also include sections with no students taking them; the
enrollment for such sections should be treated as 0. Do this in two different
ways (and create require data for testing)
1. Using a scalar subquery
2. Using aggregation on a left outer join (use the SQL natural left outer
join syntax)

CODE :-
SELECT s.course_id , s.sec_id , s.semester , s.year ,
(SELECT COUNT(t.id) FROM takes t WHERE t.course_id = s.course_id AND
t.sec_id = s.sec_id
AND t.semester = s.semester AND t.year = s.year ) AS enrolled
FROM section s ;

SELECT s.course_id, s.sec_id, s.semester, s.year, (count(t.id)) as enrollment


FROM section s
NATURAL LEFT OUTER JOIN takes t
GROUP BY s.course_id, s.sec_id, s.semester, s.year ;

SCREENSHOT OF MYSQL CODE :-


OUTPUT :-

__________________________________________________
4. Find all courses whose identifier starts with the string "CS-1".
CODE :-

SELECT * FROM course WHERE course_id LIKE "CS-1%";

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-

_________________________________________________
5. Find instructors who have taught all the above courses
1. Using the "not exists ... except ..." structure
2. Using matching of counts which we covered in class (don't forget the
distinct clause!).

CODE :-
SELECT I.ID, I.name
FROM instructor I
WHERE NOT EXISTS (
SELECT DISTINCT T.course_id
FROM takes T
WHERE NOT EXISTS (
SELECT 1
FROM teaches TC
WHERE TC.course_id = T.course_id
AND TC.ID = I.ID
)
);

SELECT I.ID, I.name


FROM instructor I
JOIN teaches T ON I.ID = T.ID
GROUP BY I.ID, I.name
HAVING COUNT(DISTINCT T.course_id) = (SELECT COUNT(DISTINCT course_id)
FROM takes);

SCREENSHOT OF MYSQL CODE :-


OUTPUT :-

_________________________________________________
6. Insert each instructor as a student, with tot_creds = 0, in the same
department.

CODE :-
INSERT INTO student( ID , name , dept_name , tot_cred)
(SELECT CONCAT('I_',RIGHT(ID, 3)) , name , dept_name , 0 FROM instructor);

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-
_______________________
_______________________
7. Now delete all the newly added "students" above (note: already existing
students who happened to have tot_creds = 0 should not get deleted).

CODE :-
DELETE FROM student WHERE ID LIKE "I_%";

SCREENSHOT OF MYSQL CODE :-


OUTPUT :-

8. Some of you may have noticed that the tot_creds value for students did not
match the credits from courses they have taken. Write and execute query to
update tot_creds based on the credits passed, to bring the database back to
consistency. (This query is provided in the book/slides.)

CODE :-

UPDATE student
SET tot_cred = (
SELECT COALESCE(SUM(course.credits), 0)
FROM takes
JOIN course ON takes.course_id = course.course_id
WHERE takes.ID = student.ID AND takes.grade IS NOT NULL
);

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-

9. Update the salary of each instructor to 10000 times the number of course
sections they have taught.

CODE :-
UPDATE instructor
SET salary = 10000 * (SELECT (COUNT(*) +4) FROM teaches t
WHERE t.ID = instructor.ID);

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-

10.Create your own query: define what you want to do in English, then write
the query in SQL. Make it as difficult as you wish, the harder the better.

CODE :-

QUESTION :-
DISPLAY THE COURSE_ID , BUILDING , SEMESTER OF THE COURSES BEING
TAUGHT IN ROOM NUMBERS OF THREE DIGITS .

QUERY :-
SELECT course_id , building , semester FROM section AS s WHERE
s.room_number >= 100 AND s.room_number <= 999 ;

SCREENSHOT OF MYSQL CODE :-

OUTPUT :-

You might also like