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

Module-2

Module 2 covers Relational Algebra, a procedural query language that serves as the theoretical foundation for relational databases and SQL. It details fundamental operators such as Selection, Projection, Union, and various types of Joins, explaining their syntax and providing examples. Additionally, it introduces SQL commands for database management, including creating, updating, and deleting data, along with data types used in SQL.

Uploaded by

gracyma007
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)
3 views

Module-2

Module 2 covers Relational Algebra, a procedural query language that serves as the theoretical foundation for relational databases and SQL. It details fundamental operators such as Selection, Projection, Union, and various types of Joins, explaining their syntax and providing examples. Additionally, it introduces SQL commands for database management, including creating, updating, and deleting data, along with data types used in SQL.

Uploaded by

gracyma007
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/ 20

Database Management System: Module – 2

Module – 2
Relational Algebra
Relational Algebra

• Relational Algebra is a procedural query language.


• Relational algebra mainly provides a theoretical foundation for relational
databases and SQL.
• The main purpose of using Relational Algebra is to define operators that
transform one or more input relations into an output relation.
• Relational algebra consists of a certain set of rules or operations that are widely
used to manipulate and query data from a relational database.
• Relational Algebra incorporates a collection of operations, such as filtering data
or combining data, that help us organize and manipulate data more efficiently.

The basic/fundamental operators used in Relational Algebra

1. Selection (σ)
2. Projection (π)
3. Union (U)
4. Set Difference (-)
5. Set Intersection (∩)
6. Rename (ρ)
7. Cartesian Product (X)
Selection (σ):

• Selection Operation is basically used to filter out rows from a given table based on
certain given condition. It basically allows you to retrieve only those rows that
match the condition as per condition passed during SQL Query.
• Select operation chooses the subset of tuples from the relation that satisfies the
given condition mentioned in the syntax of selection.
• The selection operation is also known as horizontal partitioning since it partitions
the table or relation horizontally.

P a g e 1 | 20
Database Management System: Module – 2

• Syntax:
σ condition(Relation)
• Example 1:
Given a relation Student (Roll, Name, Department, Fees, Team) with the following tuples:

o Select all the students of Team A :


Solution: σ Team = 'A' (Student)

o Select all the students of department ECE whose fees are greater than and
equal to 10000.
Solution: σ Department = 'ECE' and Fees >= 10000

P a g e 2 | 20
Database Management System: Module – 2

Example 2: Write a RA expression to find all the instructors working in finance


department.
Solution: σD_NAME =“Finance” (INSTRUCTOR)
Example 3: Find all instructors with salary greater than 50000
Solution: σ SALARY>50000 (INSTRUCTOR)
Example 4: Find all instructors working in finance department and drawing the salary
greater than 50000.
Solution: σ D_NAME =“Finance” ∧ SALARY>50000 (INSTRUCTOR)

Projection (π):

• Project operation selects (or chooses) certain attributes discarding


other attributes.
• While Selection operation works on rows, similarly projection operation
of relational algebra works on columns.
• It basically allows you to pick specific columns from a given relational
table based on the given condition and ignoring all the other remaining
columns.
• The Project operation is also known as vertical partitioning since it
partitions the relation or table vertically discarding other columns or
attributes.
• Syntax:

π Attribute (Relation)

P a g e 3 | 20
Database Management System: Module – 2

• Example:
Given a relation Faculty (Class, Dept, Position) with the following tuples:

• Project Class and Dept from Faculty

Solution: π Class, Dept (Faculty)

• Project Position from Faculty

Solution: π Position (Faculty)

Important point: The Project operation removes duplicate tuples.

P a g e 4 | 20
Database Management System: Module – 2

Union(U):

• Union Operator is basically used to combine the results of two queries into a single
result.
• The only condition is that both queries must return same number of columns with
same data types.
• The result of Union operation, which is denoted by A ∪ S, is a relation that basically
includes all the tuples that are present in A or in S, or in both, eliminating the
duplicate tuples.

Consider the following table of Students having different optional subjects in their
course.

Solution:

π (Student_Name) FRENCH U π (Student_Name) GERMAN

P a g e 5 | 20
Database Management System: Module – 2

Set Intersection (∩):

