0% found this document useful (0 votes)
28 views10 pages

Extra Question With Answer - DBMS

These are some extra questions

Uploaded by

dhruvraj1002
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)
28 views10 pages

Extra Question With Answer - DBMS

These are some extra questions

Uploaded by

dhruvraj1002
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
You are on page 1/ 10

1.

Relational Algebra Query for "Computer Science" Students

Schema: Student(StudentID, Name, Age, Department)


Query:
Retrieve the Name of students where the Department is "Computer Science".

Relational Algebra Expression:

πName(σDepartment = ’Computer Science’(Student))\pi_{\text{Name}}(\sigma_{\


text{Department = 'Computer Science'}}(Student))πName
(σDepartment = ’Computer Science’(Student))

Explanation:

● σ_Department = 'Computer Science'(Student) selects all rows where the department is


"Computer Science".

● π_Name projects the Name attribute from the selected rows.

2. E-R Diagram for Library System

Entities and Relationships:

1. Entities:

o Book: Attributes: BookID (PK), Title, Author.

o Member: Attributes: MemberID (PK), Name, Address.

2. Relationship:

o Issues: Attributes: IssueDate, DueDate.

Diagram:

● Book and Member entities are connected through the relationship Issues.

● BookID and MemberID are primary keys.

● Issues has foreign keys referring to BookID and MemberID.

Explanation:

● Each book can be issued to one member.

● The relationship Issues is many-to-many (many members can issue many books).

3. Relational Algebra Query for Employees in "Finance"

Schemas:

1. Employee(EmpID, Name, DeptID)

2. Department(DeptID, DeptName)

Query:
Find the Name of employees working in the "Finance" department.

Relational Algebra Expression:

πName(σDeptName = ’Finance’(Employee⋈Department))\pi_{\text{Name}}(\sigma_{\
text{DeptName = 'Finance'}}(Employee \bowtie Department))πName
(σDeptName = ’Finance’(Employee⋈Department))

Explanation:

● Employee ⨝ Department joins the two relations using the DeptID attribute.

● σ_DeptName = 'Finance' filters rows where DeptName is "Finance".

● π_Name projects the names of the employees.

4. Enforcing Domain Constraints in Relational Model


Relation: Car(RegistrationNo, Model, OwnerID)

Steps:

1. Unique Constraint on RegistrationNo:


Define RegistrationNo as the primary key to enforce uniqueness.

SQL Example:

sql

Copy code

CREATE TABLE Car (

RegistrationNo VARCHAR(20) PRIMARY KEY,

Model VARCHAR(50),

OwnerID INT

);

2. Foreign Key Constraint on OwnerID:


Ensure OwnerID refers to an existing Owner entity.

SQL Example:

sql

Copy code

CREATE TABLE Car (

RegistrationNo VARCHAR(20) PRIMARY KEY,

Model VARCHAR(50),

OwnerID INT,

FOREIGN KEY (OwnerID) REFERENCES Owner(OwnerID)

);

Explanation:

● The primary key ensures no duplicate RegistrationNo.

● The foreign key maintains referential integrity between Car and Owner.

5. Tuple Relational Calculus Expression


Schema: Employee(EmpID, Name, Salary)
Query: Retrieve the Name of employees earning more than $50,000.

Expression:

{e.Name ∣ e∈Employee∧e.Salary>50000}\{e.\text{Name} \ | \ e \in Employee \land e.\


text{Salary} > 50000\}{e.Name ∣ e∈Employee∧e.Salary>50000}

Explanation:

● The expression retrieves all tuples e from the Employee relation.

● It applies the condition e.Salary > 50000 and projects e.Name.

6. E-R Diagram and Explanation for Weak Entity Set

Scenario:

● Weak entity: Dependent (Attributes: DependentName, Relation).

● Strong entity: Employee (Attributes: EmpID (PK), Name, Salary).

● Relationship: Has.

Diagram:

● Dependent is connected to Employee via a one-to-many identifying relationship.

● Dependent has no primary key but uses EmpID (foreign key) as part of its composite
key.

Explanation:

