SE_DB_CH6 (2)
SE_DB_CH6 (2)
Eg1.: Select the employee tuples whose experience is greater than five or
salary is greater than 18,000
σ experience>5 OR salary>18,000 (Employee)
01 Kebede 11 SE 20
02 Abebe 11 SE 20
Intersection operation
It displays the common values in both relations. It is
denoted by ∩.
Eg: Find a person who is student and lecturer
Extention_Stud Lecturer
Id Name Id Name
001 Abebe 003 Haleta
002 Kebede 004 Beza
003 Haleta 008 Samuel
Extention_Stud ∩ Lecturer
Id Name
003 Haleta
Cont…
Set Difference (−)
The result of set difference operation is tuples, which are present
in one relation but are not in the second relation.
Eg: ∏ author (Books) − ∏ author (Articles)
Result: Provides the name of authors who have written books but
not articles.
The JOIN operation (⋈)
It is a binary operation
Join is a combination of a Cartesian product followed by a
selection process.
A Join operation pairs two tuples from different relations, if and
only if a given join condition is satisfied.
The tables in DBMS are associated using the primary key and
foreign keys.
Syntax: R⋈join_condition S
There are mainly two types of joins in DBMS:
1.Inner Joins: Theta, Natural, EQUI
2.Outer Join: Left, Right, Full
The JOIN operation (⋈) cont…
Inner join
It is used to return rows from both tables which satisfy the
given condition. It is the most widely used join operation and can
be considered as a default join-type
An Inner join or equijoin is a comparator-based join which uses
equality comparisons in the join-predicate.
However, if you use other comparison operators like “>” it can’t
be called equijoin.
The JOIN operation (⋈) cont…
Theta Join
▪ It allows you to merge two tables based on the condition
represented by theta.
▪ Theta joins work for all comparison operators.
▪ It is denoted by symbol θ. The general case of JOIN operation is
called a Theta join.
Join_condition: Ai θ Bj Where Ai : Attributes of R1
Bj : Attributes of R2
Syntax: R1⋈ Ai θ Bj R2 θ : {=, <, <=,>,>=, ≠
Cont…
Name A2
A3
Hana 35
R1 R2 25
Dawit 40
35
Solomon 45
20
Eg. Find R1 ⋈ A2<=A3 R2
First: Cartesian operation performed Secondly: THETA join performed
Name A2 A3
Hana 35 25
Hana 35 35 Name A2 A3
Hana 35 20 Hana 35 35
Dawit 40 25
Dawit 40 35
Dawit 40 20
Solomon 45 25
Solomon 45 35
Solomon 45 20
The JOIN operation (⋈) cont…
Equijoin
When Theta join uses only equality comparison operator,
it is said to be equijoin.
The JOIN operation (⋈) cont…
The NATURAL JOIN operation (⋈/*)
▪ It does not use any comparison operator.
▪ It does not concatenate the way a Cartesian product does.
▪ We can perform a Natural Join only if there is at least one common attribute that exists
between two relations.
▪ In addition, the attributes must have the same name and domain.
Result 01 QA 1 40,000
02 Developer 2 80,000
The JOIN operation (⋈) cont…
Outer Joins
▪ An inner join includes only those tuples with matching
attributes and the rest are discarded in the resulting
relation.
▪ Therefore, we need to use outer joins to include all the
tuples from the participating relations in the resulting
relation.
▪ There are three kinds of outer joins: left outer join, right
outer join, and full outer join.
Cont…
Left Outer Join(R S)
▪ All the tuples from the Left relation, RL, are included in the resulting relation.
▪ If there are tuples in RL without any matching tuple in the Right relation RR,
then the RR-attributes of the resulting relation are made NULL.
RL A B RR A B
100 Database 100 Alex
101 Mechanics 102 Maya
102 Electronics 104 Mira
RL RR will be
A B C D
100 Database 100 Alex
101 Mechanics ----- ------
102 Electronics 102 Maya
Cont…
Right Outer Join: ( R S)
▪ All the tuples from the Right relation, RR, are included in the
resulting relation.
▪ If there are tuples in RR without any matching tuple in RL, then
the RL-attributes of resulting relation are made NULL.
▪ RL RR will be:
A B C D
100 Database 100 Alex
102 Electronics 102 Maya
----- ------ 104 Mira
Cont…
Full Outer Join: ( R S)
▪ All the tuples from both participating relations are included in
the resulting relation.
▪ If there are no matching tuples for both relations, their respective
unmatched attributes are made NULL.
▪ RL RR
A B C D
Example: To find the first and last names of all employees whose
salary is above $50,000, we can write the following tuple calculus
expression:
{t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000}
The condition EMPLOYEE(t) specifies that the range relation of
tuple variable t is EMPLOYEE.
The first and last name of each EMPLOYEE tuple t that satisfies
the condition t.SALARY>50000 (SELECTION SALARY >50000)
will be retrieved.
Cont…
▪ Example: { T.name | Author(T) AND T.article = 'database' }
Output: This query selects the tuples from the AUTHOR relation.
It returns a tuple with 'name' from Author who has written an
article on 'database’.
▪ TRC (tuple relation calculus) are free variables unless can be
quantified. So that, we can use Existential (∃) and Universal
Quantifiers (∀).
▪ The phrase "for every x'' (sometimes "for all x'') is called a
universal quantifier and is denoted by ∀x.
▪ The phrase "there exists an x such that'' is called an existential
quantifier and is denoted by ∃x.
To remember
Cont…
Eg:
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output: This query will yield the same result as the previous
one.
Cont…
Domain calculus
▪ Variable range over domain(particular element)
▪ Filtering variable uses the domain of attributes.
▪ Domain relational calculus uses the same operators as tuple
calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).
▪ It uses Existential (∃) and Universal Quantifiers (∀) to bind the
variable.
Notation: { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where: a1, a2 are attributes
P stands for formula built by inner attributes
Eg: {< article, page, subject > | ∈ javatpoint ∧ subject =
'database’}
Output: This query will yield the article, page, and subject
from the relational javatpoint, where the subject is a
database.
SQL (Structured Query Language)
It is a computer language for storing, manipulating and retrieving
data stored in relational database.
SQL is an ANSI (American National Standards Institute) standard, but
there are many different versions of the SQL language.
SQL is the standard language for Relation Database System.
All relational database management systems like MySQL, MS
Access, Oracle, Sybase, Informix, postgres and SQL Server use SQL
as standard database language.
Query: question we ask to our database/tables.
SQL uses the terms table, row, and column for relation, tuple, and
attribute, respectively.
Why SQL?
Allows users to access data in relational database management systems.
Allows users to describe the data.
Allows users to define the data in database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries &
pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views
SQL is an ANSI standard computer language
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert new records in a database
SQL can delete records from a database
SQL can update records in a database
SQL is easy to learn
SQL (Structured Query Language)
Use comparison conditions on only parts of a character string, using the LIKE
comparison operator → used for string pattern matching.
Partial strings are specified using two reserved characters: modulo (%) replaces
an arbitrary number of zero or more characters, and the underscore (_ ) replaces
a single character.
For example, consider the following query.
E.g: select * from student where department LIKE ’%soft%’;
The above query displays all students whose department likes soft.
To display students with cgpa having 7 at third place in their grade → select *
from student where cgpa like ’ 7 ’;
Cont…
✓ To display students with cgpa between 3.6 and 4.0;
student;