0% found this document useful (0 votes)
10 views

Abhiman LAB

lablab

Uploaded by

abhimansaharan74
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)
10 views

Abhiman LAB

lablab

Uploaded by

abhimansaharan74
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/ 6

LAB5-

ASSIGNMENT
Abhiman Singh-
E23CSEU0174

Questions:
1. Display the details of students sorted by age in
ascending order.
2. Display the number of students in each semester.
3. Display the courses which are having more than 2
credits sorted in descending order.
4. Display courses offered in semesters 3 and 5 ordered by
course code.
5. List courses by descending order of credits and then
ascending order of semesters.
6. Find the maximum credits among courses for each
instructor.
7. Display the number of courses offered by each instructor
in semester 3.
8. Create a view that lists instructors and the number of
courses they teach.
9. Create a view that lists students and the number of
courses they are registered for.

Abhiman Singh – E23CSEU0174


10. Create a view that shows students registered in
semester 3, ordered by age.
11. Create a view that shows the count of instructors in
each department, ordered by department name.

CODES:
CREATE DATABASE University;
USE University;

CREATE TABLE Courses (


Course_code VARCHAR(10) PRIMARY KEY,
Course_title VARCHAR(50),
Credits INT,
Semester INT,
Instructor_id INT
);

CREATE TABLE Instructors (


Instructor_id INT PRIMARY KEY,
Instructor_name VARCHAR(50),
Department VARCHAR(50)
);

CREATE TABLE Students (


Registration_id VARCHAR(10) PRIMARY KEY,
S_Name VARCHAR(50),
Semester INT,
Course_code VARCHAR(10),

Abhiman Singh – E23CSEU0174


Age INT,
Instructor_id INT,
FOREIGN KEY (Course_code) REFERENCES Courses(Course_code),
FOREIGN KEY (Instructor_id) REFERENCES
Instructors(Instructor_id)
);

INSERT INTO Courses (Course_code, Course_title, Credits, Semester,


Instructor_id) VALUES
('CS301', 'DBMS', 4, 3, 101),
('CS302', 'OS', 3, 3, 102),
('CS101', 'Physics', 3, 1, 103),
('CS303', 'Java', 3, 3, 104),
('CS505', 'Discrete Math', 4, 5, 105),
('CS701', 'Cloud Computing', 3, 7, 106),
('EC303', 'DLD', 2, 5, 107),
('MBA501', 'Economics', 3, 5, 108);

INSERT INTO Instructors (Instructor_id, Instructor_name,


Department) VALUES
(101, 'Ashish', 'CSE'),
(102, 'Shivam', 'CSE'),
(103, 'Ravi', 'ME'),
(104, 'Sujit', 'CSE'),
(105, 'Alka', 'CSE'),
(106, 'Suman', 'CSE'),
(107, 'Mukund', 'EC'),
(108, 'Rohit', 'MBA');

Abhiman Singh – E23CSEU0174


INSERT INTO Students (Registration_id, S_Name, Semester,
Course_code, Age, Instructor_id) VALUES
('SCS012', 'Arjun', 3, 'CS301', 17, 101),
('SCS013', 'Anamika', 3, 'CS301', 19, 101),
('SCS088', 'Divya', 3, 'CS302', 20, 102),
('SCS014', 'Diya', 1, 'CS101', 19, 103),
('SCS015', 'Abhishek', 3, 'CS303', 17, 104),
('SCS090', 'Shiva', 3, 'CS303', 16, 104),
('SCS071', 'Rahul', 5, 'CS505', 21, 105),
('SCS043', 'Rupam', 3, 'EC303', 22, 107),
('SCS017', 'Hina', 3, 'EC303', 23, 107),
('SCS022', 'Alam', 5, 'MBA501', 21, 108),
('SCS056', 'Satya', 7, 'CS701', 23, 106);

SELECT *
FROM Students
ORDER BY Age ASC;

SELECT Semester, COUNT(*) AS Number_of_Students


FROM Students
GROUP BY Semester;

SELECT *
FROM Courses
WHERE Credits > 2
ORDER BY Credits DESC;

SELECT *

Abhiman Singh – E23CSEU0174


FROM Courses
WHERE Semester IN (3, 5)
ORDER BY Course_code ASC;

SELECT *
FROM Courses
ORDER BY Credits DESC, Semester ASC;

SELECT Course_code, MAX(Credits) AS Max_Credits


FROM Courses
GROUP BY Course_code;

SELECT Instructor_id, COUNT(*) AS Number_of_Courses


FROM Courses
WHERE Semester = 3
GROUP BY Instructor_id;

CREATE VIEW Instructor_Course_Count AS


SELECT i.Instructor_id, i.Instructor_name, COUNT(c.Course_code) AS
Number_of_Courses
FROM Instructors i
LEFT JOIN Courses c ON i.Instructor_id = c.Instructor_id
GROUP BY i.Instructor_id, i.Instructor_name;

CREATE VIEW Student_Course_Count AS


SELECT s.Registration_id, s.S_Name, COUNT(c.Course_code) AS
Number_of_Courses
FROM Students s

Abhiman Singh – E23CSEU0174


LEFT JOIN Courses c ON s.Course_code = c.Course_code
GROUP BY s.Registration_id, s.S_Name;

CREATE VIEW Semester_3_Students AS


SELECT *
FROM Students
WHERE Semester = 3
ORDER BY Age ASC;

CREATE VIEW Instructor_Department_Count AS


SELECT Department, COUNT(*) AS Number_of_Instructors
FROM Instructors
GROUP BY Department
ORDER BY Department ASC;

SELECT * FROM Instructor_Course_Count;


SELECT * FROM Student_Course_Count;
SELECT * FROM Semester_3_Students;
SELECT * FROM Instructor_Department_Count;

Abhiman Singh – E23CSEU0174

You might also like