0% found this document useful (0 votes)
7 views1 page

SQL and Relational Algebra Queries Guide

The document outlines SQL queries and relational algebra operations for managing hotel and employee databases. It includes tasks such as listing guest information, retrieving hotel bookings, and querying employee details based on various conditions. Additionally, it explains second and third normal forms with an example of normalization for a given relation.

Uploaded by

pradeepshettar50
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)
7 views1 page

SQL and Relational Algebra Queries Guide

The document outlines SQL queries and relational algebra operations for managing hotel and employee databases. It includes tasks such as listing guest information, retrieving hotel bookings, and querying employee details based on various conditions. Additionally, it explains second and third normal forms with an example of normalization for a given relation.

Uploaded by

pradeepshettar50
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

1.

Consider the following tables: Hotel(hotelno, name, city),Room(roomno, hotelno, type, price),
Booking(hotelno, guestno, datefrom, dateto, roomno), Guest(guestno, name, address)
Construct the SQL statements for the following:
i. List the names and addresses of all guest alphabetically ordered by name.
ii. List all family rooms with a price below Rs. 400 per night.
iii. List the details of booking for hotel “DasPrakash”.
iv. Retrieve all the details of the hotels in Mysuru city.

2. For the company relational schema given below construct the following queries using relational
algebra

 Employee (FName, Mint, LName, SSN, BDate, Address, Gender, Salary, SuperSSN, DNo)
 Department (DName, DNumber, MgrSSN, MgrStartDate)
 Project (PName, PNumber, PLocation, DNum)
 WorksOn (ESSN, PNo, Hours)
 Dependent (ESSN, DependentName, Gender, BDate, Relationship)

i) Retrieve the names of all employees in department 5 who work more than 10 hours per week on
“Product X” project
ii) Retrieve the ssn of all employees who either work in department number 5 or directly supervise
an employee who works in department number 5
iii) Find the names of employees who work on all projects controlled by department number 5.
iv) List the name and address of all employees who work for research dept

For the company DB Schema formulate the following queries in SQL


i) Retrieve the names of all employees whose supervisor’s supervisor has “888665555” for SSN
ii) Retrieve the names of employees who make at least $10,000 more than the employee who is paid
the least in the company
iii) For each department whose average employee salary is more than $30,000 retrieve the
department name and the number of employees working for that department
iv) Create a view that has the project name, controlling department name, number of employees and
total hours worked per week on the project for each project with more than one employee
working on it.
Create a view that has the department name, manager name and manager salary for every department

3. Explain second and third normal form with an example. Consider the relation
EMP_PROJ= {Ssn, Pnumber, Hours, Ename, Pname, Plocation}.
Assume {Ssn, Pnumber} as primary key. The dependencies are
Ssn, Pnumber  {Hours}
Ssn  {Ename}
Pnumber  {Pname, Plocation}.
Identify whether the given relation is in 2NF or not. If not, normalize the relation into 2NF.

Common questions

Powered by AI

The query is: SELECT e1.FName, e1.LName FROM Employee e1 JOIN Employee e2 ON e1.SuperSSN = e2.SSN JOIN Employee e3 ON e2.SuperSSN = e3.SSN WHERE e3.SSN = '888665555'. This uses two self-joins on the Employee table to traverse up the hierarchical structure, first linking employees to their supervisors, then those supervisors to their superiors with the specified SSN '888665555'.

Third Normal Form (3NF) ensures that every non-prime attribute is non-transitively dependent on the primary key, eliminating transitive dependencies. For example, if a table includes attributes like EmployeeID, DepartmentID, and DepartmentName, and where DepartmentName depends only on DepartmentID but not directly on EmployeeID, it violates 3NF. Converting it to 3NF requires separating the department info into a new table. This reduces redundancy and prevents anomalies during data operations.

The SQL query to achieve this is: SELECT DName, COUNT(SSN) FROM Employee e JOIN Department d ON e.DNo = d.DNumber GROUP BY DName HAVING AVG(Salary) > 30000. This groups the employees by department name, uses COUNT to get the number of employees, and filters those with an average salary greater than $30,000 using the HAVING clause.

The query to list names and addresses of all guests, sorted alphabetically by name from the Guest table, can be constructed using the SQL SELECT and ORDER BY clauses: SELECT name, address FROM Guest ORDER BY name ASC. This selects the name and address fields from the Guest table and orders the results alphabetically by the name field.

A SQL query to create this view is: CREATE VIEW ProjectSummary AS SELECT p.PName, d.DName, COUNT(w.ESSN) AS NumEmployees, SUM(w.Hours) AS TotalHours FROM Project p JOIN WorksOn w ON p.PNumber = w.PNo JOIN Department d ON p.DNum = d.DNumber GROUP BY p.PName, d.DName HAVING COUNT(w.ESSN) > 1. A view abstracts complex queries and provides a simplified, consistent interface for data retrieval, enhancing security and convenience.

The EMP_PROJ relation is not in Third Normal Form (3NF). A relation is in 3NF if it is in 2NF and all the attributes are functionally dependent only on the primary key. Given Ssn -> Ename and Pnumber -> {Pname, Plocation}, transitive dependency exists through non-prime attributes which violates 3NF. Normalizing further involves removing these transitive dependencies, forming separate relations for employee and project details.

The SQL statement to list all family rooms with a price below Rs. 400 would be: SELECT roomno, hotelno, type, price FROM Room WHERE type = 'family' AND price < 400. This selects all rooms from the Room table where the type is 'family' and the price is less than Rs. 400.

To retrieve all details of hotels located in Mysuru city, the SQL query is: SELECT * FROM Hotel WHERE city = 'Mysuru'. This selects all columns from the Hotel table where the city column matches 'Mysuru'.

A relation is in Second Normal Form (2NF) if it is in First Normal Form (1NF), and all non-key attributes are fully functional dependent on the primary key. In the EMP_PROJ relation with dependencies: Ssn, Pnumber → Hours, Ssn → Ename, Pnumber → Pname, Plocation, it is not in 2NF because Ename and {Pname, Plocation} depend on parts of the composite key (either Ssn or Pnumber). To normalize, create two new relations: EMP(Ssn, Ename) and PROJ(Pnumber, Pname, Plocation), maintaining Hours with Ssn and Pnumber in EMP_PROJ.

The relational algebra expression can be constructed by joining and selecting the necessary conditions:  (DNo=5 and PName='Product X' and Hours>10) (Employee ▹◃ (WorksOn ▹◃ Project)) ▹◃ Department. This expression first joins the Employee, WorksOn, and Project tables to filter employees in department 5 who work more than 10 hours on 'Product X'.

You might also like