Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

QUERY

LANGUAGES
■ Relational database systems are equipped with a query language that can assist its users to
query the database instances.
■ Query languages: Allow manipulation and retrieval of data from a database.
■ Relational model supports simple and powerful QLs:
– Strong formal foundation based on logic.
– Allows for much optimization
■ Query Languages != programming languages!
– QLs not expected to be “Turing complete”( no complete instruction set)
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
■ There are two kinds of mathematical query languages that form the basis for the “real”
languages (e.g. SQL) and for implementation
– Relational Algebra: More operational(procedural), very useful for representing
execution plans.
– Relational Calculus: Lets users describe what they want, rather than how to
compute it. (Non-operational, declarative.)
SQL- Structured Query Language
■ SQL is a standard language for storing, manipulating
and retrieving data in databases.
■ You use SQL in: MySQL, SQL Server, MS Access,
Oracle, Sybase, Informix, Postgres, and other database
systems.
■ SQL is an ANSI (American National Standards
Institute) standard
What Can SQL do?
■ retrieve data from a database
■ insert records in a database
■ update records in a database
■ delete records from a database
■ create new databases
■ create new tables in a database
■ create stored procedures in a database
■ create views in a database
■ set permissions on tables, procedures, and views
RELATIONAL
ALGEBRA
What is Relational Algebra (RA)
■ An algebra is a formal structure consisting of sets and operations
on those sets.
■ Not used as a query language in actual DBMSs. (SQL instead.)
■ Relational algebra is a procedural query language,
■ Operands of this algebra are relations (input and output)
– relations are sets of tuples
■ It uses operators to perform queries. An operator can be either
unary or binary.
■ Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
Basic Operators

■ Selection () Selects a subset of rows from relation.


■ Projection ( ) Deletes unwanted columns from
relation.
■ Cross-product ( × ) Allows us to combine two
relations.
■ Set-difference ( - ) Tuples in R1, but not in R2.
■ Union ( ) Tuples in R1 and in R2.
Additional operations:

■ Intersection, ; r ∩ s = r – (r – s)
■ join, (natural, equi-join, theta join, semi-join)
■ division, ÷
■ renaming: Not essential, but (very!) useful.
Selection ()

■ Selects rows that satisfy selection condition.


■ Schema of result identical to schema of (only) input relation.
■ Result relation can be the input for another relational algebra operation!
(Operator composition.)
■ SQL
SELECT * FROM table_name WHERE condition;
■ RA : σc(R)
The condition c can be =, <, , >, , <>
σc(R) = { t | t ∈R s.t. t satisfies C, i.e., C(t)}
Retrieve pet details whose rating is greater than 8 from
S2

■ SQL
SELECT * FROM S2 WHERE rating >8
■ RA
σ rating>8(S2)
Projection ( )

■ Deletes attributes that are not in projection list.


■ Schema of result contains exactly the fields in the projection list, with the
same names that they had in the (only) input relation.
■ Projection operator has to eliminate duplicates!
■ Notation:
SQL
SELECT field1, field2…, fieldn FROM table_name;
RA
A1,…,An (R)
πA(R) = { t[A] | t ∈ R}
List all pet names and their ratings from S2
■ SQL
SELECT Sname, rating FROM S2;
■ RA
sname, rating (S2)
List the ages of puppies from table S2
a) Using SQL script
b) Using Relational Algebra

Using SQL script Using Relational Algebra


SELECT age FROM S2; (S2)
age
Combining σ and
■ Project a list of attributes based on a condition
■ SQL
SELECT field1, field2, …, fieldn
FROM table_name
WHERE condition;
■ RA

A1,…,An (σc(R))
■ Firstly select rows based on a condition from R to form a new relation R1 (intermediate results)
– R1σc(R)
■ Secondly project the selected attributes from R1 to form a new relation R2
– R2 A1,…,An (R1)
Retrieve all pet names and their ratings
whose rating is greater than 8

■ SQL ■ RA

SELECT sname, rating sname, rating (σrating>8(S2))


OR
FROM S2
R1 σrating>8(S2)
WHERE rating>8;
R2 sname, rating (R1)
Cross-product or Cartesian Product (× )
■ Each row of R1 is paired with each row of R2.
■ Notation: R1  R2
■ r x s = {<t q> | t ∈ r and q ∈ s}
■ Result schema consists of every field from R1 and R2, with
field names ‘inherited’ if possible.
– Conflict: Both R1 and R2 could have the same field name
■ The degree of R1  R2 = Deg(R1) +Deg(R2)
Given two relations R and S find the
Cartesian product R×S
What is the Cartesian product of S1  R1
S1 R1
sid sname rating age sid bid day
22 Dustin 7 45.0 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96

S1.sid S1.sname S1.rating R1.age R1.sid R1.bid R1.day


22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 55.5 58 103 11/12/96
22 Dustin 7 35.0 22 101 10/10/96
31 Lubber 8 45.0 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
31 Lubber 8 35.0 22 101 10/10/96
58 Rusty 10 45.0 22 101 10/10/96
58 Rusty 10 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
The Join,
■ The most expensive, most useful and most common operation.
■ Join uses relatedness (foreign keys) to combine two tables into one.
■ Join is usually needed when a database query involves knowing
something found in one table but wanting to know something found in a
different table.
■ Join is useful because both Select and Project work on only one table at a
time.
■ Two main versions of the join:
– "natural" join: takes attribute names into account;
– "theta" join: involves a predicate
■ Join operations are denoted by the symbol ⋈.
Natural Join
■ Match only those tuples from R and S that agree in whatever
attributes are common to the schemas of R and S
■ Equijoin on all common fields.
■ Notation: R1 ⋈ R2
■ Meaning: R1 ⋈ R2 = A(C(R1  R2))
■ Where:
– The selection C checks for the equality of all common
attributes
– The projection eliminates the duplicate common attributes
Find the natural Join of S1 and R1.
S1  R1
S1.sid S1.sname S1.rating R1.age R1.sid R1.bid R1.day
22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 55.5 58 103 11/12/96
22 Dustin 7 35.0 22 101 10/10/96
31 Lubber 8 45.0 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
31 Lubber 8 35.0 22 101 10/10/96
58 Rusty 10 45.0 22 101 10/10/96
58 Rusty 10 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
Find the natural Join of S1 and R1.
■ Common attributes S1.sid and R1.sid
■ S1.sid=R1.sid (S1  R1)

