0% found this document useful (0 votes)
6 views

DMBS 10 marks Int-1 QB answers

The document provides a comprehensive overview of SQL, covering its characteristics, advantages, data types, commands, joins, and the process of converting E-R diagrams to relational schemas. It includes detailed explanations and examples for each topic, such as SQL commands like DDL, DML, and various join types. Additionally, it discusses decomposition using functional dependencies to maintain data integrity and eliminate redundancy in database design.

Uploaded by

naxoc20149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DMBS 10 marks Int-1 QB answers

The document provides a comprehensive overview of SQL, covering its characteristics, advantages, data types, commands, joins, and the process of converting E-R diagrams to relational schemas. It includes detailed explanations and examples for each topic, such as SQL commands like DDL, DML, and various join types. Additionally, it discusses decomposition using functional dependencies to maintain data integrity and eliminate redundancy in database design.

Uploaded by

naxoc20149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DMBS Internal-1 QB Answers

S.No Question Page


No
1 Outline the characteristics and advantages of SQL. (10) 2

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)

5 Explain the steps involved in reducing an Entity-Relationship (E-R) 10


diagram to a relational schema.(10)

6 Describe the process of decomposition using functional dependencies 13


with examples.(10)

7 Explain the concept of subqueries in SQL. (10) 16

8 Compare and Contrast correlated and non-correlated subqueries with 18


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.

2. Standardized Language – SQL is an ANSI and ISO standard, ensuring consistency


across databases.

3. Supports Multiple Operations – Enables data querying, insertion, updating, and


deletion.

4. Relational Database Management – Works with relational databases like MySQL,


PostgreSQL, SQL Server, and Oracle.

5. Data Integrity & Security – Enforces constraints (e.g., PRIMARY KEY, FOREIGN
KEY) and supports access control mechanisms.

6. Scalability – Efficiently handles large datasets and complex queries.

7. Transaction Control – Supports ACID (Atomicity, Consistency, Isolation,


Durability) compliance for reliable transactions.

8. Joins and Relationships – Allows combining data from multiple tables using JOIN
operations.

9. Indexing for Performance – Uses indexes to speed up search and retrieval


operations.

10. Extensibility – Supports procedural extensions like PL/SQL and T-SQL for complex
logic implementation.

Advantages of SQL:

1. Easy to Learn and Use – Uses simple, English-like syntax.


2. Efficient Data Management – Quickly retrieves and manipulates large datasets.

3. High Security – Provides role-based access control, encryption, and authentication


mechanisms.

4. Cross-Platform Compatibility – Works with various database systems and


programming languages.

5. Reduced Development Time – Pre-built functions and optimized query execution


reduce coding efforts.

6. Data Integrity – Enforces rules (constraints, triggers) to maintain accurate and


consistent data.

2
7. Concurrency Control – Manages multiple users accessing the database
simultaneously.

8. Optimized Query Execution – Query optimizers enhance performance for complex


queries.

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 and Examples

SQL data types define the kind of data a column can store in a database table. They are
broadly categorized into:

1. Numeric Data Types (For storing numbers)

Data Type Description Example

INT Integer values (whole numbers) 100, -25

SMALLINT Smaller range of integers 32000

BIGINT Large integer values 9223372036854775807

DECIMAL(p,s) Fixed-point precision (p=total digits, DECIMAL(10,2) →


s=decimal places) 1234.56

NUMERIC(p,s) Similar to DECIMAL, ensures exact NUMERIC(5,3) → 12.345


values

FLOAT Approximate floating-point numbers 3.14159

REAL Less precision than FLOAT 2.718

3
2. Character/String Data Types (For storing text)

Data Type Description Example

CHAR(n) Fixed-length string (n characters) CHAR(5) → 'SQL '

VARCHAR(n) Variable-length string (up to n characters) VARCHAR(20) → 'Database'

TEXT Large text data (size varies by database) 'This is a long paragraph.'

3. Date and Time Data Types (For storing date/time values)

Data Type Description Example

DATE Stores only date (YYYY-MM-DD) '2025-03-24'

TIME Stores only time (HH:MI:SS) '14:30:00'

DATETIME Stores date and time '2025-03-24 14:30:00'

TIMESTAMP Auto-updated date and time '2025-03-24 14:30:00'

4. Boolean Data Type (For storing true/false values)

Data Type Description Example

BOOLEAN Stores TRUE or FALSE TRUE, FALSE

5. Binary Data Types (For storing binary data like images, files)

Data Type Description Example

BLOB Binary Large Object (stores images, videos) Binary data

VARBINARY(n) Variable-length binary data VARBINARY(16)

4
3) Illustrate the various types of SQL commands. Could you explain any three
with an example query.(10)
ANSWER:

Types of SQL Commands

SQL commands are categorized into five types:

1. Data Definition Language (DDL) – Defines and modifies database structure.

o Commands: CREATE, ALTER, DROP, TRUNCATE

2. Data Manipulation Language (DML) – Handles data manipulation within tables.


o Commands: INSERT, UPDATE, DELETE, SELECT

3. Data Control Language (DCL) – Manages user permissions and access control.

