0% found this document useful (0 votes)
0 views5 pages

DBMS Practice Questions Relational Algebra, SQL, Triggers and Views

The document contains practice questions and solutions related to Database Management Systems (DBMS), focusing on relational algebra, SQL queries, triggers, and views. It includes various expressions to retrieve and manipulate data from schemas for Employee, Department, Project, Student, and Marks. Each section provides specific examples of operations such as selection, projection, joins, aggregate functions, and SQL commands for data manipulation.

Uploaded by

mastermantejas
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)
0 views5 pages

DBMS Practice Questions Relational Algebra, SQL, Triggers and Views

The document contains practice questions and solutions related to Database Management Systems (DBMS), focusing on relational algebra, SQL queries, triggers, and views. It includes various expressions to retrieve and manipulate data from schemas for Employee, Department, Project, Student, and Marks. Each section provides specific examples of operations such as selection, projection, joins, aggregate functions, and SQL commands for data manipulation.

Uploaded by

mastermantejas
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/ 5

DBMS PRACTICE QUESTIONS WITH SOLUTIONS

TOPIC 1: RELATIONAL ALGEBRA


Schema:
- Employee(EmpID, EmpName, DeptID, Salary)
- Department(DeptID, DeptName, Location)
- Project(ProjID, ProjName, DeptID)
- WorksOn(EmpID, ProjID, Hours)

A. Select (σ)
1. Write relational algebra expression to retrieve employees in department 10.

Answer: σ DeptID=10 (Employee)

2. Write relational algebra expression to retrieve employees with salary > 50000.

Answer: σ Salary>50000 (Employee)

3. Write relational algebra expression to retrieve projects by department 20.

Answer: σ DeptID=20 (Project)

4. Write relational algebra expression to retrieve employees working more than 40 hours
on any project.

Answer: σ Hours>40 (WorksOn)

5. Write relational algebra expression to retrieve departments located in Bangalore.

Answer: σ Location='Bangalore' (Department)

B. Project (π)
6. Write relational algebra expression to list all employee names.

Answer: π EmpName (Employee)

7. Write relational algebra expression to list distinct department locations.

Answer: π Location (Department)

8. Write relational algebra expression to list employee IDs and salaries.

Answer: π EmpID, Salary (Employee)

9. Write relational algebra expression to list all project names.

Answer: π ProjName (Project)


10. Write relational algebra expression to list all department names.

Answer: π DeptName (Department)

C. Joins
11. Write relational algebra expression to retrieve employee names with their department
names.

Answer: π EmpName, DeptName (Employee ⨝ Department)

12. Write relational algebra expression to get employee-project pairs with hours.

Answer: Employee ⨝ WorksOn

13. Write relational algebra expression to get project names with department names.

Answer: Project ⨝ Department

14. Write relational algebra expression to retrieve employees and project names.

Answer: π EmpName, ProjName (Employee ⨝ WorksOn ⨝ Project)

15. Write relational algebra expression to get employee names and hours worked.

Answer: π EmpName, Hours (Employee ⨝ WorksOn)

D. Aggregate Functions
16. Write relational algebra expression to find average salary of all employees.

Answer: γ AVG(Salary) (Employee)

17. Write relational algebra expression to find number of employees in each department.

Answer: γ DeptID, COUNT(EmpID) (Employee)

18. Write relational algebra expression to find maximum salary.

Answer: γ MAX(Salary) (Employee)

19. Write relational algebra expression to find number of employees working on each
project.

Answer: γ ProjID, COUNT(EmpID) (WorksOn)

20. Write relational algebra expression to find average hours spent on each project.

Answer: γ ProjID, AVG(Hours) (WorksOn)

E. Set Theory
21. Write relational algebra expression to find employees not working on any project.
Answer: π EmpID (Employee) - π EmpID (WorksOn)

22. Write relational algebra expression to find DeptIDs in Employee but not in
Department.

Answer: π DeptID (Employee) - π DeptID (Department)

23. Write relational algebra expression to find employees working on all projects.

Answer: σ COUNT(ProjID)=TotalProjects (γ EmpID, COUNT(ProjID) (WorksOn))

