Relational Query Languages
▪Relational query languages are specialized languages designed to
interact with relational databases, enabling users to perform various
operations like querying, updating, and managing data stored in
relational tables.
▪ In the 1970s, QUEL was part of the Ingres database system, developed
at the University of California, Berkeley.
▪ SEQUEL, or Structured English Query Language, was developed as
part of IBM's System R project in the early 1970s. It later evolved into
SQL (Structured Query Language).
▪ SQL is the most popular relational query language which allows users to
define, manipulate, and retrieve data.
▪ The two mathematical Query Languages form the basis for “real” query
languages (e.g. SQL), and for implementation are: Relational Algebra &
Relational Calculus.
3/5/2024
ECEg 4181 - By: Eyob S. 3
4.
SQL (Structured QueryLanguage)
▪ Structured Query Language (SQL) is a query language that is
standardized for Relational DBMS (RDBMS).
▪ Data Definition: Create and manage database structures.
▪ CREATE, ALTER, DROP, TRUNCATE for defining database structures.
▪ Data Manipulation: Work with the data inside the database.
▪ INSERT, UPDATE, DELETE for modifying data.
▪ Data Querying: Retrieve specific data from tables (or Filtering).
▪ SELECT statements for retrieving data.
▪ Data Control (DCL): Manage access to the database.
▪ E.g., GRANT, REVOKE
▪ Transaction Control (TCL): Manage transactions and maintain
database integrity. E.g., COMMIT, ROLLBACK, SAVEPOINT
3/5/2024
ECEg 4181 - By: Eyob S. 4
5.
Getting Started withSQL
▪ Log in to SQL from command prompt (cmd):
▪ mysql -u root -p
▪ mariadb -u root -p
▪ Write password or if it without password, login: mysql -u root
▪ Create a new user:
▪ CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
▪ SELECT User FROM mysql.user;
▪ Grant privileges to the new user:
▪ GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
▪ GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'username'@'localhost’;
▪ FLUSH PRIVILEGES; SHOW GRANTS FOR 'username'@localhost;
▪ Remove user account:
▪ DROP USER 'username'@localhost;
3/5/2024
ECEg 4181 - By: Eyob S. 5
Display all databases using:
SHOW DATABASES;
6.
Data Definition: CREATE
▪The CREATE statements in SQL is used to define new database
objects, such as databases, schemas, tables, views, or indexes.
▪ CREATE DATABASE statement defines a database:
▪ CREATE DATABASE <database_name>;
▪ E.g., CREATE DATABASE school;
▪ To start working with it: USE <database_name>
▪ CREATE SCHEMA defines a subdivision within a database:
▪ CREATE SCHEMA <schema_name> AUTHORIZATION <owner>;
▪ E.g., CREATE SCHEMA my_school AUTHORIZATION Abebe;
▪ CREATE OR REPLACE TABLE defines or replaces existing table:
3/5/2024
ECEg 4181 - By: Eyob S. 6
A schema is
like a logical
group within
a database.
A database is the overall container or
storage system that holds your data.
CREATE TABLE [IF NOT EXISTS] table_name(column_1_definition,
column_2_definition, ..., table_constraints
) engine=storage_engine;
7.
CREATE OR REPLACETABLE students(
student_id INT AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
major VARCHAR(50),
PRIMARY KEY(student_id) );
▪ Alternatively, first existing table can be deleted before creating newer as:
▪ DROP TABLE IF EXISTS <table_name>;
▪ CREATE TABLE <table_name>(<column_name> <type> <constraint>,…);
▪ DROP TABLE IF EXISTS projects;
▪ CREATE TABLE projects( project_id int auto_increment,
project_name varchar(255) not null, begin_date date,
completed bool default false, cost decimal(15,2) not null,
created_at timestamp default current_timestamp,
primary key(project_id) );
Example, students table
3/5/2024
ECEg 4181 - By: Eyob S. 7
CREATE TABLE IF NOT EXISTS students(
student_id INT AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
major VARCHAR(50),
PRIMARY KEY(student_id) );
8.
Data Definition: ALTER
▪To rename a table,
▪ ALTER TABLE <table_name> RENAME TO <new_table_name>;
▪ E.g., ALTER TABLE students RENAME TO pupils;
▪ To add a column to a table,
▪ ALTER TABLE <table_name> ADD <column_name> <column_definition>
[ FIRST | AFTER existing_column_name];
▪ E.g., ALTER TABLE students ADD email VARCHAR(255) NOT NULL AFTER last_name;
▪ ALTER TABLE students ADD phone VARCHAR(15), ADD address VARCHAR(255);
▪ To change the characteristics of a column,
▪ ALTER TABLE <table_name> MODIFY <column_name> <column_definition>;
▪ E.g., ALTER TABLE students MODIFY last_name VARCHAR(255) NOT NULL;
▪ To rename a column in a table,
▪ ALTER TABLE <table_name> CHANGE COLUMN <previous_name> <new_name> <col_definition>
▪ To drop a column in a table,
▪ ALTER TABLE <table_name> DROP COLUMN <column_name>;
▪ E.g., ALTER TABLE students DROP COLUMN email;
3/5/2024
ECEg 4181 - By: Eyob S. 8
ALTER TABLE statement
allows modification (adding,
changing, or dropping) of a
table, column or constraint
in a table.
▪ To verify the modification:
DESCRIBE <table_name>;
E.g., DESCRIBE students;
9.
Data Definition: DROP
▪The DROP TABLE statement removes or deletes a table from a database.
▪ DROP TABLE [IF EXISTS] <table_name>;
▪ E.g. 1, DROP TABLE IF EXISTS pupils;
▪ E.g. 2, DROP TABLE mountains, oceans;
▪ To remove a constraint:
ALTER TABLE <table_name>
DROP CONSTRAINT <constraint_name>;
E.g., ALTER TABLE students
ADD CONSTRAINT fk_dept
FOREIGN KEY(major)
REFERENCES departments(dept_name)
ON DELETE CASCADE
ON UPDATE CASCADE; -- creates fk
ALTER TABLE students
DROP CONSTRAINT fk_dept;
3/5/2024
ECEg 4181 - By: Eyob S. 9
The SHOW TABLES statement shows
changes:
SHOW [FULL] TABLES;
E.g. 1, SHOW TABLES;
E.g. 2, SHOW FULL TABLES;
SHOW [FULL] TABLES
FROM <database_name>;
E.g. 1, SHOW TABLES FROM school;
E.g. 2, SHOW FULL TABLES
LIKE '% school’;
E.g. 3, SHOW FULL TABLES
WHERE table_type='view';
E.g.4, SHOW FULL TABLES
WHERE table_type='base_table';
10.
Data Definition: TRUNCATE
▪TRUNCATE TABLE statement deletes all rows from a table.
▪ TRUNCATE [TABLE] <table_name>;
▪ E.g., TRUNCATE TABLE students;
▪ TRUNCATE TABLE statement will fail
if the truncating table has
a foreign key constraint.
▪ E.g., TRUNCATE countries; --ERROR
▪ Unlike DELETE statement, the number of affected rows by TRUNCATE TABLE
statement is always zero, which should be understood as no information.
▪ To display changes in the table:
▪ SELECT * FROM <table_name>;
▪ E.g., SELECT * FROM items;
3/5/2024
ECEg 4181 - By: Eyob S. 10
11.
Data Manipulation:
INSERT, UPDATE,DELETE
▪ INSERT INTO students SET first_name = 'Jonathan',...
▪ INSERT INTO <table_name> | <view_name> [(column_list)]
VALUES (<list_of_values_or_expressions>)
▪ E.g., INSERT INTO students
VALUES(1,‘Mekash’, ‘Bona’, ‘2000-05-19’, ‘ECE’);
▪ INSERT INTO students(first_name, last_name, major)
VALUES(‘Yonas’, ’Daniel’, ‘BME’);
▪ SELECT last_insert_id(); -- returns last id used in the table
▪ UPDATE <table_name> SET <column_name> = <value> WHERE <condtion>
▪ E.g., UPDATE students SET birth_date=‘2001-04-20’ WHERE student_id=1;
▪ DELETE FROM <table_name>| <view_name> WHERE <condtion>
▪ E.g., DELETE FROM students WHERE student_id=2; -- removes 1 row
3/5/2024
ECEg 4181 - By: Eyob S. 11
12.
Data Querying: SELECT
▪SELECT statement in SQL is used to retrieve data from one or more
tables in a relational database.
▪ SELECT [DISTINCT] <attribute_list> FROM <table_list> [LIMIT row_count];
▪ E.g., SELECT DISTINCT last_name, major FROM students;
▪ SELECT <attribute_list> FROM <table_list> WHERE <condition>;
▪ E.g., SELECT last_name FROM students WHERE major='ECE';
▪ SELECT last_name, chair_person FROM students, departments WHERE
last_name = ‘Siraj’ OR dept_name = ‘ECE’ LIMIT 1;
▪ Wildcards follows LIKE in the WHERE clause:
▪ Percentage (%): Represents zero, one, or multiple characters.
▪ Underscore (_): Represents exactly one character.
▪ Square Brackets ([]): Used to specify a range or set of characters.
▪ Caret (^) or Exclamation (!): Used inside [] to exclude characters.
▪ Escape Characters: E.g., WHERE name LIKE '50% OFF!' ESCAPE '';
3/5/2024
ECEg 4181 - By: Eyob S. 12
13.
Data Querying: SELECT
3/5/2024
ECEg4181 - By: Eyob S. 13
Find all owners with the string ‘Glasgow’ in their address.
(% means wildcard/any number of characters, _ means exactly one character)
In SQL the following simple comparison and logical operations are available
with the corresponding rules.
14.
EXAMPLES: SELECT
▪ E.g.,Retrieve 5 rows starting from the 6th row:
▪ SELECT last_name FROM students LIMIT 5 OFFSET 5;
▪ The ORDER BY clause is used to sort the results of a query in ascending
(ASC) or descending (DESC) order based on one or more columns.
▪ SELECT <column_list> FROM <table_name> ORDER BY column_name [ASC|DESC];
▪ SELECT last_name FROM students ORDER BY birth_date;
▪ The GROUP BY clause is used to group rows with the same values in
specified columns. It’s often used with aggregate functions like COUNT, SUM,
AVG, MIN, and MAX to perform calculations for each group.
▪ E.g.1, SELECT major, COUNT(*) FROM students GROUP BY major;
▪ E.g.2, CREATE VIEW my_students(Lname, major, age) AS SELECT last_name, major,
FLOOR(DATEDIFF(CURRENT_DATE, birth_date) / 365) AS age FROM students;
SELECT major, AVG(age) AS avg_age FROM students GROUP BY major ORDER BY
avg_age DESC;
3/5/2024
ECEg 4181 - By: Eyob S. 14
Relational Algebra
▪ RelationalAlgebra is a formal language for querying and manipulating
relational databases.
▪ It provides a foundation for understanding database operations and is
based on mathematical set theory and logic.
▪ Focuses on operations over sets, such as selection, projection, union,
intersection, and Cartesian product (also known as Cross product).
▪ The result of an operation is a new relation, which may have been
formed from one or more input relations.
▪ Forms the basis for SQL queries, though SQL is more practical for real-
world use.
▪ Useful for learning database query concepts and understanding the
underlying processes.
3/5/2024
ECEg 4181 - By: Eyob S. 16
Selection Operation (σ)
▪The SELECT operation is used to choose a subset of the tuples (or
records) from a relation that satisfies a selection condition.
▪ SELECT operation can be considered as a filter that keeps only those
tuples that satisfy a qualifying condition. Tuples satisfying the condition
are selected whereas the other tuples are discarded (filtered out).
▪ SELECT operation on attributes of a relation R is denoted by
▪ The relation S produced from the SELECT operation has the same
schema (i.e., the same attributes) as the relation R.
▪ Boolean expression uses comparison operators {=, <, ≤, >, ≥, ≠} and
logical operators ⋀(𝑎𝑛𝑑), ⋁(𝑜𝑟), ¬(𝑛𝑜𝑡) to form a selection condition.
3/5/2024
ECEg 4181 - By: Eyob S. 19
EXAMPLE 2:
Selection
▪ Query:Details of the members
who were born on 21/10/1997.
3/5/2024
ECEg 4181 - By: Eyob S. 21
SELECT * FROM Student
WHERE GPA <= 3.6 AND Age = 20
22.
EXAMPLE 3: Selection
3/5/2024
ECEg4181 - By: Eyob S. 22
SELECT * FROM EMPLOYEE
WHERE (Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000);
23.
SELECT Operation Properties
▪The symbol σ (sigma) is used to denote the select operator
▪ The SELECT operation is commutative:
▪
▪ Because of commutativity property, a cascade (sequence) of
SELECT operations may be applied in any order:
▪
▪ A cascade of SELECT operations may be replaced by a single
selection with a conjunction of all the conditions:
▪
▪ The number of tuples in the result of a SELECT is less than (or
equal to) the number of tuples in the input relation R
3/5/2024
ECEg 4181 - By: Eyob S. 23
24.
Projection Operation (π)
▪Projection in relational algebra is an operation used to retrieve
specific attributes (columns) from a relation (table), effectively
filtering out unwanted attributes while preserving the structure of
tuples (rows).
▪ PROJECT Operation is denoted by π (pi) in expression as follows
▪ The project operation removes any duplicate tuples
▪ This is because the result of the project operation must
be a set of tuples
▪ Mathematical sets do not allow duplicate elements
3/5/2024
ECEg 4181 - By: Eyob S. 24
25.
EXAMPLE 1: Projection
3/5/2024
ECEg4181 - By: Eyob S. 25
Query: Member IDs of members and the Book IDs
of the books they have borrowed books.
SELECT DISTINCT “ID”, “Short name” FROM “Relation”;
26.
▪ Example: Tolist each employee’s first and last name and salary,
the following is used:
▪
▪ SELECT DISTINCT Lname, Fname, Salary FROM EMPLOYEE
▪ Notice that DISTINCT in this SQL query, will eliminate duplicate tuples.
EXAMPLE 2: Projection
3/5/2024
ECEg 4181 - By: Eyob S. 26
27.
PROJECT Operation Properties
▪The number of tuples in the result of projection is always
less or equal to the number of tuples in R.
▪ If the list of attributes includes a key of R, then the number of tuples
in the result of PROJECT is equal to the number of tuples in R.
▪ PROJECT is not commutative
▪
▪ otherwise, the left-hand side is an incorrect expression.
▪ To retrieve the first name, last name, and salary of all employees
who work in department number 5, how can a select and a project
operation be applied together?
3/5/2024
ECEg 4181 - By: Eyob S. 27
Relational Algebra Expression
3/5/2024
ECEg4181 - By: Eyob S. 29
▪ A single relational algebra expression
▪ Solution:
▪ Or sequence of operations:
▪ SQL equivalent:
▪ SELECT DISTINCT Fname, Lname, Salary
FROM EMPLOYEE
WHERE Dno = 5;
Note that: ← the symbol is an assignment operator
30.
▪ The Renameoperation in relational algebra is used to rename a
relation (table) or its attributes (columns).
▪ It is helpful when working with queries that involve complex
operations or when the same relation is used multiple times in a
query, like in self-joins. The RENAME operator is denoted by
▪ the relation's name to S, and
the column (attribute) names to
▪ the relation's name only to S
▪ the column (attribute) names only to
Rename Operation (ρ)
3/5/2024
ECEg 4181 - By: Eyob S. 30
EXAMPLE 2: Rename
3/5/2024
ECEg4181 - By: Eyob S. 32
TEMP will have the same attributes as EMPLOYEE
▪ For convenience, we also use a shorthand for renaming attributes in an
intermediate relation:
▪
▪ On the other hand, R uses First_name instead of Fname & Last_name
instead of Lname though Salary is remained with the same name.
▪
▪ SQL equivalent is as follows:
UNION Operation (∪)
▪The Union operation in relational algebra is used to combine the
tuples (rows) from two relations (tables) into a single relation.
▪ The result includes all tuples from both relations, but duplicates
are removed, as a union must produce a valid set.
▪ Binary operation, denoted by: ∪
▪ The result of R ∪ S, is a relation that includes all tuples that are
either in R or in S or in both R and S
▪ R and S must have same number of attributes,
▪ Each pair of corresponding attributes must be type compatible (have
same or compatible domains),
▪ Finally, duplicate tuples will be eliminated in the result relation.
3/5/2024
ECEg 4181 - By: Eyob S. 34
35.
RESULT RESULT1∪ RESULT2
3/5/2024
ECEg 4181 - By: Eyob S. 35
SELECT * FROM (
SELECT Ssn
FROM RESULT1
UNION
SELECT Ssn
FROM RESULT2) AS RESULT
36.
3/5/2024
ECEg 4181 -By: Eyob S. 36
In this example of the 'union' operation we can store the result of the union
operation in a variable named 'All _Courses' as follows:
EXAMPLE
Intersect Operation (∩)
▪The intersect operation performs the same function as the
intersect operation in the set theory.
▪ Union compatibility required, which means we can perform
intersect operation only because both the tables have same
attributes.
▪ It is represented by ∩ symbol. This operation selects all those
tuples from the relation which are common in both the relation.
▪ It is denoted by: R1 ∩ R2 where R1 and R2 are two relations.
▪ Example: We have two courses table and we want to perform the
intersect operation on table Course_1(C_id, C_name) and
Course_2(C_id, C_name).
3/5/2024
ECEg 4181 - By: Eyob S. 38
39.
EXAMPLE
3/5/2024
ECEg 4181 -By: Eyob S. 39
So, in this example when we perform the intersect operation, we get only one
tuple(21, C++) which is common in both Course_1 table and Course_2 table.
Set Difference (orMinus)
▪ The set difference operation performs the same function as the
set difference operation in the set theory.
▪ It is denoted by - (minus) symbol.
▪ R1 - R2 where R1 and R2 are two tables.
▪ If we perform an operation R1 - R2 on relation(table) R1 and R2,
then the result would include only those tuples that are in table
R1 but not in R2.
▪ This operation removes all those tuples which are common in
both table R1 and R2, from R1 and gives output.
▪ For performing a set difference operation both the tables should
have the same attributes.
3/5/2024
ECEg 4181 - By: Eyob S. 41
42.
3/5/2024
ECEg 4181 -By: Eyob S. 42
• Example: We have two courses table and we want to perform the set
difference operation on table Course_1(C_id, C_name) and
Course_2(C_id, C_name). Remember, we can perform set difference
operation only because both the tables have same attribute.
43.
INTERSECT using MINUS
▪Can be computed using differences only: R – (R – S)
3/5/2024
ECEg 4181 - By: Eyob S. 43
R c1 c2
1 A
3 C
4 D
S c1 c2
2 B
5 C
4 D
T c1 c2
4 D
SELECT * FROM (
(SELECT * FROM R)
INTERSECT
(SELECT * FROM S)
) AS T;
SELECT * FROM (
(SELECT * FROM R) EXCEPT
(SELECT * FROM R
EXCEPT SELECT * FROM S)
) AS T;
T c1 c2
4 D
44.
Properties of ∪,∩ and -
▪ Notice that both UNION and INTERSECTION are commutative
operations; that is,
▪ R ∪ S = S ∪ R as well as R ∩ S = S ∩ R
▪ Both UNION and INTERSECTION can be treated as binary,
ternary or n-ary operations applicable to any number of relations
because both are also associative operations; that is,
▪ R ∪ (S ∪ T) = (R ∪ S) ∪ T as well as R ∩ (S ∩ T) = (R ∩ S) ∩ T
▪ INTERSECTION can be expressed in terms of union and set
difference as follows: R ∩ S = ((R ∪ S) − (R − S)) − (S − R)
▪ The MINUS operation is not commutative.
3/5/2024
ECEg 4181 - By: Eyob S. 44
45.
STUDENT - INSTRUCTOR
3/5/2024
ECEg4181 - By: Eyob S. 45
INSTRUCTOR - STUDENT
SELECT Fname, Lname
FROM INSTRUCTOR
EXCEPT
SELECT Fn AS Fname, Ln AS Lname
FROM STUDENT;
46.
▪ The symmetricdifference is a concept in set theory that refers
to the set of elements that belong to either of two sets, but not to
their intersection.
▪ In mathematical terms, the symmetric difference of relations (R)
and (S) is denoted as (R ∆ S), and can be expressed as:
R ∆ S = (R – S) ∪ (S – R)
▪ For example, T3 = T1 ∆ T2
Symmetrical Difference
3/5/2024
ECEg 4181 - By: Eyob S. 46
T1 c1 c2
1 A
3 C
4 D
T2 c1 c2
2 B
3 C
4 D
T3 c1 c2
1 A
2 B
SELECT * FROM (
(SELECT * FROM T1
EXCEPT
SELECT * FROM T2)
UNION
(SELECT * FROM T2
EXCEPT
SELECT * FROM T1)
) AS T3
47.
UNION ALL, INTERSECTALL,
and EXCEPT ALL
▪ In SQL, UNION ALL, INTERSECT ALL, and EXCEPT ALL are
extended set operations that include duplicates in the results, unlike
their standard counterparts which automatically eliminate duplicates.
▪ UNION ALL - Combines the rows from two or more relations and
does not perform duplicate elimination, making it faster and more
efficient than UNION.
▪ INTERSECT ALL - Retrieves rows that are common to both relations
and this keeps duplicates based on their occurrence in both tables.
▪ EXCEPT ALL - Retrieves rows that are in the first relation but not in
the second, including duplicates. If a row appears multiple times in the
first table and fewer times (or not at all) in the second table, the result
includes the extra occurrences.
3/5/2024
ECEg 4181 - By: Eyob S. 47
48.
EXAMPLE 1
SELECT c1,c2
FROM T1
UNION ALL
SELECT c1, c2
FROM T2;
3/5/2024
ECEg 4181 - By: Eyob S. 48
c1 c2
3 C
4 D
3 C
4 D
T1 c1 c2
1 A
3 C
4 D
T2 c1 c2
2 B
3 C
4 D
c1 c2
1 A
3 C
4 D
2 B
3 C
4 D
SELECT c1, c2
FROM T1
INTERSECT ALL
SELECT c1, c2
FROM T2;
49.
EXAMPLE 2
SELECT c1,c2
FROM T2
EXCEPT ALL
SELECT c1, c2
FROM T1;
3/5/2024
ECEg 4181 - By: Eyob S. 49
c1 c2
1 A
1 A
T1 c1 c2
1 A
1 A
3 C
4 D
T2 c1 c2
2 B
2 B
3 C
4 D
c1 c2
2 B
2 B
SELECT c1, c2
FROM T1
EXCEPT ALL
SELECT c1, c2
FROM T2;
50.
Cartesian Product (✕)
▪This operation is performed to merge columns from two relations.
It is denoted by the symbol X .
▪ Usually, the cross operation is not meaningful but are useful when
combined with some other operations like select, project etc.
▪ It is denoted by: R1 X R2 where R1 and R2 are two relations such
that the cross product will result in a table which has all the
attributes of the table R1 followed by the attributes of the R2.
▪ For example,
▪ We have two tables of Student(S_id, Name, Class, Age) and
Courses (C_id, C_name).
▪ Now, let’s perform the cross product of both tables
3/5/2024
ECEg 4181 - By: Eyob S. 50
51.
3/5/2024
ECEg 4181 -By: Eyob S. 51
SELECT *
FROM Student, Course;
SELECT *
FROM Student
CROSS JOIN Course;
or
EXAMPLE
52.
▪ Inner Join
▪Natural Join
▪ Theta Join
▪ Equi Join
Join Operations
▪ Join operation is used to combine two or more tables based on
the related attributes.
▪ It is basically a cross product followed by some more operations
like select, project etc.
▪ There are mainly two types of join which are further divided as
follows:
3/5/2024
ECEg 4181 - By: Eyob S. 52
▪ Outer Join
▪ Left Outer Join
▪ Right Inner Join
▪ Full Outer Join
53.
Inner Join
▪ Innerjoin is a type of join in which only those tuples are selected
which full fill the required conditions.
▪ All those tuples which do not satisfy the required conditions are
excluded.
▪ Let us now study in detail each type of inner join with example.
▪ JOIN operation can be specified as a CARTESIAN PRODUCT
followed by a SELECT operation, as we discussed:
3/5/2024
ECEg 4181 - By: Eyob S. 53
54.
Natural Join (⋈)
▪Natural Join is a join which is performed if there is a common
attribute between the relations.
▪ It is denoted by R1 ⋈ R2 where R1 and R2 are two relations.
▪ For example,
▪ We have two tables of Student(S_id, Name, Class, Age, C_id)
and Courses(C_id, C_name).
▪ Now, we will perform natural join on both the tables
3/5/2024
ECEg 4181 - By: Eyob S. 54
55.
3/5/2024
ECEg 4181 -By: Eyob S. 55
SELECT * FROM Student
NATURAL JOIN Course;
SELECT * FROM Student s
INNER JOIN Course c ON c.C_id=s.C_id
or
EXAMPLE
PROJ_DEPT PROJECT⋈ DEPARTMENT
3/5/2024
ECEg 4181 - By: Eyob S. 57
DEPT_LOCS DEPARTMENT ⋈ DEPT_LOCATIONS
58.
Theta Join (⋈θ)
▪Theta join is a join which combines the tuples from different relations
according to the given theta condition.
▪ The join condition in theta join is denoted by theta (θ) symbol.
▪ This join uses all kind of comparison operator.
▪ It is denoted by where R1 and R2 are relations such
that they don't have any common attribute.
▪ Example: We have two tables of Student(S_id, Name, Std, Age)
and Courses (Class, C_name).
▪ Next, we will perform theta join on both the tables.
3/5/2024
ECEg 4181 - By: Eyob S. 58
59.
3/5/2024
ECEg 4181 -By: Eyob S. 59
SELECT * FROM Student s JOIN Course c ON s.Std = c.Class;
E.g.,
60.
Equi Join
▪ Equi-Joinis a type of theta join where we use only the equality (=)
operator, unlike theta join where we can use any operator.
▪ E.g.,
3/5/2024
ECEg 4181 - By: Eyob S. 60
SELECT * FROM new1 JOIN new2
ON new1.ID = new2.ID;
SELECT * FROM new1 a, new2 b
WHERE a.ID = b.ID;
or
new1 ⋈new1.ID=new2.ID new2
Outer Join
▪ InInner Join, we matched rows are returned and unmatched rows
are not returned.
▪ But, in outer join, we include those tuples which meet the given
condition along with that, we also add those tuples which do not
meet the required condition.
▪ The result also includes the tuples from the left and right tables
which do not satisfy the conditions.
▪ Based on the tuples that are added from left, right or both the
tables, the outer join is further divided into three types.
▪ We will now study about its types with the help of examples.
▪ Note: The operators here define the existence of null value which
we will use to fill the table if the required conditions do not match.
3/5/2024
ECEg 4181 - By: Eyob S. 62
63.
Left Outer Join(⟕)
▪ Left Outer Join is a type of join in which all the tuples from left relation
are included and only those tuples from right relation are included
which have a common value in the common attribute on which the join
is being performed.
▪ It is denoted by: R1⟕R2 where R1 and R2 are relations.
▪ For example, consider Student(S_id, Name, Class, Age, C_type) and
Courses (C_type, C_name).
▪ Now, let’s we will perform left outer join on both the tables and see that
the new resulting table has all the tuples from the Student table but it
doesn't have that tuple from the course table whose attributes values
was not matching.
▪ Also, it fills the table with the null value for those columns whose value
is not defined.
3/5/2024
ECEg 4181 - By: Eyob S. 63
64.
3/5/2024
ECEg 4181 -By: Eyob S. 64
SELECT * FROM Student LEFT OUTER JOIN Course;
EXAMPLE
65.
Right Outer Join(⟖)
▪Right Outer Join is a type of join in which all the tuples from right
relation are included and only those tuples from left relation are
included which have a common value in the common attribute on
which the right join is being performed.
▪ It is denoted by: R1 ⟖ R2 where R1 and R2 are relations.
▪ Example: Consider Student(S_id, Name, Class, Age, C_type)
and Courses (C_type, C_name).
▪ Now, let’s perform right outer on both the tables and see that the
new resulting table has all the tuples from the Course table but it
doesn't have that tuple from the Student table whose attributes
values was not matching. Also, it fills the table with the null value
for those columns whose value is not defined.
3/5/2024
ECEg 4181 - By: Eyob S. 65
66.
3/5/2024
ECEg 4181 -By: Eyob S. 66
SELECT * FROM Student RIGHT OUTER JOIN Course;
EXAMPLE
Customers ⟕ Orders
CustomerNameOrderID
Abdisa Abebe NULL
Hana Naol 10308
Bezu Geresu 10309
Fenet Kassa 10310
3/5/2024
ECEg 4181 - By: Eyob S. 68
SELECT c.CustomerName, o.OrderID
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID
ORDER BY o.OrderID;
Customers ⟖ Orders
SELECT c.CustomerName, o.OrderID
FROM Customers c
RIGHT JOIN Orders o
ON c.CustomerID = o.CustomerID
ORDER BY o.OrderID;
CustomerName OrderID
Hana Naol 10308
Bezu Geresu 10309
Fenet Kassa 10310
NULL 10312
EXAMPLE
69.
Full Outer Join(⟗)
▪Full Outer Join is a type of join in which all the tuples from the left
and right relation which are having the same value on the
common attribute.
▪ Also, they will have all the remaining tuples which are not
common on in both the relations.
▪ It is denoted by: R1 ⟗ R2 where R1 and R2 are relations.
▪ E.g., Consider Student(S_id, Name, Class, Age, C_type) and
Courses (C_type, C_name). And, let’s perform full outer join on
both the tables. The new resulting table has all the tuples from the
Course table as well as the Student table. Also, it fills the table
with the null value for those columns whose value is not defined.
3/5/2024
ECEg 4181 - By: Eyob S. 69
70.
3/5/2024
ECEg 4181 -By: Eyob S. 70
SELECT * FROM Student FULL OUTER JOIN Course;
EXAMPLE
71.
⟗ (aka OUTERUNION)
▪ Assume two schemas expressed as:
▪ STUDENT(Name, Ssn, Department, Advisor)
▪ INSTRUCTOR(Name, Ssn, Department, Rank)
▪ STUDENT_OR_INSTRUCTOR ← STUDENT ⟗ INSTRUCTOR
▪ Tuples from the two relations are matched based on having the same
combination of values of the shared attributes—Name, Ssn, Department.
▪ The resulting relation, STUDENT_OR_INSTRUCTOR, will have the
following attributes:
▪ STUDENT_OR_INSTRUCTOR(Name, Ssn, Department, Advisor, Rank)
▪ Tuples appearing only in STUDENT will have a NULL for the Rank attribute,
whereas INSTRUCTOR tuples will have a NULL for the Advisor attribute.
3/5/2024
ECEg 4181 - By: Eyob S. 71
72.
DIVISION operator (÷)
▪The DIVISION operation, denoted by ÷, is useful
for a special kind of query that sometimes occurs in
database applications.
▪ This operator is used when we need to find tuples
in one relation (R) that are related to all tuples in
another relation (S).
▪ E.g., T ← R ÷ S
3/5/2024
ECEg 4181 - By: Eyob S. 72
73.
▪ The DIVISIONoperation can be expressed as a sequence of π, ×,
and – operations as follows:
T1 ← πB(R)
≡ T2 ← πB((S × T1) – R)
T3 ← T1 – T2
▪ In SQL, it can be achieved using nested queries with GROUP BY
and HAVING or NOT EXISTS clauses. E.g., R(a, b) and S(a),
▪ This is one alternative statement but we have many ways to do that.
SELECT b FROM R
WHERE a IN (SELECT a FROM S)
GROUP BY b
HAVING COUNT(DISTINCT a) = (SELECT COUNT(*) FROM S);
DIVISION equivalents
3/5/2024
ECEg 4181 - By: Eyob S. 73
T3 ← R ÷ S
74.
3/5/2024
ECEg 4181 -By: Eyob S. 74
▪ E.g., Retrieve the names of employees who work on
all the projects that ‘John Smith’ works on.
▪ Step 1, Get the list of project numbers that ‘John Smith’
works on.
▪ Step 2, Get a relation composed of <Pno, Essn>.
▪ Step 3, Apply the DIVISION to the two relations.
Relational Calculus
▪ Relationalcalculus is an alternative way of expressing queries.
▪ Relational algebra is a procedural language – we must explicitly provide a
sequence of operations to generate a desired output.
▪ Whereas Relational calculus is a declarative (non- procedural) language –
we specify what to retrieve, not how to retrieve it.
▪ Many equivalent algebra “implementations” possible for a given calculus
expression.
▪ There are two variants of relational calculus:
▪ Tuple Relational Calculus (TRC)
▪ Domain Relational Calculus (DRC)
▪ Both based on 1st order predicate calculus (aka 1st order logic, or FOL).
3/5/2024
ECEg 4181 - By: Eyob S. 76
77.
Tuple Relational Calculus(TRC)
▪ In TRC, we use tuple variables to represent tuples in relations.
The operations include:
▪ AND (˄)
▪ OR (˅)
▪ NOT (¬)
▪ Universal quantifier, For all x (Ɐx)
▪ Existential quantifier, There exist x (ⱻx)
▪ A query in the TRC expressed as: { t | P(t) }
▪ A set of all tuples { t } such that the predicate P(t) is true for { t }.
▪ E.g., { t | t ∈ EMPLOYEE ˄ t.Salary > 30000 }
▪ TRC uses tuple variables and specifies conditions to relate tuples.
3/5/2024
ECEg 4181 - By: Eyob S. 77
78.
“expressive power ofthe
languages is identical”
▪ If a retrieval can be specified in relational calculus, it can also be
specified in the relational algebra, and vise versa.
▪ A query language L is relationally complete if L can express any
query that can be expressed in the relational calculus.
▪ { t | t ∈ EMPLOYEE ˄ t[Salary] > 30000 } or rewritten like:
▪ { t | t ∈ EMPLOYEE ˄ t.Salary > 30000 } is similar in function to:
T ← σSalary>30000 (EMPLOYEE)
▪ SELECT * FROM EMPLOYEE WHERE Salary>30000
3/5/2024
ECEg 4181 - By: Eyob S. 78
79.
Handling Projections
▪ Retrievelast name of employees whose salary is greater than $25k.
▪ {t | ∃e ∈ EMPLOYEE (t[LName] = e[LName] ˄ e[Salary] > 25000)}
▪ The new relation t is only defined for the attributes it is explicitly
assigned (e.g., LName). This is similar in function to:
ΠLName ( σ Salary > 25000 (EMPLOYEE) )
▪ Example:
▪ Find the names of all employees whose department is in the “Watson” building.
▪ {t | ∃e ∈ EMPLOYEE (t[LName] = e[LName] ˄ ∃d ∈ DEPARTMENT (d[DName] =
e[DName] ˄ d[building]=“Watson”))}
3/5/2024
ECEg 4181 - By: Eyob S. 79
80.
JOIN in theTRC
▪ The join operation can be expressed by specifying conditions that
relate tuples from different relations.
▪ E.g., Suppose we have two relations, EMPLOYEE and DEPARTMENT, and
we want to join them based on the common attribute DeptID.
{t | ∃e ∃d (EMPLOYEE(e) ∧ DEPARTMENT(d) ∧ e.DeptID = d.DeptID
∧ t.LName = e.LName ∧ t.DName = d.DName)}
▪ e.DeptID = d.DeptID is the join condition, and
▪ t.LName = e.LName ∧ t.DName = d.DName are projections.
▪ In SQL, it can be written like:
▪ SELECT e.LName, d.DName FROM EMPLOYEE e, DEPARTMENT d
WHERE e.DeptID = d.DeptID;
3/5/2024
ECEg 4181 - By: Eyob S. 80
81.
EXAMPLES – JOIN
▪Get last name of male employees along with their department name
▪ e ← EMPLOYEE
d ← DEPARTMENT
T ← Π LName, DName (σe.Sex=‘M’ (e ⋈e.DeptID = d.DeptID d))
alternative way:
▪ { t | ∃ e ∈ EMPLOYEE ( ∃ d ∈ DEPARTMENT ( t.LName = e.LName ˄
t.DName = d.DName ˄ e.Sex = ‘M’ ˄ e.DeptID = d.DeptID ) ) }
▪ SELECT e.LName, d.DName FROM EMPLOYEE e, DEPARTMENT d
WHERE e.Sex = ‘M’ AND e.DeptID = d.DeptID;
▪ Ex. Find the names of all employees whose department is in the “Watson” building.
▪ {t | ∃e ∈ EMPLOYEE (t[LName] = e[LName] ˄ ∃d ∈ DEPARTMENT (
d[DName] = e[DName] ˄ d[building]=“Watson”))}
3/5/2024
ECEg 4181 - By: Eyob S. 81
T ← Π LName, DName (σe.Sex=‘M’ ˄ e.DeptID = d.DeptID (e x d))
82.
EXERCISES
▪ Consider twoschemas student(ID, name, age), takes(ID, c_id)
and course(c_id, c_name, faculty). Find all students who have
taken all courses offered in the ECE faculty.
▪ {t | ∃ r ∈ student (t [ID] = r [ID]) ∧ (∀ u ∈ course (u [faculty]=“ECE”
⇒ ∃ s ∈ takes (t [ID] = s [ID ] ∧ s [c_id] = u [c_id]))}
▪ Let a schema given as: section(c_id, semester, year).
where, semester attribute values are either “Fall” or “Spring.” Find
the set of all courses taught in the Fall 2024 semester, but not in
the Spring 2025 semester.
▪ {t | ∃s ∈ section (t [c_id ] = s [c_id ] ∧ s [semester] = “Fall” ∧ s
[year] = 2024 ∧ ¬ ∃u ∈ section (t [c_id ] = u [c_id ] ∧ u [semester]
= “Spring” ∧ u [year] = 2025 )}
3/5/2024
ECEg 4181 - By: Eyob S. 82
83.
Safety of TRCExpressions
▪ It is possible to write tuple calculus expressions that generate infinite
relations.
▪ For example, { t | ¬ t ∈ r } results in an infinite relation if the domain of any
attribute of relation r is infinite
▪ To guard against the problem, we restrict the set of allowable expressions
to safe expressions.
▪ An expression { t | P ( t ) } in the tuple relational calculus is safe if every
component of t appears in one of the relations, tuples, or constants that
appear in P.
• NOTE: this is more than just a syntax condition.
▪ E.g. { t | t [A] = 5 ∨ true } is not safe --- it defines an infinite set with attribute
values that do not appear in any relation or tuples or constants in P.
3/5/2024
ECEg 4181 - By: Eyob S. 83
84.
Domain Relational Calculus(DRC)
▪ The DRC is equivalent in power to the tuple relational calculus.
▪ In DRC, we use domain variables to represent values in tuples and
specify conditions to relate those values.
▪ Each query is an expression of the form:
{ < x1, x2, …, xn > | P (x1, x2, …, xn)}
Where: x1, x2, …, xn represent domain variables, and P represents a formula
similar to that of the predicate calculus
▪ E.g., Find the first name, last name for employees whose salary is
greater than $30,000.
{ <f, l> | ∃ s ( <f, l, s> ∈ EMPLOYEE ˄ s > 30000 ) }
3/5/2024
ECEg 4181 - By: Eyob S. 84
85.
EXAMPLES
▪ E.g.1, Retrievelast name of employees whose salary is greater than $30,000.
{ < l > | ∃ l, s ( < f, l, s > ∈ EMPLOYEE ˄ s > 30000 )}
▪ As shown above, it's not mandatory to list all attributes of a relation in the predicate part
of a Domain Relational Calculus (DRC) query. Alternatively, it can be re-expressed as:
{ < LName > | ∃e ∈ EMPLOYEE ˄ e.Salary > 30000 }
▪ E.g.2, Suppose we have two relations: EMPLOYEE(ID, Name, DeptID) and
DEPARTMENT(DeptID, DeptName). Using the relations EMPLOYEE and
DEPARTMENT, the join operation can be expressed by specifying conditions
that relate domain variables from different relations.
{<Name, DeptName> | ∃ DeptID (<Name, DeptID> ∈ EMPLOYEE ∧
<DeptID, DeptName> ∈ DEPARTMENT)}
3/5/2024
ECEg 4181 - By: Eyob S. 85
86.
EXERCISES
▪ Let schemasstudent(ID, name, age), takes(ID, c_id) and course(c_id,
c_name, faculty). Find the ID, name and age of all students who have taken
all courses offered in the ECE faculty.
{ < i, n, a > | ∃ i, n, a (< i, n, a > ∈ student ∧ (∀ ci, fa (< ci, cn, fa > ∈ course ∧ fa=“ECE”
⇒ ∃ i, ci ∈ takes))}
▪ Suppose a schema given as: section(c_id, semester, year) where, semester
attribute values are either “Fall” or “Spring.” Find the set of all courses taught
in the Fall 2024 semester, or in the Spring 2025 semester, or both.
{<c> | ∃ s, y (<c, s, y> ∈ section ∧ ( (s = “Fall” ∧ y = 2024 ) v (s = “Spring” ∧ y = 2025)))}
▪ Find the set of all courses taught in the Fall 2024 semester, and in the Spring
2025 semester.
{<c> | ∃ s, y ( <c, s, y> ∈ section ∧ ( s = “Fall” ∧ y = 2024 ) ∧ ( s = “Spring” ∧ y = 2025 ) ) }
3/5/2024
ECEg 4181 - By: Eyob S. 86
87.
Safety of DRCExpressions
▪ All values that appear in tuples of the expression are values from
domain (P) (that is, the values appear either in P or in a tuple of a
relation mentioned in P).
▪ For every “there exists” sub-formula of the form ∃ x (P1(x )), the
sub-formula is true if and only if there is a value of x in domain
(P1) such that P1(x ) is true.
▪ For every “for all” sub-formula of the form ∀x (P1 (x )), the sub-
formula is true if and only if P1(x ) is true for all values x from
domain (P1).
3/5/2024
ECEg 4181 - By: Eyob S. 87
88.
NoSQL (Not OnlySQL)
▪ NoSQL database is a type of database management system designed
for handling large-scale, flexible, and schema-free data storage.
▪ NoSQL databases do not rely on fixed table structures or SQL queries.
Instead, they offer dynamic data models that are optimized for scalability,
speed, and real-time processing.
▪ No predefined table structures, making it easy to store and update data
without needing schema modifications.
▪ It is horizontally scalable; therefore, it can distribute data across multiple
servers to handle large-scale workloads efficiently.
▪ It is well optimized for fast reads and writes in large applications.
▪ Includes key-value stores, document stores, column-family stores, and
graph databases, each suited for different use cases.
3/5/2024
ECEg 4181 - By: Eyob S. 88
89.
Key-value
Store
▪ Simple key-valuepairs for caching and session management.
The key for each pair is unique.
▪ Primary operations include: insert(key,value), delete(key), update(key,value),
lookup(key), reverse lookup(key), …, etc.
▪ They’re atomic, schema-less, highly flexible, and used for unstructured data.
▪ Suitable for high performance or low latency applications, and efficient data
retrieval.
▪ Not suitable for complex data structures, and data-ware house.
▪ E.g., Redis (Remote Dictionary Server), Memcached, ScyllaDB
3/5/2024
ECEg 4181 - By: Eyob S. 89
90.
Document
Store
▪ Its basicdata models are:
▪ The general notion of a document – words, phrases, sentences, paragraphs,
sections, subsections, footnotes, etc.
▪ Metadata – title, author, date, embedded tags, etc.
▪ unique keys (or identifiers) – auto generated or user defined keys
▪ Flexible schema
– subcomponent structure may be nested, and vary from document-to-document.
▪ Formats vary greatly
– Portable Document Format (PDF), eXtensible Markup Language (XML),
– JavaScript Object Notation (JSON), Binary JSON (BSON),
– plain text, various binary, scanned image.
▪ Not suitable for referential integrity, complex relationships and joins.
▪ E.g., MongoDB, CouchDB, DynamoDB, etc.
3/5/2024
ECEg 4181 - By: Eyob S. 90
91.
Column-Family Stores
3/5/2024
ECEg 4181- By: Eyob S. 91
▪ Database is a collection of key/value pairs. Key consists of 3 parts which are
a row key, a column key, and a time-stamp (i.e., the version). The column key
consists of two parts – a column family, and a qualifier.
▪ One “row” in a wide-column NoSQL database table is the same as many rows
in several relations/tables in a relational database.
▪ E.g., Apache Cassandra, Bigtable(Google cloud), Hbase(Hadoop framework), etc.
▪ Denormalized database, and optimized for
▪ Big-data and analytical processing.
▪ Data-ware house.
▪ Flexible schema
▪ the set of columns is not fixed, and
▪ may differ from row-to-row.
▪ Not suitable for
▪ random filtering, aggregation, or joins.
92.
Graph Store
▪ Thebasic data model:
▪ Directed graphs
▪ Nodes & edges, properties, i.e., “labels”
▪ Handles complex data structures, knowledge management and analytics.
▪ Hard for horizontally scaling, graph databases require careful partitioning to
avoid breaking connections.
▪ Graph databases handle scaling using sharding and partitioning techniques,
and distributed graph systems.
▪ Some modern graph databases (like Neo4j, Amazon Neptune and ArangoDB) use
intelligent sharding and replication techniques to distribute data across multiple nodes.
▪ Databases like JanusGraph and TigerGraph support horizontal scaling by spreading
graph data across multiple machines while maintaining relationships.
3/5/2024
ECEg 4181 - By: Eyob S. 92
93.
ACID vs. BASE
▪SQL database systems support ACID requirements:
▪ Atomicity, Consistency, Isolation, Durability
▪ In a distributed web applications, the focus shifts to:
▪ Consistency, Availability, Partition tolerance (CAP)
▪ CAP theorem - At most two of the above can be enforced at any
given time.
▪ Reducing consistency, at least temporarily, maintains the other
two. Thus, distributed NoSQL systems used to support BASE:
▪ Basic Availability
▪ Soft state
▪ Eventual consistency
3/5/2024
ECEg 4181 - By: Eyob S. 93
94.
Summary
▪ Most modernNoSQL systems diverge from the relational model or standard RDBMS functionality:
3/5/2024
ECEg 4181 - By: Eyob S. 94
Relational (SQL) Non-relational (NoSQL)
data models relation
tuple
attribute
collection / graph
document
key/value
query models relational algebra
relational calculus
graph traversal
text search
map/reduce
implementations vertically scalable
(rigid schemas)
ACID compliance
flexible schemas
(schema-less)
BASE compliance
use cases interconnected data
transactional support
consistency and reliability
extremely large traffic of requests
horizontal scaling/ data partitioning
low latency
• NoSQL databases are widely used in applications requiring scalability, such as social media
platforms, e-commerce, and real-time analytics.