2. How to create an ER diagram of organisation?
Q
Creating an ER (Entity-Relationship) diagram for an organization
involves identifying the entities, attributes, and relationships that
represent the structure and operations of that organization.
Steps to Create an ER Diagram for an Organization
1. Identify Key Entities
Entities are the main components in your system. For an organization,
common entities include:
1.Employee
2.Department
3.Project
4.Client
5.Manager
6.Attendance
7.Salary
2. Determine Attributes for Each Entity
Employee
1.EmpID (Primary Key)
2.Name
3.Address
4.Phone
5.Email
6.DeptID (Foreign Key)
7.SalaryID (Foreign Key)
Department
1.DeptID (Primary Key)
2.DeptName
3.Location
Project
1.ProjectID (Primary Key)
2.ProjectName
3.StartDate
4.DeptID (Foreign Key)
Client
1.ClientID (Primary Key)
2.Name
3.Contact
Salary
1.SalaryID (Primary Key)
2.Amount
3.Bonus
3. Define Relationships Between Entities
1.An employee belongs to one department, but a department can
have many employees →One-to-Many
2.An employee can work on multiple projects and a project can
have multiple employees →Many-to-Many
3.A manager is also an employee (recursive relationship) →
One-to-One or One-to-Many
4.A project is assigned to one department
5.A client can request multiple projects
4. Draw the ER Diagram
Use the following notation:
1.Rectangle = Entity
2.Oval = Attribute
3.Diamond = Relationship
4.Line = Link between them
5.PK = Primary Key
6.FK = Foreign Key
[Department]---------------------------<works
in>-----------------[Employee]
| DeptID (PK) | EmpID (PK)
| DeptName | Name
| Location | DeptID (FK)
|
________________________________|__________________
| |
| |
Assigned>
< <Gets
Paid>
| |
[Project] [Salary]
| ProjectID (PK) | SalaryID
(PK)
| ProjectName | Amount
| DeptID (FK) | Bonus
[Client] ---------<Requests>---------- [Project]
ClientID (PK) ProjectID (PK)
3. Explain all the commands of DML with suitable example
Q
DML commands are used to manipulate data stored in the database
(not the structure). These commands are:
1. INSERT:-Used to add new records into a table.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:-
INSERT INTO Students (StudentID, Name, Age)
VALUES (1, 'Alex', 20);
2. SELECT:-Used to retrieve data from one or more tables.
Syntax:
SELECT column1, column2 FROM table_name;
Example:
SELECT Name, Age FROM Students;
You can use conditions:
SELECT * FROM Students WHERE Age > 18;
UPDATE:-Used to modify existing records in a table.
3.
yntax:
S
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:-
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
Always use a WHEREclause to avoid updating all rows.
4. DELETE:-Used to delete records from a table.
Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Students
WHERE StudentID = 1;
WHERE
Without , all rows will be deleted:
DELETE FROM Students; -- Deletes all rows!
4. what is the role of joins in database define all its types
Q
Ans:-Joins in SQL are used to combine rows from two or more tables
based on a related column between them. They allow you to fetch
data that is spread across multiple tables using relationships like
foreign keys.
Why Use Joins?
1.To retrieve related data from multiple tables
2.To enforce data normalization
3.To avoid data duplication
4.To perform complex queries
Types of Joins in SQL
1. INNER JOIN:- Returns only the matching rows from both tables
based on the specified condition.
yntax:
S
SELECT *
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT Students.Name, Departments.DeptName
FROM Students
INNER JOIN Departments
ON Students.DeptID = Departments.DeptID;
. LEFT JOIN (or LEFT OUTER JOIN):-Returns all records from the
2
left table and the matched records from the right table. If there's no
NULLis returned for right table columns.
🔸
match,
Syntax:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT Students.Name, Departments.DeptName
FROM Students
LEFT JOIN Departments
ON Students.DeptID = Departments.DeptID;
. RIGHT JOIN (or RIGHT OUTER JOIN):- Returns all records from
3
the right table and the matched records from the left table.
Syntax:
SELECT *
FROM table1
RIGHT JOIN table2
N table1.common_column = table2.common_column;
O
Example:
SELECT Students.Name, Departments.DeptName
FROM Students
RIGHT JOIN Departments
ON Students.DeptID = Departments.DeptID;
4. FULL JOIN (or FULL OUTER JOIN)
Returns all records when there is a match in either left or right table.
Rows without matches get NULLin place of missing values.
Syntax:
SELECT * FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT Students.Name, Departments.DeptName
FROM Students
FULL OUTER JOIN Departments
ON Students.DeptID = Departments.DeptID;
. CROSS JOIN:- Returns the Cartesian product of both tables (every
5
row of table1 combined with every row of table2). Used rarely and
🔸
carefully.
Syntax:
SELECT *
FROM table1
CROSS JOIN table2;
Example:
SELECT Students.Name, Courses.CourseName
FROM Students
CROSS JOIN Courses;
6. SELF JOIN:-A self join is when a table is joined with itself.
Syntax:
SELECT A.Name, B.Name
FROM Employees A, Employees B
WHERE A.ManagerID = B.EmployeeID;
Use Case:Finding employees who report to the same manager.
Q5. Write a short note on:-
1. Spurious Tuple
Ans:-A spurious tuple is an unwanted or incorrect row that appears in
the result of a JOIN operation due to improper join conditions.
It usually occurs when the database is not properly normalized.
Example:
If two tables are joined on non-key attributes, it may produce
duplicate or meaningless records, leading to incorrect query results.
. Candidate Key
2
Ans:-A candidate key is a minimal set of attributes that can uniquely
identify a tuple (row) in a relation.
There can be multiple candidate keys in a table, but only one is
chosen as the primary key.
Example:
Studentstable, both
In a RollNumberandEmailcould be
candidate keys.
. Referential Integrity
3
Ans:-Referential Integrity ensures that a foreign key in one table must
match a primary key in another table (or be NULL).
It helps maintain consistency and prevents orphan records.
Example:
Employee.DeptIDreferences
If Department.DeptID , we can't
add an employee with a DeptIDthat doesn't exist in the Department
table.
. Relational Algebra – Set Operations
4
Ans:-Relational Algebra provides theoretical operations for querying
databases. Important set operations include:
1.UNION→ Combines rows from two relations, removing
duplicates
2.INTERSECTION→ Returns common rows from both relations
3.DIFFERENCE (MINUS)→ Returns rows present in the first
relation but not in the second
4.CARTESIAN PRODUCT (×)→ Pairs each row from the first
table with every row from the second table
Note:All set operations require the same number of columns with
compatible data types.
5.Difference between SELECT vs PROJECTION
SELECT PROJECTION
QL command to
S elational algebra operation to
R
retrieve data from a select specific columns from a
efinition database.
D relation.
sed in SQL (Structured U
U sed in relational algebra
Context Query Language). (theoretical framework).
ELECT column1,
S
column2 FROM
Syntax table_name; π(column1, column2)(relation)
an include filtering,
C ocuses solely on selecting
F
Complexi sorting, grouping, and columns; does not include
ty joining. filtering or sorting.
etrieves data based on
R
Function specified criteria and can Simply extracts specified
lity
a manipulate the result set. columns from a dataset.
ELECT name, age
S
FROM users WHERE
xample age > 18;
E π(name, age)(users)
an return a result set
C
with various operations eturns a new relation with only
R
Output applied. the specified columns.