DBMS Practice Questions Relational Algebra, SQL, Triggers and Views
DBMS Practice Questions Relational Algebra, SQL, Triggers and Views
A. Select (σ)
1. Write relational algebra expression to retrieve employees in department 10.
2. Write relational algebra expression to retrieve employees with salary > 50000.
4. Write relational algebra expression to retrieve employees working more than 40 hours
on any project.
B. Project (π)
6. Write relational algebra expression to list all employee names.
C. Joins
11. Write relational algebra expression to retrieve employee names with their department
names.
12. Write relational algebra expression to get employee-project pairs with hours.
13. Write relational algebra expression to get project names with department names.
14. Write relational algebra expression to retrieve employees and project names.
15. Write relational algebra expression to get employee names and hours worked.
D. Aggregate Functions
16. Write relational algebra expression to find average salary of all employees.
17. Write relational algebra expression to find number of employees in each department.
19. Write relational algebra expression to find number of employees working on each
project.
20. Write relational algebra expression to find average hours spent on each project.
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.
23. Write relational algebra expression to find employees working on all projects.
25. Write relational algebra expression to find employees working on both Project A and
Project B.
SQL Questions
26. Insert a new student into the Student table.
Answer: INSERT INTO Student (SID, SName, DeptID, Age) VALUES (101, 'Ravi',
1, 20);
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'));
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);
Views
37. Create a view to display student names and their department names.
38. Create a view to show student names and their marks in ‘DBMS’ only.
41. Create a trigger to log every new student added into a log table.
42. Create a trigger to automatically set age = 18 if age is not provided during student
insert.