Database Management System · Unit 6
SQL and Query Processing
Exam-focused notes for SQL and Query Processing (Database Management System, BIT202): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
What this unit covers
- SQL data definition language
- SQL data manipulation language
- Create table statements
- Update and delete operations
- Cascade on delete
- Outer joins
- Group by and having clauses
- Views and view creation
- Relational algebra operations
SQL data definition language
Write SQL statements to create a table and update value in a table. Also mention the use of cascade on delete while creating the table. Use your own assumption for the table and update conditions. [5]
Explanation of each clause: Clause Purpose ------ PRIMARY KEY Uniquely identifies each row in the table NOT NULL Ensures the column cannot have a NULL value FOREIGN KEY Creates a referential link between Employee.deptid and Department.deptid ON DELETE CASCA...
Full solved answer →Group by and having clauses
How is the Group By Having clause used in SQL statements? Mention the syntax and example. [5]
The GROUP BY clause is used to arrange identical data into groups. It is typically used with aggregate functions (COUNT, SUM, AVG, MAX, MIN) to perform calculations on each group of rows. The HAVING clause is used to filter groups based on a condition. It i...
Full solved answer →Views and view creation
What is the view? How can you create a view in SQL? Illustrate with an example. [5]
A view is a virtual table that is derived from one or more base tables (or other views) using a SQL query. It does not physically store data itself; instead, it stores the query definition, and the data is retrieved dynamically whenever the view is accessed...
Full solved answer →Relational algebra operations
Given following relations, write relational algebra statements for Person(Pid, pname, dob, paid) and Doctor(Did, pid, dname, dspeciality). a. Retrieving name of person who is doctor b. Retrieving all doctors whose speciality is pediatric. [5]
- Person (Pid, pname, dob, paid) - Doctor (Did, pid, dname, dspeciality) Note: Standard relational algebra operations are used below, consistent with Tribhuvan University CSIT curriculum. --- To find the names of persons who are also doctors, we need to joi...
Full solved answer →Retrieve the Employee Name, Department Number and Department Name of "John Smith" using Relational Algebra. The relations are given below. a. EMPLOYEE (Ssn, Fname, Lname, Bdate, Address, Sex, Salary, SuperSSN, Dno), b. PROJECT (Pnumber, Pname, Plocation, Dnum), c. WORKS ON (Essn, Pno, Hours), d. DEPARTMENT (Dname, Dnumber, Mgr_ssn, Mgr Start date) [5]
- EMPLOYEE (Ssn, Fname, Lname, Bdate, Address, Sex, Salary, SuperSSN, Dno) - DEPARTMENT (Dname, Dnumber, Mgr\ssn, Mgr\Start\date) Note: Only EMPLOYEE and DEPARTMENT relations are needed for this query. --- We need: Employee Name (Fname, Lname), Department N...
Full solved answer →SQL data manipulation language
From the relations given below, answer the following questions EMPLOYEE (Ssn, Ename, Bdate, Address, Sex, Salary, Super_ssn, Dno), DEPARTMENT (Dname, Dnumber, Mgr_ssn, Mgr_start_date), PROJECT (Pname, Pnumber, Plocation, Dnum), DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship), DEPT_LOCATIONS (Dnumber, Dlocation): a. Retrieve the name and address of all employees who work for the 'Computer' department in SQL, b. For each Department, retrieve the department number, the number of employees in the department, and their average salary using SQL, c. Retrieve the employees name and their Dname and Pname ordered by the employee's Dname, d. Retrieve the name and salary of all employees who work in department number 5 using Relational Algebra, e. Retrieve the name of the manager of each department using Relational Algebra.Following the above statement answer the following question.[10+0]
- EMPLOYEE (Ssn, Ename, Bdate, Address, Sex, Salary, Superssn, Dno) - DEPARTMENT (Dname, Dnumber, Mgrssn, Mgrstartdate) - PROJECT (Pname, Pnumber, Plocation, Dnum) - DEPENDENT (Essn, Dependentname, Sex, Bdate, Relationship) - DEPTLOCATIONS (Dnumber, Dlocati...
Full solved answer →From the relation given below, answer the following questions. EMPLOYEE (Ssn, Fname, Mnit, Name, Bdate, Address, Sex, Salary, Super_ssn, Dno) DEPARTMENT (Dname, Dnumber, Mgr_ssn, Mgr_start_date) PROJECT (Pname, Pnumber, Plocation, Dnum) DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship) DEPT_LOCATIONS (Dnumber, Dlocation): a. Retrieve all the attributes of employees who work for the 'Computer' department department no 5 using SQL, b. For each Department, retrieve the department number, the number of employees in department, and their average salary using SQL, c. For each project on which more than two employees work, retrieve the project number, name, and the number of employees who work on that project using SQL, d. Select the EMPLOYEE tuples whose department is 10, or those whose salary is greater than Rs 50,000 using Relational Algebra, e. Retrieve the names of employees who have no dependents using Relational Algebra.[10]
- EMPLOYEE (Ssn, Fname, Mnit, Name, Bdate, Address, Sex, Salary, Superssn, Dno) - DEPARTMENT (Dname, Dnumber, Mgrssn, Mgrstartdate) - PROJECT (Pname, Pnumber, Plocation, Dnum) - DEPENDENT (Essn, Dependentname, Sex, Bdate, Relationship) - WORKSON (Essn, Pno,...
Full solved answer →Consider a database system with following schemas; Hospital(hname, haddress, hspecilaity) Doctor(did, dname, dspecilization, ) Worksat(did, hname, workinghrs) Pharmacy(phname, hname, no_of_sales, total_revenue) Now write SQL statements and relational algebra statements for following queries: a. Select name of all doctors having specialization 'gyno', b. Select the name and address of hospital where working hours is 'day', c. Using natural join select the name of doctors whose working hours are 'night', d. Find the average salary of the doctors, e. Find names of hospital and their pharmacy which generate revenue more than 10000. Sort the result in descending order on the basis of pharmacy name.[10]
--- SQL: Relational Algebra: First apply selection on dspecialization = 'gyno', then project dname. --- SQL: Relational Algebra: Join Hospital and Worksat on hname, apply selection on workinghrs = 'day', then project required attributes. --- SQL: Note: Natu...
Full solved answer →Outer joins
Define Outer Join in SQL. Given following relations, show the results of left and right outer joins.
Employee:
| Eid | Ename | Address | Dno |
|---|---|---|---|
| 1 | Ram | KTM | 111 |
| 2 | Rita | PKR | 222 |
| 3 | Hari | KTM | 333 |
Department:
| Dno | Dname |
|---|---|
| 111 | HRM |
| 222 | Admin |
| 444 | Account |
[5]
Employee (E): Eid Ename Address Dno -------------------------- 1 Ram KTM 111 2 Rita PKR 222 3 Hari KTM 333 Department (D): Dno Dname -------------- 111 HRM 222 Admin 444 Account Join condition: E.Dno = D.Dno --- An Outer Join returns all rows from one or bo...
Full solved answer →Make Unit 6 stick
Practice BIT202 with flashcards & quizzes