24. Write relational algebra expression to find employees in department 10 or 20.

Answer: σ DeptID=10 (Employee) ∪ σ DeptID=20 (Employee)

25. Write relational algebra expression to find employees working on both Project A and
Project B.

Answer: π EmpID (σ ProjID='A' (WorksOn)) ∩ π EmpID (σ ProjID='B'


(WorksOn))

TOPIC 2: SQL QUERIES


Schema:
- Student(SID, SName, DeptID, Age)
- Department(DeptID, DName)
- Marks(SID, Subject, Marks)

SQL Questions
26. Insert a new student into the Student table.

Answer: INSERT INTO Student (SID, SName, DeptID, Age) VALUES (101, 'Ravi',
1, 20);

27. Delete all students from DeptID = 2.

Answer: DELETE FROM Student WHERE DeptID = 2;

28. Update age of student with SID = 101 to 21.

Answer: UPDATE Student SET Age = 21 WHERE SID = 101;

29. Add a new column Email to Student table.

Answer: ALTER TABLE Student ADD Email VARCHAR(100);

30. Select names of students older than 18.

Answer: SELECT SName FROM Student WHERE Age > 18;

31. Select student names and their department names.


Answer: SELECT S.SName, D.DName FROM Student S JOIN Department D ON
S.DeptID = D.DeptID;

32. Select students who scored more than average in DBMS.

Answer: SELECT SName FROM Student WHERE SID IN (SELECT SID FROM
Marks WHERE Subject = 'DBMS' AND Marks > (SELECT AVG(Marks) FROM
Marks WHERE Subject = 'DBMS'));

33. Count number of students in each department.

Answer: SELECT DeptID, COUNT(*) FROM Student GROUP BY DeptID;

34. Find departments with more than 5 students.

Answer: SELECT DeptID FROM Student GROUP BY DeptID HAVING


COUNT(*) > 5;

35. Select top 5 students by marks in DBMS.

Answer: SELECT SID, Marks FROM Marks WHERE Subject = 'DBMS' ORDER
BY Marks DESC LIMIT 5;

36. Select students who scored more than all students from DeptID=2.

Answer: SELECT SID FROM Marks WHERE Marks > ALL (SELECT Marks
FROM Student S JOIN Marks M ON S.SID = M.SID WHERE DeptID = 2);

TOPIC 3: TRIGGERS AND VIEWS

Views
37. Create a view to display student names and their department names.

Answer: CREATE VIEW StudentDeptView AS


SELECT S.SName, D.DName FROM Student S JOIN Department D ON S.DeptID
= D.DeptID;

38. Create a view to show student names and their marks in ‘DBMS’ only.

Answer: CREATE VIEW DBMSMarks AS


SELECT S.SName, M.Marks FROM Student S JOIN Marks M ON S.SID = M.SID
WHERE M.Subject = 'DBMS';

39. Create a view to show average marks per subject.

Answer: CREATE VIEW AvgSubjectMarks AS


SELECT Subject, AVG(Marks) AS AvgMarks FROM Marks GROUP BY Subject;
Triggers
40. Create a trigger to prevent inserting marks greater than 100.

Answer: CREATE TRIGGER check_marks_limit


BEFORE INSERT ON Marks
FOR EACH ROW
BEGIN
IF NEW.Marks > 100 THEN
SET NEW.Marks = 100;
END IF;
END;

41. Create a trigger to log every new student added into a log table.

Answer: CREATE TABLE StudentLog (SID INT, SName VARCHAR(100),


LogTime TIMESTAMP);

CREATE TRIGGER log_student_insert


AFTER INSERT ON Student
FOR EACH ROW
BEGIN
INSERT INTO StudentLog (SID, SName, LogTime)
VALUES (NEW.SID, NEW.SName, CURRENT_TIMESTAMP);
END;

42. Create a trigger to automatically set age = 18 if age is not provided during student
insert.

Answer: CREATE TRIGGER default_age_trigger


BEFORE INSERT ON Student
FOR EACH ROW
BEGIN
IF NEW.Age IS NULL THEN
SET NEW.Age = 18;
END IF;
END;

You might also like