You are on page 1of 20

Module 09

Partha Pratim
Das

Objectives &
Outline Database Management Systems
Additional Basic
Operations Module 09: Introduction to SQL/2
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Partha Pratim Das
Clause
Where Clause
Predicates
Department of Computer Science and Engineering
Duplicates
Indian Institute of Technology, Kharagpur
Module Summary
ppd@cse.iitkgp.ac.in

Database Management Systems Partha Pratim Das 09.1


Module Recap PPD

Module 09

Partha Pratim • Introduced relational query language


Das
• Familiarized with data definition and basic query structure
Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.2


Module Objectives PPD

Module 09

Partha Pratim • To complete the understanding of basic query structure


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.3


Module Outline PPD

Module 09

Partha Pratim • Additional Basic Operations


Das
◦ Cartesian Product
Objectives &
Outline ◦ Rename AS Operation
Additional Basic ◦ String Values
Operations
Cartesian Product
◦ Order By
Rename AS
Operation
◦ Select Top / Fetch
String Values
◦ Where Clause Predicate
Order By Clause
Select Top / Fetch ◦ Duplicates
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.4


Additional Basic Operations PPD

Module 09

Partha Pratim
Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates Additional Basic Operations
Module Summary

Database Management Systems Partha Pratim Das 09.5


Cartesian Product

Module 09

Partha Pratim • Find the Cartesian product instructor X teaches


Das
select *
Objectives &
Outline
from instructor , teaches
Additional Basic ◦ generates every possible instructor-teaches pair, with all attributes from both
Operations
Cartesian Product
relations
Rename AS
Operation
◦ For common attributes (for example, ID), the attributes in the resulting table are
String Values
renamed using the relation name (for example, instructor.ID)
Order By Clause
Select Top / Fetch
Clause
• Cartesian product not very useful directly, but useful combined with where-clause
Where Clause
Predicates
condition (selection operation in relational algebra)
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.6


Cartesian Product

Module 09

Partha Pratim
Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.7


Examples PPD

Module 09 • Find the names of all instructors who have taught some course and the course id
Partha Pratim
Das
select name, course id
from instructor , teaches
Objectives &
Outline where instructor .ID = teaches.ID
Additional Basic
Operations
◦ Equi-Join, Natural Join
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.8


Examples PPD

Module 09

Partha Pratim • Find the names of all instructors in the Art department who have taught some course
Das
and the course id
Objectives &
Outline
select name, course id
Additional Basic
from instructor , teaches
Operations
Cartesian Product
where instructor .ID = teaches.ID and instructor.dept name = ’Art’
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.9


Rename AS Operation

Module 09

Partha Pratim • The SQL allows renaming relations and attributes using the as clause:
Das
old name as new name
Objectives &
Outline • Find the names of all instructors who have a higher salary than some instructor in
Additional Basic ’Comp. Sci’.
Operations
Cartesian Product select distinct T.name
Rename AS
Operation from instructor as T, instructor as S,
String Values
Order By Clause
where T .salary > S.salary and S.dept name = ’Comp. Sci’
Select Top / Fetch
Clause • Keyword as is optional and may be omitted
Where Clause
Predicates instructor as T ≡ instructor T
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.10


Cartesian Product Example

Module 09

Partha Pratim • Relation emp super


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
• Find the supervisor of “Bob”
Select Top / Fetch
Clause • Find the supervisor of the supervisor of “Bob”
Where Clause
Predicates • Find ALL the supervisors (direct and indirect) of “Bob”
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.11


String Operations

Module 09

Partha Pratim • SQL includes a string-matching operator for comparisons on character strings. The
Das
operator like uses patterns that are described using two special characters:
Objectives &
Outline ◦ percent ( % ). The % character matches any substring
Additional Basic ◦ underscore ( ). The character matches any character
Operations
Cartesian Product • Find the names of all instructors whose name includes the substring “dar”
Rename AS
Operation select name
String Values
Order By Clause
from instructor
Select Top / Fetch
Clause
where name like ’%dar %’
Where Clause
Predicates • Match the string “100%”
Duplicates
like ’100%’ escape ’\’
Module Summary
• in that above we use backslash (\) as the escape character

Database Management Systems Partha Pratim Das 09.12


String Operations (2)

Module 09

Partha Pratim • Patterns are case sensitive


Das
• Pattern matching examples:
Objectives &
Outline ◦ ’Intro%’ matches any string beginning with “Intro”
Additional Basic
Operations
◦ ’%Comp%’ matches any string containing “Comp” as a substring
Cartesian Product ◦ ’ ’ matches any string of exactly three characters
Rename AS
Operation ◦ ’ %’ matches any string of at least three characters
String Values
Order By Clause • SQL supports a variety of string operations such as
Select Top / Fetch
Clause
Where Clause
◦ concatenation (using “k”)
Predicates
◦ converting from upper to lower case (and vice versa)
Duplicates

