Extra Question With Answer - DBMS
Extra Question With Answer - DBMS
Explanation:
1. Entities:
2. Relationship:
Diagram:
● Book and Member entities are connected through the relationship Issues.
Explanation:
● The relationship Issues is many-to-many (many members can issue many books).
●
Schemas:
2. Department(DeptID, DeptName)
Query:
Find the Name of employees working in the "Finance" department.
π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.
Steps:
SQL Example:
sql
Copy code
Model VARCHAR(50),
OwnerID INT
);
SQL Example:
sql
Copy code
Model VARCHAR(50),
OwnerID INT,
);
Explanation:
● The foreign key maintains referential integrity between Car and Owner.
Expression:
Explanation:
Scenario:
● Relationship: Has.
Diagram:
● Dependent has no primary key but uses EmpID (foreign key) as part of its composite
key.
Explanation:
SQL Example:
sql
Copy code
DependentName VARCHAR(50),
Relation VARCHAR(20),
EmpID INT,
);
Question: Write a SQL query to create a table Students with the following structure:
Answer:
sql
Copy code
DepartmentID INT,
);
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)
Question: Alter the Students table to add a column Email (varchar(100), unique).
Answer:
sql
Copy code
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
Question: Write a query to delete a record where Name = 'John' in the Students table.
Answer:
sql
Copy code
Question: Create a query using the IN operator to fetch all students with DepartmentID as 1,
2, or 3.
Answer:
sql
Copy code
Question: Write a query to fetch the total number of students in each department using
GROUP BY.
Answer:
sql
Copy code
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
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
Question: Write a SQL query to fetch records from the Students table and sort them by Age
in descending order.
Answer:
sql
Copy code
Question: Write a query to fetch students who are older than 20 but younger than 25 using the
BETWEEN operator.
Answer:
sql
Copy code
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
FROM Students
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
FROM Students
GROUP BY DepartmentID
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
FROM Students
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
FROM Students S1
FROM Students S2