0% found this document useful (0 votes)
16 views26 pages

Chapter 6 Query Languges

This document discusses relational query languages, which facilitate data manipulation and retrieval from databases, emphasizing the distinction between query languages and programming languages. It covers formal relational query languages such as relational algebra and calculus, detailing their operations including selection, projection, and joins. The document also provides examples and explanations of these operations, highlighting their significance in understanding SQL and query processing.

Uploaded by

Efeson Uliso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views26 pages

Chapter 6 Query Languges

This document discusses relational query languages, which facilitate data manipulation and retrieval from databases, emphasizing the distinction between query languages and programming languages. It covers formal relational query languages such as relational algebra and calculus, detailing their operations including selection, projection, and joins. The document also provides examples and explanations of these operations, highlighting their significance in understanding SQL and query processing.

Uploaded by

Efeson Uliso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Jimma University

JIT Fundamentals of
Faculty of computing Database systems
&Informatics

Chapter Six
Query Languages
Relational Query Languages

• Query languages: Allow manipulation and


retrieval of data from a database.
• Relational model supports simple, powerful
QLs:
• Strong formal foundation based on logic.
• Allows for much optimization.

• Query Languages != programming


languages!
2
• QLs not expected to be “Turing complete”.
Formal Relational Query Languages
• Two mathematical Query Languages form the
basis for “real” languages (e.g. SQL), and for
implementation:
• Relational Algebra: More operational, very
useful for representing execution plans.
• Relational Calculus: Lets users describe
what they want, rather than how to compute
it. (Non-operational, declarative.)

 Understanding Algebra & Calculus is key to


understanding SQL, query processing!
Is this the Algebra you know?
• Algebra -> operators and atomic operands
• Expressions -> applying operators to atomic
operands and/or other expressions

• Algebra of arithmetic: operands are variables and


constants, and operators are the usual arithmetic
operators
E.g., (x+y)*2 or ((x+7)/(y-3)) + x

• Relational algebra: operands are variables that


stand for relations and relations (sets of tuples),
and operations include union, intersection,
selection, projection, Cartesian product, etc
– E.g., (π c-ownerChecking-account) ∩ (π s-
ownerSavings-account)
What is a query?

• A query is applied to relation instances, and the


result of
a query is also a relation instance. (view, query)
– Schemas of input and output fixed, but instances
not.

• Operators refer to relation attributes by position


or name:
– E.g., Account(number, owner, balance, type)

• – Positional notation easier for formal definitions,


Relational Algebra Operations

The usual set operations: union, intersection,


difference
•Operations that remove parts of relations:
selection, projection
•Operations that combine tuples from two
relations:
Cartesian product, join
•Since each operation returns a relation,
operations can be composed!
Relational Algebra Oberations
• Basic operations:

– 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 reln. 1, but not in
reln. 2.
– Union (  ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential,
but (very!) useful.
Selection: Example
Another selection
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!
(Why??)
• Note: real systems typically don’t do duplicate
elimination unless the user explicitly asks for it.
(Why not?)
Example of Projection
Projection removes duplicates
Union, Intersection, Set-Difference

• All of these operations take two input relations, which

must be union-compatible:
• Same number of fields.

• `Corresponding’ fields have the same type.

• What is the schema of result?


Example Instances

sid sname rating age


S1 22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0

sid sname rating age


S2 28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Union, Intersection, Set-Difference
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
S1 S2

sid sname rating age


sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1 S2 S1 S2
Combining Tuples of Two Relations

• Cross product (Cartesian product)

• Joins

1
6
Cartesian-Product Operation – Example
 Relations r, s:
A B C D E

 1  10 a
 10 a
 2  20 b
r  10 b
s
 r x s:
A B C D E
 1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b
Cross Product Example
Cross Product

How to resolve????
Renaming operator: 

Rename whole relation: Teacher X secondteacher(Teacher) 


Teacher.t-num, Teacher.t-name, secondteacher.t-num, secondteacher.t-name
OR rename attribute before combining:

Teacher X secondteacher(t-num2, t-name2)(Teacher)


t-num, t-name, t-num2, t-name2
OR rename after combining

JOIN
• General Syntax:
R⨝S
• R and S are the two relations (tables).
•The JOIN operation combines the data based on a specified condition
(e.g., equality, comparison).
• Types of JOINs:
1. Theta Join (θ-Join)
2. Equi Join
3. Natural Join
4. Outer Joins (Left, Right, Full)
5. Cross Join (Cartesian Product)
Join.EquiJoin: Example
Join : Example
Condition Join
Condition Join
Equi and Natural Join
End!!!

You might also like