• Set Intersection basically allows to fetches only those rows of data that are
common between two sets of relational tables.
• The result of Intersection operation, which is denoted by A ∩ S, is a relation that
basically includes all the tuples that are present in both A an S.

From the above table of FRENCH and GERMAN, the Set Intersection is used as follows:

Solution:

π (Student_Name) FRENCH ∩ π (Student_Name) GERMAN

P a g e 6 | 20
Database Management System: Module – 2

Set Intersection (∩):

• Set Intersection basically allows to fetches only those rows of data that are
common between two sets of relational tables.
• The result of Intersection operation, which is denoted by A – S, is a relation that
basically includes all the tuples that are present in A but not in S.

From the above table of FRENCH and GERMAN, Set Difference is used as follows
Solution:
π (Student_Name) FRENCH - π( Student_Name) GERMAN

P a g e 7 | 20
Database Management System: Module – 2

Rename (ρ) :
• Rename operator basically allows you to give a temporary name to a specific
relational table or to its columns.
• It is very useful when we want to avoid ambiguity, especially in complex Queries.
• The symbol ‘ρ’ (rho) is used to denote the RENAME operator and R is the result of
the sequence of operation or expression.
• General form:

ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.

• Example-1: Query to rename the attributes Name, Age of table Department to A,


B.

ρ (A, B) (Department)

• Example-2: Query to rename the relation Student as Male Student and the
attributes of Student – RollNo, SName as (Sno, Name).

ρ MaleStudent(Sno, Name) π RollNo, SName(σ Condition(Student))

Cross Product (X):

• Cartesian product Operator combines every row of one table with every row of
another table, producing all the possible combination.
• It’s mostly used as a precursor to more complex operation like joins.
• Cross-product between two relations. Let’s say A and B, so the cross product
between A X B will result in all the attributes of A followed by each attribute of B.
Each record of A will pair with every record of B.

P a g e 8 | 20
Database Management System: Module – 2

Table A: Table B:
Ax B:

Joins in DBMS:

• A join is an operation that combines the rows of two or more tables based on
related columns.
• This operation is used for retrieving the data from multiple tables simultaneously
using common columns of tables.
• There are many types of Joins in SQL. Depending on the use case, we can use
different type of SQL JOIN clause. The most commonly used join types with
syntax and examples:

P a g e 9 | 20
Database Management System: Module – 2

▪ INNER JOIN
▪ LEFT JOIN
▪ RIGHT JOIN
▪ FULL JOIN
▪ Natural Join

SQL INNER JOIN

• The INNER JOIN keyword selects all rows from both the tables as long as the
condition is satisfied.
• This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.

• Syntax:

SELECT table1.column1, table1.column2, table2.column1 FROM table1


INNER JOIN table2
ON table1.matching_column = table2.matching_column;

SQL LEFT JOIN

• LEFT JOIN returns all the rows of the table on the left side of the join and matches
rows for the table on the right side of the join.
• For the rows for which there is no matching row on the right side, the result-set
will contain null.
• LEFT JOIN is also known as LEFT OUTER JOIN.

P a g e 10 | 20
Database Management System: Module – 2

• Syntax:
SELECT table1.column1, table1.column2, table2.column1 FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

SQL RIGHT JOIN

• RIGHT JOIN returns all the rows of the table on the right side of the join and
matching rows for the table on the left side of the join.
• It is very similar to LEFT JOIN for the rows for which there is no matching row on
the left side, the result-set will contain null.
• RIGHT JOIN is also known as RIGHT OUTER JOIN.

• Syntax:
SELECT table1.column1,table1.column2,table2.column1 FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

SQL FULL JOIN

• FULL JOIN creates the result-set by combining results of both LEFT


JOIN and RIGHT JOIN.
• The result-set will contain all the rows from both tables. For the rows for which
there is no matching, the result-set will contain NULL values.

P a g e 11 | 20
Database Management System: Module – 2

• Syntax:
SELECT table1.column1,table1.column2,table2.column1 FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

SQL Natural Join

• Natural join is an SQL join operation that creates a join on the base of the common
columns in the tables.
• To perform natural join there must be one common attribute (Column) between
two tables.
• Natural join will retrieve from multiple relations. It works in three steps.
• Syntax:

