Relational algebra and calculus in Database management system
anilkumar621733
0 views
57 slides
Oct 15, 2025
Slide 1 of 57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
About This Presentation
Relational algebra and calculus in DBMS
Size: 344.21 KB
Language: en
Added: Oct 15, 2025
Slides: 57 pages
Slide Content
UNIT -2 Relational Algebra & Calculus
Relational Algebra Procedural query language used to query and manipulate data in a relational database It works on relations (tables) Produces new relations as output Provides a set of operations to retrieve data
Operation Symbol Description Example Selection Ο Selects rows that satisfy a condition Ο Age > 20 (Student) Projection Ο Selects specific columns Ο Name, Age (Student) Rename Ο Renames a relation or attributes Ο S(Student) Join β¨ Combines related tuples from two relations Student β¨ Course Division Γ· Finds tuples related to all tuples in another relation (used in complex queries)
SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
SELECT column1, column2 FROM table1 INTERSECT SELECT column1, column2 FROM table2;
Aggregate Functions in DBMS SELECT AGG_FUNC(column) FROM table [WHERE condition]; C ount() Sum() Avg () Min() Max()
SELECT COUNT(*) FROM EMPLOYEE; SELECT AVG(SALARY) FROM EMPLOYEE; SELECT MAX(SALARY) FROM EMPLOYEE WHERE DEPTID = 10; SELECT DEPTID, SUM(SALARY) FROM EMPLOYEE GROUP BY DEPTID; SELECT DEPTID, COUNT(*) FROM EMPLOYEE GROUP BY DEPTID;
Logical Operators in DBMS A ND OR NOT ALL ANY/SOME In Between Exists
SELECT * FROM EMPLOYEE WHERE SALARY > 50000 AND LOCATION = 'New Yorkβ; SELECT * FROM EMPLOYEE WHERE LOCATION = 'Chicago' OR SALARY > 65000; SELECT * FROM EMPLOYEE WHERE NOT LOCATION = 'New Yorkβ;
SELECT * FROM EMPLOYEE WHERE LOCATION IN ('Chicago', 'San Franciscoβ); SELECT * FROM EMPLOYEE WHERE SALARY BETWEEN 55000 AND 70000;
GROUP BY SELECT Department, COUNT(*) FROM Employees GROUP BY Department; SELECT Department, COUNT(*) FROM Employees GROUP BY Department HAVING COUNT(*) > 1; H AVING
SELECT name, age FROM Students ORDER BY age; ORDER BY SELECT name, marks FROM Students ORDER BY marks DESC; SELECT name, age, marks FROM Students ORDER BY age ASC, marks DESC;
SELECT TOP 5 name, marks FROM Students ORDER BY marks DESC; TOP SELECT name, marks FROM Students ORDER BY marks DESC LIMIT 5; LIMIT SELECT name, marks FROM Students ORDER BY marks DESC LIMIT 5 OFFSET 5;
ORDER BY SELECT Name, Age FROM Employees ORDER BY Age DESC; DELETE FROM Employees WHERE Age < 30;
SELECT * FROM Students WHERE Name LIKE 'A%β; SELECT * FROM Students WHERE Name LIKE '%nβ; SELECT * FROM Students WHERE Name LIKE '% sh %'; LIKE
BETWEEN SELECT * FROM Students WHERE Marks BETWEEN 50 AND 80; SELECT Β ProductName FROM Β Products WHERE Β ProductID =Β ANY Β ( SELECT Β ProductID Β Β FROM Β OrderDetails Β Β WHERE Β Quantity =Β 10 ); A NY SELECT column _name FROM table_name WHERE expression operator ANY (subquery);
SELECT column_name FROM table_name WHERE expression operator ALL (subquery); A LL SELECT Β ProductName FROM Β Products WHERE Β ProductID =Β ALL Β ( SELECT Β ProductID Β Β FROM Β OrderDetails Β Β WHERE Β Quantity =Β 10 );
ID Name City 1 Ravi Chennai 2 Anu Mumbai 3 Kiran Chennai 4 Meena Delhi 5 Arjun Mumbai DISTINCT SELECT DISTINCT City FROM Customers; Chennai Mumbai Delhi
Alias SELECT Name AS StudentName FROM Students; An alias is a temporary name given to a column or a table in SQL. It is only for display (does not change the real column or table name). Makes query results easier to read.
LIKE β pattern match BETWEEN β range check ALL / ANY β comparison with subquery set DISTINCT β remove duplicates
StudentID Name CourseID 1 Kavi 101 2 Arjun 102 3 Priya 103 4 Rani 101 Join A JOIN in SQL is used to combine rows from two or more tables based on a related column between them INNER JOIN CourseID CourseName 101 Computer Sci 102 Mathematics 104 Physics SELECT table1.column1, table2.column2, ... FROM table1 INNER JOIN table2 ON table1.common_column = table2.common_column; Students Courses
LEFT JOIN (LEFT OUTER JOIN) SELECT Students.Name , Courses.CourseName FROM Students LEFT JOIN Courses ON Students.CourseID = Courses.CourseID ; Returns all rows from the left table (Students) + matching rows from right (Courses). If no match, NULL is shown.
RIGHT JOIN (RIGHT OUTER JOIN) SELECT Students.Name , Courses.CourseName FROM Students RIGHT JOIN Courses ON Students.CourseID = Courses.CourseID ; Returns all rows from the right table (Courses) + matching rows from left (Students).
FULL OUTER JOIN Returns all rows from both tables. If no match, NULL is placed . SELECT Students.Name , Courses.CourseName FROM Students FULL OUTER JOIN Courses ON Students.CourseID = Courses.CourseID ;
Name CourseID CourseName Kavi 101 Computer Sci Arjun 102 Mathematics Rani 101 Computer Sci NATURAL JOIN SELECT * FROM table1 NATURAL JOIN table2; CourseID is the common column in both tables β so NATURAL JOIN matches automatically. Priya is excluded ( CourseID = 103 not in Courses). Physics (104) excluded because no student has CourseID 104.
Advanced SQL
Analytical Queries Often called window functions or analytic functions . These queries go beyond simple GROUP BY and allow calculations across sets of rows while still keeping row-level detail.
What Are Analytical Queries Perform calculations across a set of rows related to the current row. Provide rankings, running totals, moving averages, percentiles, etc. Use window functions with the OVER() clause.
Employee_id Name Department salary 1 Alice HR 5000 2 Bob HR 6000 3 Charlie IT 7000 4 David IT 8000 5 Eva IT 9000 6 Frank Sales 5500 7 Grace Sales 6500
employee_id name department salary dept_total_salary 1 Alice HR 5000 11000 2 Bob HR 6000 11000 3 Charlie IT 7000 24000 4 David IT 8000 24000 5 Eva IT 9000 24000 6 Frank Sales 5500 12000 7 Grace Sales 6500 12000 Example Queries with OVER() Total salary by department SELECT employee_id , name, department, salary, SUM(salary) OVER(PARTITION BY department) AS dept_total_salary FROM employees;
What are Ranking Functions Ranking functions assign a rank, position, or number to rows in a result set. They require OVER() with ORDER BY (and often PARTITION BY). ROW_NUMBER() β assigns a unique sequential number to each row (no ties). RANK() β assigns rank, but leaves gaps when values are tied.
name department salary row_num Eva IT 9000 1 David IT 8000 2 Charlie IT 7000 3 Frank Sales 6500 4 Grace Sales 6500 5 Bob HR 6000 6 Alice HR 5000 7 SELECT name, department, salary, ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary DESC) AS row_num FROM employees;
Name department salary salary_rank Eva IT 9000 1 David IT 8000 2 Charlie IT 7000 3 Frank Sales 6500 4 Grace Sales 6500 4 Bob HR 6000 6 Alice HR 5000 7 SELECT name, department, salary, RANK() OVER(ORDER BY salary DESC) AS salary_rank FROM employees
What is a View A View is a virtual table based on the result of an SQL query. It does not store data physically (by default); it just stores the SQL query definition. CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
CREATE VIEW high_salary_employees AS SELECT name, department, salary FROM employees WHERE salary > 7000; SELECT * FROM high_salary_employees ;
Functional Dependencies A functional dependency (FD) describes a relationship between attributes in a relation (table). If attribute X determines attribute Y , it is written as: XβY Example: Aadhaar Card β Person Details AadhaarNo β{ Name,DOB,Address } But the reverse is not true (two people can have the same name).
Trivial Functional Dependency Non-Trivial Functional Dependency Partial Dependency Full Functional Dependency Types of Functional Dependencies
Trivial Functional Dependency A dependency is trivial if the right-hand side (RHS) is a subset of the left-hand side (LHS). Example: {π πππππ,ππππ}β βββ βππππ (because Name is already part of { RollNo , Name})
Non-Trivial Functional Dependency A dependency is non-trivial if RHS is not a subset of LHS Example:π πππππβ βββ βππππ (Name is not part of RollNo )
Partial Dependency When a part of a composite key determines a non-key attribute. Example: In relation ( RollNo , CourseCode β StudentName ), ( RollNo , CourseCode ) is key. RollNo β StudentName (partial dependency, since only part of key determines a non-key). Analogy: From just RollNo , you already know StudentName , even without CourseCode .
Full Functional Dependency A dependency where the entire composite key is required to determine the attribute. Example: ( RollNo , CourseCode ) β Grade (both together determine grade). Analogy: You need both RollNo and CourseCode to know the grade.
Armstrongβs Axioms Armstrongβs axioms are a set of inference rules used to derive all possible functional dependencies (FDs) from a given set. They form the foundation for reasoning about FDs .
If Y β X , then πβ ββ βββ ββ βπ Example: { RollNo , Name} β Name (Because Name is part of the left side). Reflexivity Rule
Augmentation Rule If X β Y , then XZ β YZ Example: RollNo β Name By Augmentation: { RollNo , Dept} β {Name, Dept} Knowing RollNo + Dept also gives you Name + Dept .
Transitivity If X β Y and Y β Z , then X β Z Example 1 (Student Table): RollNo β Dept , Dept β HOD, RollNo β HOD
Normalization in DBMS
Normalization is a systematic approach to organize data within a database to reduce redundancy and eliminate undesirable characteristics such as insertion, update, and deletion anomalies. Normalization
1NF (First Normal Form) in DBMS Each cell of the table contains atomic (indivisible) values. There are no repeating groups or arrays. Each record must be unique (no duplicate rows).
StudentID Name Subjects 1 Rahul Math, Science 2 Priya English, History 3 Arjun Physics, Chemistry StudentID Name Subject 1 Rahul Math 1 Rahul Science 2 Priya English 2 Priya History 3 Arjun Physics 3 Arjun Chemistry 1NF Normal table
It is already in 1NF. There is no partial dependency. 2NF removes partial dependency. This matters only when the table has a composite primary key. 2NF (Second Normal Form) in DBMS
StudentID CourseID StudentName CourseName 1 C1 Rahul Math 2 C2 Priya English 1 C2 Rahul English StudentID StudentName 1 Rahul 2 Priya CourseID CourseName C1 Math C2 English StudentID CourseID 1 C1 2 C2 1 C2 2 NF Normal table
It is already in 2NF . It has no transitive dependency . Transitive dependency means: A non-prime attribute depends on another non-prime attribute, instead of depending directly on the primary key. 3 NF
StudentID Course Instructor 1 DBMS Prof. A 2 DBMS Prof. A 3 Networks Prof. B Instructor Course Prof. A DBMS Prof. B Networks StudentID Instructor 1 Prof. A 2 Prof. A 3 Prof. B BCNF - Boyce-Codd Normal Form