Professional Documents
Culture Documents
Lec 2.4
Lec 2.4
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
Module 09
Additional Basic
Operations
Cartesian Product
Rename AS
Operation
String Values
Order By Clause
Select Top / Fetch
Clause
Where Clause
Predicates
Duplicates
Module Summary
Module 09
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
Module 09
Module Summary
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
Module 09
Module Summary
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
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
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
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
Module 09
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
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
Module 09
Module Summary
◦ finding string length, extracting substrings, etc.
Module 09
Module Summary
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
Module 09
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
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
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 09
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”.