SELECT *
FROM TABLE1
NATURAL JOIN TABLE2
• Example:
DEPARTMENT EMPLOYEE

P a g e 12 | 20
Database Management System: Module – 2

SELECT * FROM employee NATURAL JOIN department;

Difference between Natural Join and Inner Join

• Natural Join joins two tables based on the same attribute name and datatypes.

• The resulting table will contain all the attributes of both the table but keep only
one copy of each common column while Inner Join joins two tables on the basis of
the column which is explicitly specified in the ON clause.

• The resulting table will contain all the attributes from both tables including the
common column also.

Examples for Joins in DBMS:

P a g e 13 | 20
Database Management System: Module – 2

1. INNER JOIN
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;

2. Left Join:

SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student


LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

P a g e 14 | 20
Database Management System: Module – 2

3. Right Join:

SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student


RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

4. Full Join:

SELECT Student.NAME,StudentCourse.COURSE_ID FROM Student


FULL JOIN StudentCourse ON StudentCourse.ROLL_NO = Student.ROLL_NO:

P a g e 15 | 20
Database Management System: Module – 2

Overview of SQL Query Language

• Structured Query Language is a standard Database language that is used to


create, maintain, and retrieve the relational database.
• SQL is case insensitive. But it is a recommended practice to use keywords (like
SELECT, UPDATE, CREATE, etc.) in capital letters and use user-defined things
(like table name, column name, etc.) in small letters.

SQL COMMANDS

Data Types in SQL:


• In SQL there are three main data types: string, numeric, and date and time.
String Data Types:

CHAR(size) A FIXED length string (can contain letters, numbers, and


special characters). The size parameter specifies the
column length in characters - can be from 0 to 255. Default
is 1
VARCHAR(size) A VARIABLE length string (can contain letters, numbers,
and special characters). The size parameter specifies the
maximum string length in characters - can be from 0 to
65535

P a g e 16 | 20
Database Management System: Module – 2

Numeric Data Types:

IT(size) A bit-value type. The number of bits per value is specified


in size. The size parameter can hold a value from 1 to 64.
The default value for size is 1.
TINYINT(size) A very small integer. Signed range is from -128 to 127.
Unsigned range is from 0 to 255. The size parameter
specifies the maximum display width (which is 255)
SMALLINT(size) A small integer. Signed range is from -32768 to 32767.
Unsigned range is from 0 to 65535. The size parameter
specifies the maximum display width (which is 255)
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to
8388607. Unsigned range is from 0 to 16777215.
The size parameter specifies the maximum display width
(which is 255)

Date and Time Data Types

P a g e 17 | 20
Database Management System: Module – 2

CREATE DATABASE Statement:

Syntax:
CREATE DATABASE databasename;
Example:
CREATE DATABASE testDB;
CREATE TABLE Statement:

Syntax:
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example:
CREATE TABLE Customer
( CustomerID INT,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);

P a g e 18 | 20
Database Management System: Module – 2

DROP DATABASE Statement

Syntax:
DROP DATABASE databasename;
Example:
DROP DATABASE testDB;

DROP TABLE Statement

Syntax:
DROP TABLE table_name;
Example:
DROP TABLE Persons;

SELECT Query:

Syntax:

SELECT column1,column2…. FROM table_name ;

Example:

SELECT CustomerID, CustomerName, LastName FROM Customer;

SELECT Statement with WHERE Clause:

SELECT CustomerName FROM Customer where Age = '21';

SQL SELECT Statement with GROUP BY Clause

SELECT COUNT (item), Customer_id FROM Orders GROUP BY order_id;

Statement with HAVING Clause

SELECT Department, sum(Salary) as Salary FROM employee GROUP BY


department HAVING SUM(Salary) >= 50000;

P a g e 19 | 20
Database Management System: Module – 2

SELECT Statement with ORDER BY clause in SQL

SELECT * FROM Customer ORDER BY Age DESC;

SQL INSERT INTO Statement

Syntax:
• INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
• INSERT INTO table_name VALUES (value1, value2, value3, ...);

UPDATE Statement
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
SQL DELETE Statement

Syntax:

DELETE FROM table_name WHERE condition;

Example:

DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

P a g e 20 | 20

You might also like