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

Assignment Constraints and OPerators

Uploaded by

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

Assignment Constraints and OPerators

Uploaded by

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

1.

Create a table named TIU_BCS with the following attributes:


I. Student_Name varchar(20) NOT NULL,
II. Roll_number Numeric(4) PRIMARY KEY,
III. Semester varchar(10) NOT NULL,
IV. Age numeric(2) NOT NULL (AGE SHOULD BE >18).
V. GENDER VARCHAR(20) (Impose check Constraint on GENDER
– CHECK IF GENDER IN ‘MALE, ‘FEMALE’, ‘OTHER’)
VI. Phone_no varchar(10), ( Impose UNIQUE CONSTRAINT on
Phone_no).
VII. City varchar(20) (Impose default value for CITY as
‘KOLKATA’).
VIII. Dept_name varchar(20) NOT NULL,
IX. Dept id numeric(3) NOT NULL,
X. CR_rollno NUMERIC(4) (Referencing the PRIMARY KEY OF
THE SAME TABLE).

ANSWER :

CREATE TABLE TIU_BCS (


Student_Name VARCHAR(20) NOT NULL,
Roll_number NUMERIC(4) PRIMARY KEY,
Semester VARCHAR(10) NOT NULL,
Age NUMERIC(2) NOT NULL CHECK (Age > 18),
Gender VARCHAR(20) CHECK (Gender IN ('MALE', 'FEMALE',
'OTHER')),
Phone_no VARCHAR(10) UNIQUE,
City VARCHAR(20) DEFAULT 'KOLKATA',
Dept_name VARCHAR(20) NOT NULL,
Dept_id NUMERIC(3) NOT NULL,
CR_rollno NUMERIC(4),
FOREIGN KEY (CR_rollno) REFERENCES TIU_BCS(Roll_number));

2. Insert 15 rows in the TIU_BCS table

ANSWER:
INSERT INTO TIU_BCS (Student_Name, Roll_number, Semester, Age, Gender,
Phone_no, City, Dept_name, Dept_id, CR_rollno)
VALUES
('Alice Smith', 1001, 'Semester 1', 20, 'FEMALE', '9876543210',
'KOLKATA', 'CSE', 101, NULL),
('Bob Johnson', 1002, 'Semester 2', 22, 'MALE', '9876543211',
'KOLKATA', 'ECE', 102, NULL),
('Carol Davis', 1003, 'Semester 1', 21, 'FEMALE', '9876543212',
'KOLKATA', 'IT', 103, NULL),
('David Wilson', 1004, 'Semester 3', 23, 'MALE', '9876543213',
'KOLKATA', 'MECH', 104, NULL),
('Eva Brown', 1005, 'Semester 2', 22, 'FEMALE', '9876543214',
'KOLKATA', 'CIVIL', 105, NULL),
('Frank Moore', 1006, 'Semester 4', 24, 'MALE', '9876543215',
'KOLKATA', 'CSE', 101, 1001),
('Grace Taylor', 1007, 'Semester 1', 20, 'FEMALE', '9876543216',
'KOLKATA', 'ECE', 102, 1002),
('Hank Lee', 1008, 'Semester 2', 21, 'MALE', '9876543217',
'KOLKATA', 'IT', 103, 1003),
('Ivy Clark', 1009, 'Semester 3', 22, 'FEMALE', '9876543218',
'KOLKATA', 'MECH', 104, 1004),
('Jack Lewis', 1010, 'Semester 4', 23, 'MALE', '9876543219',
'KOLKATA', 'CIVIL', 105, 1005),
('Kimberly Martin', 1011, 'Semester 1', 20, 'FEMALE',
'9876543220', 'KOLKATA', 'CSE', 101, NULL),
('Liam Walker', 1012, 'Semester 2', 22, 'MALE', '9876543221',
'KOLKATA', 'ECE', 102, 1002),
('Mona Scott', 1013, 'Semester 3', 21, 'FEMALE', '9876543222',
'KOLKATA', 'IT', 103, 1003),
('Nate Adams', 1014, 'Semester 4', 23, 'MALE', '9876543223',
'KOLKATA', 'MECH', 104, 1004),
('Olivia Thompson', 1015, 'Semester 1', 20, 'FEMALE',
'9876543224', 'KOLKATA', 'CIVIL', 105, 1005),
('Paul King', 1016, 'Semester 2', 22, 'MALE', '9876543225',
'KOLKATA', 'CSE', 101, 1001),
('Quinn Green', 1017, 'Semester 3', 21, 'FEMALE', '9876543226',
'KOLKATA', 'ECE', 102, 1002),
('Rita Baker', 1018, 'Semester 4', 23, 'FEMALE', '9876543227',
'KOLKATA', 'IT', 103, 1003),
('Steve Wright', 1019, 'Semester 1', 20, 'MALE', '9876543228',
'KOLKATA', 'MECH', 104, 1004),
('Tina Harris', 1020, 'Semester 2', 22, 'FEMALE', '9876543229',
'KOLKATA', 'CIVIL', 105, 1005);