● A weak entity like Dependent cannot exist without a corresponding Employee.

● Has is an identifying relationship, ensuring the dependency.

● The composite key for Dependent is (EmpID, DependentName).

SQL Example:

sql

Copy code

CREATE TABLE Dependent (

DependentName VARCHAR(50),

Relation VARCHAR(20),
EmpID INT,

PRIMARY KEY (EmpID, DependentName),

FOREIGN KEY (EmpID) REFERENCES Employee(EmpID)

);

Question: Write a SQL query to create a table Students with the following structure:

StudentID (Primary Key, integer, not null)

Name (varchar(50), not null)

Age (integer, check Age > 0)

DepartmentID (integer, foreign key referencing Departments table).

Answer:

sql

Copy code

CREATE TABLE Students (

StudentID INT PRIMARY KEY NOT NULL,

Name VARCHAR(50) NOT NULL,

Age INT CHECK (Age > 0),

DepartmentID INT,

FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)

);

Question: Write a SQL query to insert the following values into the Students table: 101,
'John', 22, 1.

Answer:

sql

Copy code
INSERT INTO Students (StudentID, Name, Age, DepartmentID)

VALUES (101, 'John', 22, 1);

Question: Alter the Students table to add a column Email (varchar(100), unique).

Answer:

sql

Copy code

ALTER TABLE Students

ADD Email VARCHAR(100) UNIQUE;

Question: Write a query to update the Age of the student with StudentID = 101 to 23.

Answer:

sql

Copy code

UPDATE Students

SET Age = 23

WHERE StudentID = 101;

Question: Write a query to delete a record where Name = 'John' in the Students table.

Answer:

sql

Copy code

DELETE FROM Students

WHERE Name = 'John';

Question: Create a query using the IN operator to fetch all students with DepartmentID as 1,
2, or 3.

Answer:
sql

Copy code

SELECT * FROM Students

WHERE DepartmentID IN (1, 2, 3);

Question: Write a query to fetch the total number of students in each department using
GROUP BY.

Answer:

sql

Copy code

SELECT DepartmentID, COUNT(*) AS TotalStudents

FROM Students

GROUP BY DepartmentID;

Question: Write a query to find the maximum Age of students in the Students table using an
aggregate function.

Answer:

sql

Copy code

SELECT MAX(Age) AS MaxAge

FROM Students;

Question: Retrieve the list of all students whose names start with the letter 'J' using a string
function.

Answer:

sql

Copy code
SELECT * FROM Students

WHERE Name LIKE 'J%';

Question: Write a SQL query to fetch records from the Students table and sort them by Age
in descending order.

Answer:

sql

Copy code

SELECT * FROM Students

ORDER BY Age DESC;

Question: Write a query to fetch students who are older than 20 but younger than 25 using the
BETWEEN operator.

Answer:

sql

Copy code

SELECT * FROM Students

WHERE Age BETWEEN 21 AND 24;

Question: Create a query to display the Name and DepartmentID of students who have a
NULL value in the Email field.

Answer:

sql

Copy code

SELECT Name, DepartmentID

FROM Students

WHERE Email IS NULL;

Question: Write a query to find the total number of students in the Students table, grouped by
DepartmentID, and include only departments with more than 5 students using HAVING.
Answer:

sql

Copy code

SELECT DepartmentID, COUNT(*) AS TotalStudents

FROM Students

GROUP BY DepartmentID

HAVING COUNT(*) > 5;

Question: Write a SQL query to perform an inner join between Students and Departments
tables to display StudentID, Name, and DepartmentName.

Answer:

sql

Copy code

SELECT Students.StudentID, Students.Name, Departments.DepartmentName

FROM Students

INNER JOIN Departments ON Students.DepartmentID = Departments.DepartmentID;

Question: Write a correlated subquery to find all students whose Age is greater than the
average Age in their respective department.

Answer:

sql

Copy code

SELECT StudentID, Name, Age, DepartmentID

FROM Students S1

WHERE Age > (SELECT AVG(Age)

FROM Students S2

WHERE S1.DepartmentID = S2.DepartmentID);

You might also like