o Commands: GRANT, REVOKE

4. Transaction Control Language (TCL) – Manages transactions in SQL.

o Commands: COMMIT, ROLLBACK, SAVEPOINT

5. Data Query Language (DQL) – Retrieves data from the database.

o Command: SELECT

Explanation with Example Queries

1. Data Definition Language (DDL) – CREATE

Used to create database objects like tables.


Example:

CREATE TABLE Students (

ID INT PRIMARY KEY,

Name VARCHAR(50),
Age INT,

Course VARCHAR(30)

);

Explanation: Creates a table named Students with four columns.

5
2. Data Manipulation Language (DML) – INSERT

Used to add records into a table.

Example:

INSERT INTO Students (ID, Name, Age, Course)


VALUES (1, 'John Doe', 20, 'Computer Science');

Explanation: Inserts a student record into the Students table.

3. Data Query Language (DQL) – SELECT

Used to retrieve data from a table.

Example:

SELECT Name, Course FROM Students WHERE Age > 18;


Explanation: Fetches the Name and Course of students older than 18.
These SQL commands ensure efficient database creation, management, and retrieval of
information.

4. Compare inner joins, outer joins (left, right, full), and cross joins with
examples.(10)
ANSWER:

Comparison of SQL Joins


Joins are used in SQL to combine records from two or more tables based on a related column.

Join Type Description

Inner Join Returns only matching rows from both tables.

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

StudentID Name CourseID

1 John 101

2 Alice 102

3 Bob 103

Table: Courses

CourseID CourseName

101 SQL

102 Python

104 Java

1. INNER JOIN (Only Matching Records)

SELECT Students.Name, Courses.CourseName

FROM Students

INNER JOIN Courses ON Students.CourseID = Courses.CourseID;


Result:

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

LEFT JOIN Courses ON Students.CourseID = Courses.CourseID;

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)

SELECT Students.Name, Courses.CourseName

FROM Students

RIGHT JOIN Courses ON Students.CourseID = Courses.CourseID;


Result:

Name CourseName

John SQL

Alice Python

NULL Java

Explanation: All courses are included; Java has NULL since no student is enrolled in it.

4. FULL OUTER JOIN (All Records from Both Tables)

SELECT Students.Name, Courses.CourseName

FROM Students

FULL JOIN Courses ON Students.CourseID = Courses.CourseID;

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)

SELECT Students.Name, Courses.CourseName

FROM Students

CROSS JOIN Courses;


Result:

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

Join Type Matching Records Unmatched Records

INNER JOIN Only matching Not included

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

CROSS JOIN Every combination No matching needed

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:

Steps to Convert an E-R Diagram to a Relational Schema

Converting an Entity-Relationship (E-R) diagram into a Relational Schema involves the


following steps:

Step 1: Convert Entities into Tables

• Each strong entity in the E-R diagram is converted into a table.

• The primary key of the entity becomes the primary key of the table.

Example:
Entity: Student

StudentID (PK) Name Age

Relational Schema:

CREATE TABLE Student (


StudentID INT PRIMARY KEY,

Name VARCHAR(50),

Age INT

);

Step 2: Convert Weak Entities into Tables

• 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:

CREATE TABLE StudentAddress (

AddressID INT PRIMARY KEY,


StudentID INT,

10
Address VARCHAR(255),

FOREIGN KEY (StudentID) REFERENCES Student(StudentID)

);

Step 3: Convert Relationships into Tables

• 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.

Example (Many-to-Many Relationship):


A Student can enroll in multiple Courses, and a Course can have multiple Students.
Relational Schema:

CREATE TABLE StudentCourse (

StudentID INT,

CourseID INT,

PRIMARY KEY (StudentID, CourseID),

FOREIGN KEY (StudentID) REFERENCES Student(StudentID),

FOREIGN KEY (CourseID) REFERENCES Course(CourseID)


);

Step 4: Convert Attributes

• Simple attributes directly become table columns.

• Composite attributes are split into individual columns.

• Multivalued attributes require a separate table.

Example (Multivalued Attribute – Phone Numbers for Students):


| PhoneID (PK) | StudentID (FK) | PhoneNumber |

Relational Schema:

CREATE TABLE StudentPhone (


PhoneID INT PRIMARY KEY,

11
StudentID INT,

PhoneNumber VARCHAR(15),

FOREIGN KEY (StudentID) REFERENCES Student(StudentID)

);

Step 5: Convert Generalization and Specialization

• 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.

Example (Generalization: Employee → Teacher & Staff):

CREATE TABLE Employee (

EmployeeID INT PRIMARY KEY,

Name VARCHAR(50),

Type VARCHAR(20) -- 'Teacher' or 'Staff'

);

CREATE TABLE Teacher (

EmployeeID INT PRIMARY KEY,

Subject VARCHAR(50),

FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)

);

CREATE TABLE Staff (


EmployeeID INT PRIMARY KEY,

Department VARCHAR(50),

FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)

);

12
Step 6: Normalize the Tables

• Ensure the schema follows normalization rules (1NF, 2NF, 3NF) to reduce
redundancy and improve efficiency.

