Unit 4
Unit 4
Unit 4
✓ When you insert or update data in a column with “not null constraint”, you
are required to provide a valid value, and leaving it empty is not allowed.
Check Constraint
It allows you to ensure a value meets a certain condition before it is inserted
into a table.
1. Basic 2. Derived/advanced
It is used to retrieve tuples(rows) from the table where the given condition is satisfied.
Example: Suppose we want the row(s) from STUDENT Relation where "AGE" is 20
Example: Suppose we want the names of all students from STUDENT Relation.
∏ NAME(STUDENT) NAME
Aman
Atul
Baljeet
Harsh
Prateek
Rename (ρ)
It is denoted by "Rho"(ρ).
ρ(STUDENT_NAME,∏ NAME(STUDENT))
Cross Product(X)
It is denoted by symbol ‘X’.
STUDENT ✕ DETAILS
It is denoted by a "Hyphen"(-) and it returns all the tuples(rows) which are in relation
R but not in relation S.
Set operators: Intersection & Set Difference
Example:
FRENCH GERMAN
Student_Name Roll_Number Student_Name Roll_Number
Vivek 13
Ram 01
Geeta 17
Mohan 02
Shyam 21
Vivek 13
Rohan 25
Geeta 17
Set operators: Intersection
Example: If we want the names which are present in STUDENT as
well as in EMPLOYEE relation.
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
NAME
Baljeet
Harsh
Set operators: Set Difference
Example: To know the names of students who are in STUDENT
Relation but not in EMPLOYEE Relation.
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
NAME
Aman
Atul
Prateek
Join (⋈)
A join is an operation that combines the rows/tuple of two or more tables
based on common attribute/columns.
The main purpose of Join is to retrieve the data from multiple tables.
It is denoted by ⨝.
• Equi Join
• Natural Join
➢ Natural Join joins two tables based on the same attribute name and datatypes.
Natural Join
Department Employee
Output:
SELECT * FROM department
NATURAL JOIN employee;
Outer Join
Outer joins retrieves matching as well as non-matching records from related tables.
Types:
Output:
SELECT * FROM department
LEFT OUTER JOIN employee ON
department.DEPT_NAME =
employee.DEPT_NAME;
Right Join
It retrieves all records from the right table and retrieves matching records from the left table.
3 9 3 27 3 9 27
4 16 5 125 5 - 125
Right Join
Department Employee
Output:
SELECT * FROM department
RIGHT OUTER JOIN employee ON
department.DEPT_NAME =
employee.DEPT_NAME;
Full Join
It combines the results of both LEFT JOIN and RIGHT JOIN. The result set
will contain all the rows from both tables.
Example: Table A Table B A⟗ B
Number Square Number Cube Number Square Cube
2 4 2 8 2 4 8
3 9 3 27 3 9 27
4 16 5 125 4 16 NULL
5 NULL 125
Full Join
Department Employee
Output:
SELECT * FROM department
FULL OUTER JOIN employee ON
department.DEPT_NAME =
employee.DEPT_NAME;
Aggregate Function
Aggregation function is used to perform the calculations on multiple rows of a single
column of a table.
It returns a single value.
Aggregate Function
CREATE TABLE Employee (Id INT, Name CHAR(1), Id Name Salary
Salary DECIMAL(10)); 1 A 802
INSERT INTO Employee (Id, Name, Salary)
VALUES (1, 'A', 802), 2 B 403
(2, 'B', 403),
(3, 'C', 604), 3 C 604
(4, 'D', 705),
4 D 705
(5, 'E', 606),
(6, 'F', NULL); 5 E 606
6 F NULL
Aggregate Function
Aggregate Function Example:
--Count the number of employees
Total Employees: 6
SELECT COUNT(*) AS Total Employees FROM Employee;
-- Calculate the total salary
SELECT SUM(Salary) AS Total Salary FROM Employee; Total Salary: 3120
-- Find the average salary
SELECT AVG(Salary) AS Average Salary FROM Employee; Average Salary: 624
-- Get the highest salary
SELECT MAX(Salary) AS Highest Salary FROM Employee;
Highest Salary: 802
-- Determine the lowest salary
SELECT MIN(Salary) AS Lowest Salary FROM Employee;
Lowest Salary: 403
Thank You!!!
www.paruluniversity.ac.in