3. Display all the details of the student whose age is 22 and


department is either CSE OR ECE.

ANSWER:

SELECT * FROM TIU_BCS WHERE Age = 22 AND Dept_name IN ('CSE',


'ECE');
OR
SELECT * FROM TIU_BCS WHERE Age = 22 AND (Dept_name = ‘CSE’ OR
Dept_name = ‘ECE’);
4. Retrieve Roll number, Name and city of the students whose name
contains “ic” substring in them.

SELECT Roll_number, Student_Name, City FROM TIU_BCS WHERE


Student_Name LIKE '%ic%';

5. Retrieve Roll number, Name and city of the students whose name
starts with ‘O’.

SELECT Roll_number, Student_Name, City FROM TIU_BCS WHERE


Student_Name LIKE 'O%';

6. Display the Roll number, Student_name, Dept_name of the students


whose department is not ECE and not IT.

SELECT Roll_number, Student_Name, Dept_name FROM TIU_BCS WHERE


Dept_name NOT IN ('ECE', 'IT');

OR

SELECT Roll_number, Student_Name, Dept_name FROM TIU_BCS WHERE NOT


Dept_name='ECE' AND NOT Dept_name='IT';

OR

SELECT Roll_number, Student_Name, Dept_name FROM TIU_BCS EXCEPT


SELECT Roll_number, Student_Name, Dept_name FROM TIU_BCS WHERE
Dept_name IN ('IT', 'ECE');

7. Retrieve the details of the student who are CR.

SELECT DISTINCT s.* FROM TIU_BCS as s JOIN TIU_BCS as c ON


s.Roll_number = c.CR_rollno;

8. Update the table by adding another new column Email (varchar(20)).

ALTER TABLE TIU_BCS ADD Email varchar(20);

9. Update the Emails of Employee having names Alice, Grace, and Kate.

UPDATE TIU_BCS set Email= ‘[email protected]’ where Student_Name=


‘Alice';

UPDATE TIU_BCS set Email= ‘[email protected]’ where Student_Name=


‘Grace';
UPDATE TIU_BCS set Email= ‘[email protected]’ where Student_Name=
‘Kate';

10. Display the details of the Employees who don’t have any valid
email id.

SELECT * FROM TIU_BCS WHERE Email IS NULL;

11. Create a table Student_CR with the following attributes:

I. CR_id INT PRIMARY KEY, -- Unique identifier for the class


representative
II. Roll_number NUMERIC(4) NOT NULL, -- Roll number of the student who is
a CR
III. CR_start_date DATE NOT NULL, -- Date when the student started being a
CR
IV. CR_end_date DATE, -- Date when the student ended being a CR (NULL if
currently a CR)
V. Department VARCHAR(50), -- Department to which the CR belongs
VI. FOREIGN KEY (Roll_number) REFERENCES TIU_BCS (Roll_number) --
Foreign key referencing the main student table

ANSWER:
CREATE TABLE Student_CR
(CR_id INT PRIMARY KEY,
Roll_number NUMERIC(4) NOT NULL,
CR_start_date DATE NOT NULL,
CR_end_date DATE,
Department VARCHAR(50),
FOREIGN KEY (Roll_number) REFERENCES TIU_BCS (Roll_number));

12. Insert CR details of CSE, ECE and IT department in Student_CR


table.
ANSWER:
INSERT INTO Student_CR (CR_id, Roll_number, CR_start_date,
CR_end_date, Department) VALUES

(1, 1001, '2024-01-15', NULL, 'CSE'),


(2, 1002, '2024-03-01', '2024-08-31', 'ECE'),

(3, 1003, '2024-02-01', NULL, 'IT'),

(4, 1004, '2024-04-01', '2024-09-30', 'MECH'),

(5, 1005, '2024-05-01', NULL, 'CIVIL');

13. Find the students’ roll numbers who are considered as class
representative.

ANSWER:

SELECT Roll_number FROM TIU_BCS3 INTERSECT SELECT Roll_number FROM


Student_CR;

14. Create another table marks with the following attributes:


Roll_number NUMERIC(4) NOT NULL,
Subject VARCHAR(50) NOT NULL,
Marks NUMERIC(5, 2) NOT NULL,
Exam_date DATE NOT NULL
PRIMARY KEY (Roll_number, Subject, Exam_date),
FOREIGN KEY (Roll_number) REFERENCES TIU_BCS(Roll_number)

CREATE TABLE Marks (


Roll_number NUMERIC(4) NOT NULL,
Subject VARCHAR(50) NOT NULL,
Marks NUMERIC(5, 2) NOT NULL,
Exam_date DATE NOT NULL,
PRIMARY KEY (Roll_number, Subject, Exam_date),
FOREIGN KEY (Roll_number) REFERENCES TIU_BCS(Roll_number));

15. Add 10 rows to the Marks table.

INSERT INTO Marks (Roll_number, Subject, Marks, Exam_date)


VALUES
(1001, 'Mathematics', 88.50, '2024-05-15'),
(1001, 'Physics', 92.00, '2024-05-16'),
(1002, 'Chemistry', 75.25, '2024-05-15'),
(1002, 'Biology', 80.00, '2024-05-16'),
(1003, 'Computer Science', 85.75, '2024-05-15'),
(1003, 'Mathematics', 90.25, '2024-05-16'),
(1004, 'Mechanical Engineering', 78.00, '2024-05-15'),
(1004, 'Thermodynamics', 82.50, '2024-05-16'),
(1005, 'Structural Analysis', 79.00, '2024-05-15'),
(1005, 'Surveying', 83.75, '2024-05-16');

16. List all students and their marks for mathematics.

SELECT T.Student_Name, M.Subject, M.Marks FROM TIU_BCS T JOIN Marks


M ON T.Roll_number = M.Roll_number WHERE M.Subject = 'Mathematics';

17. Find all students who are class representatives and their marks.

SELECT T.Student_Name, M.Subject, M.Marks


FROM TIU_BCS T
JOIN Marks M ON T.Roll_number = M.Roll_number
WHERE T.Roll_number IN (SELECT CR_rollno FROM TIU_BCS WHERE
CR_rollno IS NOT NULL);

18. Retrieve details of students whose marks in 'Physics' are greater


than 90.

SELECT T.Student_Name, T.Roll_number, M.Marks FROM TIU_BCS T JOIN


Marks M ON T.Roll_number = M.Roll_number WHERE M.Subject = 'Physics'
AND M.Marks > 90;

19. List all class representatives along with their department and
marks for a specific subject.

SELECT T.Student_Name, T.Dept_name, M.Subject, M.Marks FROM TIU_BCS


T JOIN Marks M ON T.Roll_number = M.Roll_number WHERE T.Roll_number
= T.CR_rollno AND M.Subject = 'Computer Science';

20. Find students who are not class representatives and list their
marks.

SELECT T.Student_Name, M.Subject, M.Marks


FROM TIU_BCS T
JOIN Marks M ON T.Roll_number = M.Roll_number
WHERE T.Roll_number NOT IN (SELECT CR_rollno FROM TIU_BCS WHERE
CR_rollno IS NOT NULL);

21. Retrieve the average marks for each department.

SELECT T.Dept_name, AVG(M.Marks) AS Average_Marks FROM TIU_BCS T


JOIN Marks M ON T.Roll_number = M.Roll_number GROUP BY T.Dept_name;
22. List all students and their marks, but only include those with
marks greater than or equal to 80.

SELECT T.Student_Name, M.Subject, M.Marks FROM TIU_BCS T JOIN Marks M


ON T.Roll_number = M.Roll_number WHERE M.Marks >= 80;

23. Get the details of students and their marks that belong to a
specific department and are class representatives.

SELECT T.Student_Name, T.Dept_name, M.Subject, M.Marks


FROM TIU_BCS T
JOIN Marks M ON T.Roll_number = M.Roll_number
WHERE T.Dept_name = 'CSE'
AND T.Roll_number = T.CR_rollno;

24. List students who have not taken any exams (i.e., have no entries
in the Marks table).

SELECT T.Student_Name, T.Roll_number


FROM TIU_BCS T
LEFT JOIN Marks M ON T.Roll_number = M.Roll_number
WHERE M.Roll_number IS NULL;

25. Find all subjects and their average marks for students in a
specific department.

SELECT M.Subject, AVG(M.Marks) AS Average_Marks FROM TIU_BCS T


JOIN Marks M ON T.Roll_number = M.Roll_number WHERE T.Dept_name =
'IT' GROUP BY M.Subject;

You might also like