DMBS 10 marks Int-1 QB answers
DMBS 10 marks Int-1 QB answers
2 Summarize the SQL data types available. Provide examples for each data 3
type.(10)
3 Illustrate the various types of SQL commands. Could you explain any 5
three with an example query.(10)
4 Compare inner joins, outer joins (left, right, full), and cross joins with 6
examples.(10)
1
1) Outline the characteristics and advantages of SQL. (10 marks)
ANSWER:
Characteristics of SQL:
1. Declarative Language – Users specify what they want (data retrieval, modification)
rather than how to achieve it.
5. Data Integrity & Security – Enforces constraints (e.g., PRIMARY KEY, FOREIGN
KEY) and supports access control mechanisms.
8. Joins and Relationships – Allows combining data from multiple tables using JOIN
operations.
10. Extensibility – Supports procedural extensions like PL/SQL and T-SQL for complex
logic implementation.
Advantages of SQL:
2
7. Concurrency Control – Manages multiple users accessing the database
simultaneously.
9. Integration with Other Technologies – Easily connects with applications using APIs
(JDBC, ODBC).
10. Supports Big Data – Works with modern technologies like Hadoop, NoSQL, and
cloud databases.
2) Summarize the SQL data types available. Provide examples for each data
type.(10 marks)
ANSWER:
SQL data types define the kind of data a column can store in a database table. They are
broadly categorized into:
3
2. Character/String Data Types (For storing text)
TEXT Large text data (size varies by database) 'This is a long paragraph.'
5. Binary Data Types (For storing binary data like images, files)
4
3) Illustrate the various types of SQL commands. Could you explain any three
with an example query.(10)
ANSWER:
3. Data Control Language (DCL) – Manages user permissions and access control.
o Command: SELECT
Name VARCHAR(50),
Age INT,
Course VARCHAR(30)
);
5
2. Data Manipulation Language (DML) – INSERT
Example:
Example:
4. Compare inner joins, outer joins (left, right, full), and cross joins with
examples.(10)
ANSWER:
Left Join (Left Returns all rows from the left table and matching rows from the right
Outer Join) table; unmatched rows from the right table are NULL.
Right Join (Right Returns all rows from the right table and matching rows from the left
Outer Join) table; unmatched rows from the left table are NULL.
Full Join (Full Returns all rows from both tables, with NULLs for non-matching
Outer Join) rows.
Cross Join Returns the Cartesian product of both tables (each row in the first
table combines with every row in the second table).
6
Example Tables
Table: Students
1 John 101
2 Alice 102
3 Bob 103
Table: Courses
CourseID CourseName
101 SQL
102 Python
104 Java
FROM Students
Name CourseName
John SQL
Alice Python
Explanation: Only students with matching CourseID in both tables are displayed (Bob is
excluded as CourseID 103 is not in Courses).
2. LEFT JOIN (All Records from Left Table + Matching from Right)
SELECT Students.Name, Courses.CourseName
FROM Students
Result:
Name CourseName
7
John SQL
Alice Python
Bob NULL
Explanation: All students are included; Bob has NULL because CourseID 103 has no match.
3. RIGHT JOIN (All Records from Right Table + Matching from Left)
FROM Students
Name CourseName
John SQL
Alice Python
NULL Java
Explanation: All courses are included; Java has NULL since no student is enrolled in it.
FROM Students
Result:
Name CourseName
John SQL
Alice Python
Bob NULL
NULL Java
Explanation: All students and courses are displayed, even if there's no match.
8
5. CROSS JOIN (Cartesian Product)
FROM Students
Name CourseName
John SQL
John Python
John Java
Alice SQL
Alice Python
Alice Java
Bob SQL
Bob Python
Bob Java
Explanation: Each student is paired with every course, generating all possible combinations.
Summary Table
LEFT JOIN Matching + all left table NULL for non-matching right
RIGHT JOIN Matching + all right table NULL for non-matching left
FULL JOIN Matching + all from both tables NULL for non-matching
Each join serves a different purpose in SQL queries, ensuring flexible data retrieval.
9
5) Explain the steps involved in reducing an Entity-Relationship (E-R) diagram
to a relational schema.(10)
ANSWER:
• The primary key of the entity becomes the primary key of the table.
Example:
Entity: Student
Relational Schema:
Name VARCHAR(50),
Age INT
);
• Weak entities depend on strong entities and require a foreign key for identification.
• The primary key of the weak entity table includes the foreign key of the related
strong entity.
Example:
Weak Entity: StudentAddress (Dependent on Student)
| AddressID (PK) | StudentID (FK) | Address |
Relational Schema:
10
Address VARCHAR(255),
);
• One-to-Many (1:M): The foreign key of the "many" side is placed in the
corresponding table.
• Many-to-Many (M:N): A separate junction table is created with foreign keys from
both entities.
• One-to-One (1:1): A foreign key is placed in either of the related tables.
StudentID INT,
CourseID INT,
Relational Schema:
11
StudentID INT,
PhoneNumber VARCHAR(15),
);
• Generalization: The common attributes are placed in a single parent table, and
specialized entities are stored in separate child tables.
• Specialization: Each specialized entity has its own table, linked with the parent table
via a foreign key or inheritance strategy.
Name VARCHAR(50),
);
Subject VARCHAR(50),
);
Department VARCHAR(50),
);
12
Step 6: Normalize the Tables
• Ensure the schema follows normalization rules (1NF, 2NF, 3NF) to reduce
redundancy and improve efficiency.
This ensures the E-R diagram is successfully transformed into a well-structured relational
database.
What is Decomposition?
Decomposition is the process of breaking a large relation (table) into two or more smaller,
well-structured relations to eliminate redundancy and maintain data integrity while
preserving dependencies.
Why is Decomposition Needed?
Types of Decomposition
1. Lossless (Non-Loss) Decomposition
Ensures that no data is lost when relations are joined back using natural join.
13
2. Dependency-Preserving Decomposition
Ensures that all functional dependencies from the original table are maintained in the
decomposed tables.
A functional dependency (FD) exists when one attribute uniquely determines another.
Example: Consider the relation R(StudentID, Name, Course, Instructor, Dept) with
dependencies:
1. StudentID → Name, Course
• If the relation is not in BCNF (Boyce-Codd Normal Form) or 3NF (Third Normal
Form), decomposition is needed.
In our example, Course → Instructor, Dept violates BCNF, as Course is not a candidate key.
R1∩R2 (common attributes) must be a key for at least one of the tablesR1 \cap R2 \text{
(common attributes) must be a key for at least one of the
tables}R1∩R2 (common attributes) must be a key for at least one of the tables
• Here, Course is a key in R2, ensuring lossless join.
14
Step 5: Verify Dependency Preservation
CREATE TABLE R1 (
Name VARCHAR(50),
Course VARCHAR(50)
);
CREATE TABLE R2 (
Instructor VARCHAR(50),
Dept VARCHAR(50)
);
This ensures no redundancy, maintains data consistency, and eliminates anomalies while
preserving dependencies.
15
7) Explain the concept of subqueries in SQL. (10)
ANSWER:
Subqueries in SQL
Definition
A subquery (also known as a nested query or inner query) is a SQL query that is embedded
within another SQL query (main query). The subquery executes first, and its result is used
by the main query.
Types of Subqueries
1. Single-Row Subquery
• Returns one value (one row, one column).
Example: Find employees who earn more than the average salary.
FROM Employees
Explanation: The subquery calculates the average salary, and the main query returns
employees earning above this value.
2. Multi-Row Subquery
FROM Employees
Explanation: The subquery retrieves John’s department, and the main query lists all
employees in that department.
16
3. Correlated Subquery
Example: Find employees whose salary is higher than the department’s average salary.
FROM Employees E1
Explanation: The subquery calculates each department’s average salary, and the main
query filters employees earning more than their department's average.
4. Nested Subquery
Example: Find employees in departments where the manager earns above $50,000.
SELECT Name, Department
FROM Employees
WHERE Department IN (
WHERE EmployeeID IN (
)
);
Explanation:
1. The inner subquery finds ManagerID of employees earning more than $50,000.
17
Use Cases of Subqueries
Subqueries help simplify SQL queries and make them more powerful!
1. Non-Correlated Subqueries
Definition
Example: Find employees who earn more than the average salary.
SELECT Name, Salary
FROM Employees
Explanation:
• The main query filters employees with salaries above this value.
• The subquery runs once and does not depend on outer query rows.
2. Correlated Subqueries
Definition
• A correlated subquery depends on the outer query for execution.
18
• It executes once for each row in the outer query.
Example: Find employees whose salary is higher than the average salary in their
department.
FROM Employees E1
Explanation:
• The subquery calculates the average salary for each department (using
E1.Department = E2.Department).
• The main query selects employees earning more than their department’s average
salary.
• The subquery runs for each row in the outer query, making it slower for large
datasets.
Comparison Table
Execution Runs once before the outer Runs once per row of the outer query.
query.
Dependency Independent of the outer query. References a column from the outer
query.
Use Case When the subquery result is When the subquery needs to be executed
constant for all rows. per row in the main query.
Conclusion
• Use Non-Correlated Subqueries for better performance when the subquery’s result
does not depend on outer query rows.
19