S1.sid S1.sname S1.rating R1.age R1.sid R1.bid R1.day


22 Dustin 7 45.0 22 101 10/10/96
22 Dustin 7 55.5 58 103 11/12/96
22 Dustin 7 35.0 22 101 10/10/96
31 Lubber 8 45.0 22 101 10/10/96
31 Lubber 8 55.5 58 103 11/12/96
31 Lubber 8 35.0 22 101 10/10/96
58 Rusty 10 45.0 22 101 10/10/96
58 Rusty 10 55.5 58 103 11/12/96
58 Rusty 10 35.0 22 101 10/10/96
Find the natural Join of S1 and R1.
■ Common attributes S1.sid and R1.sid
■ (
S1.sid, S1.sname, S1.rating, R1.age, R1.bid, R1.day S1.sid=R1.sid (S1  R1))

S1.sid S1.sname S1.rating R1.age R1.sid R1.bid R1.day

22 Dustin 7 45.0 22 101 10/10/96

22 Dustin 7 35.0 22 101 10/10/96

58 Rusty 10 55.5 58 103 11/12/96


Find the natural Join of S1 and R1.
■ Common attributes S1.sid and R1.sid
■ (
S1.sid, S1.sname, S1.rating, R1.age, R1.bid, R1.day S1.sid=R1.sid (S1  R1))

S1.sid S1.sname S1.rating R1.age R1.bid R1.day

22 Dustin 7 45.0 101 10/10/96

22 Dustin 7 35.0 101 10/10/96

58 Rusty 10 55.5 103 11/12/96


Find the natural join of R and S.
R = R.A, R.B, S.C(R.B=S.B(R1  R2))
R S
Find r1
Theta Join R1 ⋈ R2
■ A natural join that involves a predicate
– A(C(R1  R2)))= C (R1 ⋈ R2)= R1 ⋈
R2
■ Where is a predicate or condition
Given two relations r1 and r2. Use SQL scripts and relational algebra to answer
the following queries.
a) List employees in the ‘sales’ department
b) Find the head of the ‘production’ department
c) Who is the head of the employee named ‘Smith’?
Given two relations r1 and r2. Use SQL
scripts and relational algebra to answer the
following queries.
a) List employees in the ‘sales’
department
SQL
SELECT Employee Employee
FROM r1
WHERE Department = ‘Sales’; Smith

RA
Employee (σDepartment= ‘sales’(r1))
Given two relations r1 and r2. Use SQL
scripts and relational algebra to answer
the following queries.
b) Find the head of the ‘production’
department
SQL
SELECT Head
FROM r2
WHERE Department =
‘production’;
Head
RA
Mori
HeadσDepartment= ‘production’(r2))
Given two relations r1 and r2. Use SQL
scripts and relational algebra to answer the
following queries.
c) Who is the head of the employee named
‘Smith’
SQL
SELECT head
FROM r1
INNER JOIN r2
Head
ON r1.Department = r2. Department
where Employee = “Smith"; Brown
RA
Head (r1 ⋈ r2)) OR
Head (r1 (Employee=’Smith’) r2)
JOIN
MySQL
Different Types of SQL JOINs

■ (INNER) JOIN
■ LEFT (OUTER) JOIN
■ RIGHT (OUTER) JOIN
■ FULL (OUTER) JOIN
INNER JOIN

■ Returns records (rows) that have matching values in both


tables
■ Alias Join, or natural Join
■ SQL Syntax- JOIN
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
■ Relational Algebra
Table 1
Exercise Practical
■ Create a database with the following tables S1 and R1
Create database practical;

S1
sid sname rating
22 Dustin 7
31 Lubber 8
58 Rusty 10
R1
age sid bid day

45.0 22 101 10/10/2021

55.5 58 103 11/12/2021

35.0 22 101 10/10/2021


■ Use MySQL to Perform a natural join of S1 and R1
List Pet Names whose age is more than 40.0
A Writers database contains details about the Author, Book and the publishers. The details are
given below in the database schema
■ Author(Author_Number, Author_Name, Email_address, City)
■ Publisher(Publisher_Name, City )
■ Book(Book_Number, Title, Author_Number, Publisher_Name)
 
Assuming that a book can only be written by one author and an author can write as many
books. A publisher can publish many books. Use the database schema to write Relational
Algebra Queries to:
 
1. List all author details that are found in the city of Gweru. [2]
2. List all names of publishers found in Harare. [2]
3. What are the titles of books written by Author called “M. Moyo” who lives in the city of
Karoi? [4]
Consider the following relational schema for XYZ college:
■ School(School)
■ Department(Dept, School)
■ Lecturer(LID, Lname, Dept)
■ Course(Cnum, Cname, LID)
Use Relational Algebra to answer the following queries:
1. Display all the names of lecturers in the ICT department. [2]
2. List all departments in the school of Engineering. [3]
3. Display lecturer names from the Engineering department. [4]
4. Find the name of the lecturer who teaches a course with Cnum = “C101”. [4]
5. List all course names taught by lecturers from the school of Engineering. [6]

You might also like