Module Summary
◦ finding string length, extracting substrings, etc.

Database Management Systems Partha Pratim Das 09.13


Ordering the Display of Tuples

Module 09

Partha Pratim • List in alphabetic order the names of all instructors


Das
select distinct name
Objectives &
Outline
from instructor
Additional Basic
order by name
Operations
Cartesian Product • We may specify desc for descending order or asc for ascending order, for each
Rename AS
Operation attribute; ascending order is the default.
String Values
Order By Clause
◦ Example: order by name desc
Select Top / Fetch
Clause • Can sort on multiple attributes
Where Clause
Predicates
◦ Example: order by dept name, name
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.14


Selecting Number of Tuples in Output

Module 09

Partha Pratim • The Select Top clause is used to specify the number of records to return
Das
• The Select Top clause is useful on large tables with thousands of records. Returning a
Objectives &
Outline large number of records can impact performance
Additional Basic select top 10 distinct name
Operations
Cartesian Product from instructor
Rename AS
Operation • Not all database systems support the SELECT TOP clause.
String Values
Order By Clause ◦ SQL Server & MS Access support select top
Select Top / Fetch
Clause ◦ MySQL supports the limit clause
Where Clause
Predicates ◦ Oracle uses fetch first n rows only and rownum
Duplicates

Module Summary
select distinct name
from instructor
order by name
fetch first 10 rows only

Database Management Systems Partha Pratim Das 09.15


Where Clause Predicates

Module 09

Partha Pratim • SQL includes a between comparison operator


Das
• Example: Find the names of all instructors with salary between $90, 000 and $100, 000
Objectives &
Outline (that is, ≥ $90, 000 and ≤ $100, 000)
Additional Basic select name
Operations
Cartesian Product from instructor
Rename AS
Operation where salary between 90000 and 100000
String Values
Order By Clause • Tuple comparison
Select Top / Fetch
Clause select name, course id
Where Clause
Predicates from instructor , teaches
Duplicates
where (instructor.ID, dept name) = (teaches.ID, ’Biology’);
Module Summary

Database Management Systems Partha Pratim Das 09.16


In Operator

Module 09

Partha Pratim • The in operator allows you to specify multiple values in a where clause
Das
• The in operator is a shorthand for multiple or conditions
Objectives &
Outline select name
Additional Basic from instructor
Operations
Cartesian Product where dept name in (’Comp. Sci.’, ’Biology’)
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates

Module Summary

Database Management Systems Partha Pratim Das 09.17


Duplicates

Module 09

Partha Pratim • In relations with duplicates, SQL can define how many copies of tuples appear in the
Das
result
Objectives &
Outline • Multiset versions of some of the relational algebra operators – given multiset relations
Additional Basic r1 and r2 :
Operations
Cartesian Product a) σθ (r1 ): If there are c1 copies of tuple t1 in r1 , and t1 satisfies selections σθ , then
Rename AS
Operation there are c1 copies of t1 in σθ (r1 )
String Values
Order By Clause
b) ΠA (r ): For each copy of tuple t1 in r1 , there is a copy of tuple ΠA (t1 ) in ΠA (r1 )
Select Top / Fetch
Clause
where ΠA (t1 ) denotes the projection of the single tuple t1
Where Clause
Predicates
c) r1 x r2 : If there are c1 copies of tuple t1 in r1 and c2 copies of tuple t2 in r2 , there
Duplicates are c1 x c2 copies of the tuple t1 .t2 in r1 x r2
Module Summary

Database Management Systems Partha Pratim Das 09.18


Duplicates (2)

Module 09

Partha Pratim • Example: Suppose multiset relations r1 (A, B) and r2 (C ) are as follows:
Das
r1 = {(1, a)(2, a)} r 2 = {(2), (3), (3)}
Objectives &
Outline • Then ΠB (r1 ) would be {(a), (a)}, while ΠB (r1 ) x r2 would be
Additional Basic {(a, 2), (a, 2), (a, 3), (a, 3), (a, 3), (a, 3)}
Operations
Cartesian Product • SQL duplicate semantics:
Rename AS
Operation select A1 , A2 , . . . , An
String Values
Order By Clause from r1 , r2 , . . . , rm
Select Top / Fetch
Clause where P
Where Clause
Predicates is equivalent to the multiset version of the expression:
Duplicates

Module Summary ΠA1 ,A2 ,...,An (σP (r1 × r2 × . . . × rm ))

Database Management Systems Partha Pratim Das 09.19


Module Summary PPD

Module 09

Partha Pratim • Completed the understanding of basic query structure


Das

Objectives &
Outline

Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Duplicates permission of the authors.
Module Summary Edited and new slides are marked with “PPD”.

Database Management Systems Partha Pratim Das 09.20

You might also like