Summary of the Steps:

1. Convert entities into tables.

2. Convert weak entities with foreign keys.


3. Convert relationships based on their type.

4. Convert attributes (simple, composite, multivalued).

5. Convert generalization/specialization into appropriate table structures.

6. Normalize the schema to remove redundancy.

This ensures the E-R diagram is successfully transformed into a well-structured relational
database.

6) Describe the process of decomposition using functional dependencies with


examples.(10)
ANSWER:
Decomposition Using Functional Dependencies

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?

• To remove data redundancy.

• To avoid anomalies (insertion, update, and deletion).

• To ensure the relation is in higher normal forms (2NF, 3NF, BCNF).

• To maintain data consistency.

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.

Process of Decomposition Using Functional Dependencies

Step 1: Identify Functional Dependencies (FDs)

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

2. Course → Instructor, Dept

Step 2: Check for Normalization Violations

• If the relation is not in BCNF (Boyce-Codd Normal Form) or 3NF (Third Normal
Form), decomposition is needed.

• A relation is not in BCNF if it has functional dependencies where a non-key


attribute determines another attribute.

In our example, Course → Instructor, Dept violates BCNF, as Course is not a candidate key.

Step 3: Decompose the Relation

To eliminate redundancy and anomalies, decompose R(StudentID, Name, Course,


Instructor, Dept) into two smaller relations:

1. R1(StudentID, Name, Course) → (Ensures Student details)

2. R2(Course, Instructor, Dept) → (Ensures Course details)

Step 4: Verify Lossless Decomposition

A decomposition is lossless if:

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

All original dependencies should be maintained.

• StudentID → Name, Course is preserved in R1.


• Course → Instructor, Dept is preserved in R2.

• Since all dependencies exist in the decomposed tables, the decomposition is


dependency-preserving.

Final Decomposed Schema

R1(StudentID, Name, Course)

CREATE TABLE R1 (

StudentID INT PRIMARY KEY,

Name VARCHAR(50),

Course VARCHAR(50)
);

R2(Course, Instructor, Dept)

CREATE TABLE R2 (

Course VARCHAR(50) PRIMARY KEY,

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).

• Used with operators like =, >, <, >=, <=.

Example: Find employees who earn more than the average salary.

SELECT Name, Salary

FROM Employees

WHERE Salary > (SELECT AVG(Salary) FROM Employees);

Explanation: The subquery calculates the average salary, and the main query returns
employees earning above this value.

2. Multi-Row Subquery

• Returns multiple values (one column, multiple rows).

• Used with operators like IN, ANY, ALL.

Example: Find employees working in the same department as 'John'.

SELECT Name, Department

FROM Employees

WHERE Department IN (SELECT Department FROM Employees WHERE Name = 'John');

Explanation: The subquery retrieves John’s department, and the main query lists all
employees in that department.

16
3. Correlated Subquery

• Executes for each row in the main query.

• Uses the outer query’s column inside the subquery.

Example: Find employees whose salary is higher than the department’s average salary.

SELECT Name, Salary, Department

FROM Employees E1

WHERE Salary > (SELECT AVG(Salary) FROM Employees E2 WHERE E1.Department =


E2.Department);

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

• A subquery inside another subquery.

Example: Find employees in departments where the manager earns above $50,000.
SELECT Name, Department

FROM Employees

WHERE Department IN (

SELECT Department FROM Employees

WHERE EmployeeID IN (

SELECT ManagerID FROM Employees WHERE Salary > 50000

)
);

Explanation:

1. The inner subquery finds ManagerID of employees earning more than $50,000.

2. The middle subquery finds departments where these managers work.

3. The outer query fetches all employees in those departments.

17
Use Cases of Subqueries

Fetching specific data dynamically.


Writing complex conditions efficiently.
Improving query readability without multiple joins.

Subqueries help simplify SQL queries and make them more powerful!

8) Compare and Contrast correlated and non-correlated subqueries with


examples.(10)
ANSWER:

Comparison of Correlated and Non-Correlated Subqueries

Subqueries in SQL are classified into Correlated and Non-Correlated (Independent)


subqueries based on their execution behavior.

1. Non-Correlated Subqueries

Definition

• A non-correlated subquery is independent of the outer query.


• It executes only once, and the result is passed to the main query.

• It does not reference any column from the outer query.

Example: Find employees who earn more than the average salary.
SELECT Name, Salary

FROM Employees

WHERE Salary > (SELECT AVG(Salary) FROM Employees);

Explanation:

• The subquery calculates AVG(Salary).

• 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.

• It references a column from the outer query inside the subquery.

Example: Find employees whose salary is higher than the average salary in their
department.

SELECT Name, Salary, Department

FROM Employees E1

WHERE Salary > (SELECT AVG(Salary) FROM Employees E2 WHERE E1.Department =


E2.Department);

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

Feature Non-Correlated Subquery Correlated Subquery

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.

Performance Faster, as it runs only once. Slower, as it executes multiple times.

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.

• Use Correlated Subqueries when row-wise comparison is needed, though it may


affect performance on large datasets.

19

You might also like