PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
1
Relational Model
&
Relational Algebra
Module 3
Relational Model
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
2
Relational Model
In relational model
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
3
Terminology
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
4
𝑫𝒂𝒕𝒂𝒃𝒂𝒔𝒆 𝑺𝒄𝒉𝒆𝒎𝒂
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
5
𝑫𝒂𝒕𝒂𝒃𝒂𝒔𝒆 𝑺𝒄𝒉𝒆𝒎𝒂
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
6
The 𝒔𝒄𝒉𝒆𝒎𝒂 for 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 relation is
𝑡𝑒𝑎𝑐ℎ𝑒𝑟(𝐼𝐷, 𝑛𝑎𝑚𝑒, 𝑑𝑒𝑝𝑡_𝑛𝑎𝑚𝑒, 𝑐𝑖𝑡𝑦, 𝑠𝑎𝑙𝑎𝑟𝑦)
Relational Integrity Constraints
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
7
Relational Integrity Constraints
A. Domain Constraint
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
8
EID | Name | Phone |
01 | Bikash Dutta | 123456789 234456678 |
Relational Integrity Constraints
B. Key Constraint
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
9
EID | Name | Phone |
01 | Bikash | 6000000009 |
02 | Paul | 9000090009 |
01 | Tuhin | 9234567892 |
Relational Integrity Constraints
C. Entity Integrity Constraint
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
10
EID | Name | Phone |
01 | Bikash | 9000900099 |
02 | Paul | 600000009 |
NULL | Sony | 9234567892 |
Relational Integrity Constraints
D. Referential Integrity Constraint
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
11
EID | Name | DNO |
01 | Divine | 12 |
02 | Dino | 22 |
04 | Vivian | 14 |
DNO | Place |
12 | Jaipur |
13 | Mumbai |
14 | Delhi |
Relational Integrity Constraints
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
12
Relational Integrity Constraints
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
13
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
14
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
15
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
16
ID | name | dept_name | city | salary |
102 | Rachel Greene | Fashion Technology | New York | 65000 |
265 | Joey Tribbiani | Food Technology | New York | 50000 |
142 | Sheldon Cooper | Physics | Pasadena | 74000 |
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
17
𝑡𝑒𝑎𝑐ℎ𝑒𝑟 relation.
key.
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
18
𝑡𝑒𝑎𝑐ℎ𝑒𝑟 = {𝐼𝐷, 𝑛𝑎𝑚𝑒, 𝑑𝑒𝑝𝑡_𝑛𝑎𝑚𝑒, 𝑐𝑖𝑡𝑦, 𝑠𝑎𝑙𝑎𝑟𝑦}
ID | name | dept_name | city | salary |
102 | Rachel Greene | Fashion Technology | New York | 65000 |
265 | Joey Tribbiani | Food Technology | New York | 50000 |
142 | Sheldon Cooper | Physics | Pasadena | 74000 |
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
19
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
20
Keys
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
21
ID | name | dept_id | city | salary |
102 | Rachel Greene | D001 | New York | 65000 |
265 | Joey Tribbiani | D002 | New York | 50000 |
142 | Sheldon Cooper | D003 | Pasadena | 74000 |
356 | Penny Hofstadter | D004 | Nebraska | 40000 |
555 | Robert Wheeler | D005 | Detroit | 152000 |
641 | Monica Geller | D004 | New York | 85000 |
dept_id | dept_name | build_no |
D001 | Fashion Technology | A1 |
D002 | Food Technology | A3 |
D003 | Physics | B2 |
D004 | Hotel Management | E1 |
D005 | Business Management | D5 |
D006 | History | B2 |
Foreign key
𝑡𝑒𝑎𝑐ℎ𝑒𝑟
Primary key of
𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡
Referencing Relation 𝑟1
Referenced Relation 𝑟2
Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
22
Relational Algebra
Select
Project
Union
Set Difference
Cartesian product
Rename
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
23
Unary relational Operations
SET theory Opertaions
Binary Operations
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
24
Employee_ID | Name | Salary | Department |
101 | Ram | 60000 | HR |
102 | Sita | 45000 | IT |
103 | Shiva | 55000 | Marketing |
104 | Vishnu | 75000 | IT |
105 | Deepthi | 65000 | HR |
106 | Varun | 55000 | Marketing |
σ(condition)(Relation)
σ(Salary >= 55000)(Employee)
Employee_ID | Name | Salary | Department |
101 | Ram | 60000 | HR |
103 | Shiva | 55000 | Marketing |
104 | Vishnu | 75000 | IT |
105 | Deepthi | 65000 | HR |
106 | Varun | 55000 | Marketing |
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
25
II. PROJECT (π) Opertaion
Employee_ID | Name | Salary | Department |
101 | Ram | 60000 | HR |
102 | Sita | 45000 | IT |
103 | Shiva | 55000 | Marketing |
104 | Vishnu | 75000 | IT |
105 | Deepthi | 65000 | HR |
106 | Varun | 55000 | Marketing |
π(column1, column2, ...)(Relation)
π(Name, Salary)(Employee)
Name | Salary |
Ram | 60000 |
Sita | 45000 |
Shiva | 55000 |
Vishnu | 75000 |
Deepthi | 65000 |
Varun | 55000 |
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
26
III. RENAME (ρ) Opertaion
Employee_ID | Name | Salary | Department |
101 | Ram | 60000 | HR |
102 | Sita | 45000 | IT |
103 | Shiva | 55000 | Marketing |
104 | Vishnu | 75000 | IT |
105 | Deepthi | 65000 | HR |
106 | Varun | 55000 | Marketing |
ρ(Workers)(Emp_ID, Emp_Name, Emp_Salary, Emp_Dept)(Employee)
ρ(New_relation)(Attributes_of_relation)
(old_relation)
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
27
Example:
Employee_ID | Name | Salary | Department |
101 | Ram | 60000 | HR |
102 | Sita | 45000 | IT |
103 | Shiva | 55000 | Marketing |
104 | Vishnu | 75000 | IT |
105 | Deepthi | 65000 | HR |
106 | Varun | 55000 | Marketing |
π(Name)(σ(Salary > 55000)(Employee))
Name |
Ram |
Vishnu |
Deepthi |
Unary relational Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
28
Example:
EID | Name | Dept | Salary |
1 | Alice | HR | 50000 |
2 | Bob | IT | 60000 |
3 | Carol | HR | 55000 |
4 | David | Finance | 52000 |
5 | Emma | Marketing | 47000 |
6 | Frank | IT | 61000 |
7 | Grace | Finance | 49000 |
8 | Henry | Marketing | 45000 |
9 | Ivy | HR | 53000 |
10 | Jack | IT | 65000 |
Unary relational Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
29
Q. No. | Operation | Question | Relational Algebra Expression |
1 | Selection (σ) | Find all employees who work in the 'HR' department. | σ(Dept='HR’)(Employee) |
2 | Projection (π) | List the names and departments of all employees. | Π(Name, Dept)(Employee) |
3 | Selection (σ) | Find employees whose salary is greater than 55000. | σ Salary > 55000(Employee) |
4 | Projection (π) | List all unique departments from the Employee table. | πDept(Employee) |
5 | Rename (ρ) | Rename the Employee relation as Staff. | ρStaff(Employee) |
6 | Rename (ρ) | Rename attribute ‘Dept’ to ‘Department’. | ρ(EID, Name, Department, Salary)(Employee) |
7 | Combined σ + π | Find names of employees in 'HR' department only. | πName(σDept='HR'(Employee)) |
8 | Rename + Projection | Rename Employee to Emp, and list their IDs and Salaries. | πEID, Salary(ρEmp(Employee)) |
Solution:
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
30
B. SET Theory Operations
I. Union (∪) Operation
The UNION operator is a set operator that combines the result sets of two or more SELECT statements into a single result set.
Rules for applying UNION
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
31
Emp_ID | Name | Gender | CTC |
1001 | Ajay | M | 15 |
1002 | Babloo | M | 23 |
1003 | Fredy | F | 15 |
1004 | Dheeraj | M | 12 |
1005 | Evina | F | 16 |
Emp_ID | Name | Gender | CTC |
1006 | Garima | M | 10 |
1007 | Chhavi | F | 15 |
1008 | Hans | M | 8 |
1009 | Ivanka | F | 7 |
1010 | Jai | M | 16 |
Freelancer
Permanent
SELECT Name, CTC
FROM freelancer
UNION
SELECT Name, CTC
FROM permanent;
Name | CTC |
Ajay | 15 |
Babloo | 23 |
Dheeraj | 12 |
Evina | 16 |
Garima | 10 |
Hans | 8 |
Ivanka | 7 |
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
32
UNION ALL
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
33
II. Intersection (∩) Operation
The intersection operation returns only those rows that will be common to both of the SELECT statements.
Key Characteristics of SQL INTERSECT:
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
34
Emp_ID | Name | Gender | CTC |
1001 | Ajay | M | 15 |
1002 | Babloo | M | 23 |
1003 | Fredy | F | 15 |
1004 | Dheeraj | M | 12 |
1005 | Evina | F | 16 |
Emp_ID | Name | Gender | CTC |
1006 | Garima | M | 10 |
1007 | Chhavi | F | 15 |
1008 | Hans | M | 8 |
1009 | Ivanka | F | 7 |
1010 | Jai | M | 16 |
Freelancer
Permanent
SELECT CTC
FROM Permanent
INTERSECT
SELECT CTC
FROM Freelancer;
CTC |
15 |
16 |
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
35
III. Set Difference (-) Operation
A binary operation that gives tuples in one relation but not present in another relation.
In simple terms, it returns all the rows from the first relation, which is not present in the second one.
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
36
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
37
IV. Cartesian Product (X) Operation
Color X Size
SET Theory Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
38
Example:
Stu_First | Stu_Last |
Aisha | Arora |
Bikash | Dutta |
Makku | Singh |
Raju | Chopra |
Emp_first | Emp_Last |
Raj | Kumar |
Honey | Chand |
Makku | Singh |
Karan | Rao |
Solve for:
Fundamental Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
39
Unary relational Operations
SET theory Opertaions
Binary Operations
Binary Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
40
C. Binary Operations
I. JOIN (⋈) Operation
one to many or many to many relationships between them.
Types of Join
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
41
I. JOIN (⋈) Operation
It selects the values present in both the Table performing Inner join.
Inner Join is further classified into
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
42
I. JOIN (⋈) Operation
A ⋈θ B where θ is the condition for join.
• Syntax
SELECT column_name (s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
43
Student Table
StudentCourse Table
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE
FROM Student�INNER JOIN StudentCourse�ON Student.ROLL_NO = StudentCourse.ROLL_NO;
Result Table
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
44
R | S |
10 | 5 |
7 | 20 |
T | U |
10 | 12 |
17 | 6 |
A ⨝ (S<T) B
R | S | T | U |
10 | 5 | 10 | 12 |
• Syntax
SELECT column_name (s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
table1
table2
• SQL Query
SELECT table1.R,table1.S,table2.T,table2.U
FROM table1
INNER JOIN table2
ON table1. S < table2.T;
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
45
I. JOIN (⋈) Operation
ii. Equi Join (⋈θ)
R | S |
10 | 5 |
7 | 20 |
5 | 10 |
T | U |
10 | 12 |
17 | 6 |
11 | 9 |
A ⨝ (S=T) B
R | S | T | U |
5 | 10 | 10 | 12 |
• SQL Query
SELECT table1.R,table1.S,table2.T,table2.U
FROM table1
INNER JOIN table2
ON table1. S = table2.T;
SQL INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
46
I. JOIN (⋈) Operation
iii. Natural Join (⋈)
R | S |
10 | 5 |
7 | 20 |
5 | 10 |
T | U |
10 | 12 |
17 | 6 |
10 | 9 |
A ⨝ B
R | S | T | U |
10 | 5 | 10 | 12 |
7 | 20 | 17 | 6 |
5 | 10 | 10 | 9 |
INNER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
47
SQL OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
48
I. JOIN (⋈) Operation
b) Outer Join (⋈) operation
Outer join is a type of join that retrieves matching as well as non-matching records from related tables.
Types of outer join
SQL OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
49
I. JOIN (⋈) Operation
b) Outer Join (⋈) operation
It retrieves all records from the left table and retrieves matching records from the right table.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....�FROM table1 �LEFT JOIN table2�ON table1.matching_column = table2.matching_column;
SQL LEFT OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
50
Student Table
StudentCourse Table
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Result Table
SQL LEFT OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
51
Number | Square |
2 | 4 |
3 | 9 |
4 | 16 |
Number | Cube |
2 | 8 |
3 | 27 |
5 | 125 |
A ⟕ B
Number | Square | Cube |
2 | 4 | 8 |
3 | 9 | 27 |
4 | 16 | NULL |
• SQL Query
SELECT table1.Number,table1.Square,table2.Cube
FROM table1
INNER JOIN table2
ON table1. Number = table2.Number;
table1
table2
SQL RIGHT OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
52
I. JOIN (⋈) Operation
b) Outer Join (⋈) operation
ii. Right Outer Join
retrieves all records from the right table and retrieves matching records from the left table. And for the record which doesn't lies in Left table will be marked as NULL in result Set.
SELECT table1.column1,table1.column2,table2.column1,....�FROM table1 �RIGHT JOIN table2�ON table1.matching_column = table2.matching_column;
SQL RIGHT OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
53
Student Table
StudentCourse Table
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Result Table
SQL RIGHT OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
54
Number | Square |
2 | 4 |
3 | 9 |
4 | 16 |
Number | Cube |
2 | 8 |
3 | 27 |
5 | 125 |
A ⟕ B
Number | Square | Cube |
2 | 4 | 8 |
3 | 9 | 27 |
5 | NULL | 125 |
• SQL Query
SELECT table1.Number,table1.Square,table2.Cube
FROM table1
RIGHT JOIN table2
ON table1. Number = table2.Number;
table1
table2
SQL FULL OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
55
I. JOIN (⋈) Operation
b) Outer Join (⋈) operation
ii. Full Outer Join
SELECT table1.column1,table1.column2,table2.column1,....�FROM table1 �FULL JOIN table2�ON table1.matching_column = table2.matching_column;
SQL FULL OUTER JOIN
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
56
Student Table
StudentCourse Table
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Result Table
NAME | COURSE_ID |
HARSH | 1 |
PRATIK | 2 |
RIYANKA | 2 |
DEEP | 3 |
SAPTARHI | 1 |
DHANRAJ | NULL |
ROHIT | NULL |
NIRAJ | NULL |
NULL | 4 |
NULL | 5 |
NULL | 4 |
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
57
Number | Square |
2 | 4 |
3 | 9 |
4 | 16 |
Number | Cube |
2 | 8 |
3 | 27 |
5 | 125 |
A ⟗ B
Number | Square | Cube |
2 | 4 | 8 |
3 | 9 | 27 |
4 | 16 | NULL |
5 | NULL | 125 |
• SQL Query
SELECT table1.Number,table1.Square,table2.Cube
FROM table1
FULL JOIN table2
ON table1. Number = table2.Number;
SQL FULL OUTER JOIN
SQL Division Operations
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
58
II. Division Operation
The DIVISION operation is a binary relational operation that divides one set of rows into another set of rows based on specified conditions.
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
59
Sample Relations:
∙ Employee(EmpID, Name, DeptID, Salary, HireDate)
∙ Department(DeptID, DeptName, Location)
∙ Products(ProductID, ProductName, Price, Quantity)
∙ Orders(OrderID, CustomerID, OrderDate, TotalAmount)
a) Select Operations:
1. Write a relational algebra expression to find all employees in the Employee table who work in
the IT department.
2. How would you select all products from the Products table with a price greater than $50?
3. Write an expression to retrieve all orders from the Orders table placed in the month of
January.
b) Project Operation:
1. Write a relational algebra expression to project the Name and Salary columns from the
Employee table.
2. How would you project only the ProductName and Price columns from the Products table?
3. Write an expression to create a list of all unique department names from the Department table.
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
60
c) Rename Operations:
1. Write a relational algebra expression to rename the relation Employee to Staff.
2. How would you rename the attribute DeptID in the Employee table to DepartmentID?
3. Write an expression to rename the relation Products to Items.
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
61
a) Union Operations:
1. Write a relational algebra expression to combine the EmployeeNY and EmployeeCA tables into a single relation.
2. How would you use Union to merge Products_A and Products_B into a single list of products?
3. Write an expression to find all unique students by combining Students_UG and Students_G.
4. How would you create a combined list of employees from both EmployeeNY and EmployeeCA using Union?
Q. Sample Relations:
∙ EmployeeNY(EmpID, Name, DeptID, Salary)
∙ EmployeeCA(EmpID, Name, DeptID, Salary)
∙ Products_A(ProductID, ProductName, Price)
∙ Products_B(ProductID, ProductName, Price)
∙ Students_UG(StudentID, Name, Major)
∙ Students_G(StudentID, Name, Major)
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
62
b) Intersection Operations:
1. Write a relational algebra expression to find employees who are also managers.
2. How would you use Intersection to find products that are both in stock and have been sold?
3. Write an expression to identify students who are in both Students_A and Students_B.
4. How would you find employees who are assigned to the same department as managers using Intersection?
Sample Relations:
∙ Employee(EmpID, Name, DeptID)
∙ Manager(EmpID, DeptID)
∙ ProductStock(ProductID, Quantity)
∙ ProductSales(ProductID, QuantitySold)
∙ Students_A(StudentID, Name)
∙ Students_B(StudentID, Name)
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
63
c) Set difference Operations:
1. Write a relational algebra expression to find current employees by subtracting EmployeeLeft from EmployeeAll.
2. How would you use Set Difference to find products in Products_A that are not in Products_B?
3. Write an expression to identify students who have not graduated by subtracting Graduates
from StudentsAll.
4. How would you use Set Difference to find employees who are not managers by subtracting
Manager from EmployeeAll?
Sample Relations:
Practice Examples
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
64
a) Join Operations:
1. Write a relational algebra expression to join Employee with Department based on DeptID.
2. How would you join Orders with Customers to get a list of all orders along with the
customer names?
3. Write an expression to join OrderDetails with Products to get the product names for each
order.
4. How would you join Employee with Orders to find all employees who have placed
orders?
5. Write a relational algebra expression to join OrderDetails with Orders to get the order
dates for each product
Sample Relations:
∙ Employee(EmpID, Name, DeptID)
∙ Department(DeptID, DeptName)
∙ Orders(OrderID, CustomerID, OrderDate)
∙ Customers(CustomerID, CustomerName)
∙ Products(ProductID, ProductName)
∙ OrderDetails(OrderID, ProductID, Quantity)
Practice
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
65
Q1: Retrieve all professors who belong to Section 1. |
Q2: Find all students assigned to Prof_id = 1.�Q3: List the First_name and Last_name of all professors.�Q4: Retrieve only the Student IDs and their Prof_id. |
Q5: Rename the Professor table as Faculty. |
Q6: Rename the attribute Last_name in Student as Surname. |
Q7: Get a list of all unique last names from both Professor and Student. |
Q8: Find professors who are not assigned to any student. |
Q9: Pair every professor with every student. |
Q10: Find common first names in both Professor and Student tables. |
Q11: (Inner Join) Find names of students along with their professor’s name. |
Q12: (Left Outer Join) Get a list of all students along with their professor details |
Q13: (Right Outer Join) Get a list of all professors with their assigned students. |
Q14: (Full Outer Join) Get a list of all professors and students, matching where possible. |
Practice
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
66
Extended relational algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
67
Extended Relational Algebra
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
68
Function | Description |
COUNT() | Counts the number of rows |
SUM() | Returns the total sum of a numeric column |
AVG() | Calculates the average value |
MIN() | Returns the minimum value |
MAX() | Returns the maximum value |
Syntax:
SELECT AGGREGATE_FUNCTION(column_name)
FROM table_name
[WHERE condition]
[GROUP BY column_name];
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
69
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
1. Count() function: Count the total number of employees
SELECT COUNT(*) AS TotalEmployees
FROM Employee;
TotalEmployees |
5 |
Result Table:
Employee
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
70
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
2. SUM() – Total Salary of All Employees
SELECT SUM(Salary) AS TotalSalary
FROM Employee;
Result Table:
Employee
TotalSalary |
300000 |
50000+60000+55000+65000+70000
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
71
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
3. AVG() – Average Salary of Employees
SELECT AVG(Salary) AS AverageSalary
FROM Employee;
Result Table:
Employee
AverageSalary |
60000 |
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
72
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
4. MIN() and MAX() – Minimum and Maximum Salary
Result Table:
Employee
SELECT MIN(Salary) AS MinSalary, MAX(Salary) AS MaxSalary
FROM Employee;
MinSalary | MaxSalary |
50000 | 70000 |
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
73
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
5. SUM() Grouped by Department – Total Salary by Dept
SELECT Dept, SUM(Salary) AS DeptSalary
FROM Employee
GROUP BY Dept;
Result Table:
Employee
Dept | DeptSalary |
HR | 105000 |
IT | 125000 |
Finance | 70000 |
Sample SQL Queries Using Aggregation Functions
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
74
EmpID | Name | Dept | Salary |
101 | Alice | HR | 50000 |
102 | Bob | IT | 60000 |
103 | Charlie | HR | 55000 |
104 | David | IT | 65000 |
105 | Eva | Finance | 70000 |
6. COUNT() Grouped by Department – Number of Employees per Dept
SELECT Dept, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY Dept;
Result Table:
Employee
Dept | EmployeeCount |
HR | 2 |
IT | 2 |
Finance | 1 |
WHERE vs HAVING in SQL
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
75
Feature | WHERE | HAVING |
Filters | Rows (individual records) | Groups (aggregated results) |
Used with | SELECT, UPDATE, DELETE | SELECT (must be with GROUP BY) |
Evaluated | Before aggregation | After aggregation |
Can use aggregate functions? | No | Yes |
SELECT column1, column2
FROM table
WHERE condition;
SELECT column1, AGG_FUNCTION(column2)
FROM table
GROUP BY column1
HAVING condition;
WHERE Clause
HAVING Clause
WHERE in SQL
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
76
SaleID | Product | Category | Quantity | Price |
1 | Laptop | Electronics | 2 | 50000 |
2 | Phone | Electronics | 1 | 20000 |
3 | Shirt | Apparel | 3 | 1500 |
4 | Laptop | Electronics | 1 | 52000 |
5 | Pants | Apparel | 2 | 1800 |
6 | Phone | Electronics | 3 | 21000 |
SELECT *
FROM Sales
WHERE Category = 'Electronics';
SaleID | Product | Category | Quantity | Price |
1 | Laptop | Electronics | 2 | 50000 |
2 | Phone | Electronics | 1 | 20000 |
4 | Laptop | Electronics | 1 | 52000 |
6 | Phone | Electronics | 3 | 21000 |
Sales
HAVING in SQL
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
77
SaleID | Product | Category | Quantity | Price |
1 | Laptop | Electronics | 2 | 50000 |
2 | Phone | Electronics | 1 | 20000 |
3 | Shirt | Apparel | 3 | 1500 |
4 | Laptop | Electronics | 1 | 52000 |
5 | Pants | Apparel | 2 | 1800 |
6 | Phone | Electronics | 4 | 21000 |
SELECT Product, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY Product
HAVING SUM(Quantity) > 3;
Sales
Product | TotalQuantity |
Phone | 5 |
WHERE + GROUP BY + HAVING in SQL
PREPARED BY: MS. PRIYANKA KAMBLE RELATIONAL MODEL AND RELATIONAL ALGEBRA
78
SaleID | Product | Category | Quantity | Price |
1 | Laptop | Electronics | 2 | 50000 |
2 | Phone | Electronics | 1 | 20000 |
3 | Shirt | Apparel | 3 | 1500 |
4 | Laptop | Electronics | 1 | 52000 |
5 | Pants | Apparel | 2 | 1800 |
6 | Phone | Electronics | 4 | 21000 |
SELECT Product, SUM(Quantity) AS TotalQuantity
FROM Sales
WHERE Category = 'Electronics'
GROUP BY Product
HAVING SUM(Quantity) > 3;
Sales
Product | TotalQuantity |
Phone | 5 |