L3 sql
L3 sql
Arnab Bhattacharya
[email protected]
drop table: drop table r deletes the table from the database
Must satisfy other constraints already applied
Example
drop t a b l e f a c u l t y ;
drop table: drop table r deletes the table from the database
Must satisfy other constraints already applied
Example
drop t a b l e f a c u l t y ;
Case-insensitive
select * chooses all attributes
To eliminate duplicates, use select distinct . . .
Otherwise, by default is select all . . .
Case-insensitive
select * chooses all attributes
To eliminate duplicates, use select distinct . . .
Otherwise, by default is select all . . .
Can contain arithmetic expressions
s e l e c t coursecode , y r − 1959
from o f f e r i n g
where y r = 2018;
Five operations that work on multisets: avg, min, max, sum, count
Example: Find the average cpi of students
Five operations that work on multisets: avg, min, max, sum, count
Example: Find the average cpi of students
s e l e c t avg ( c p i )
from s t u d e n t ;
Five operations that work on multisets: avg, min, max, sum, count
Example: Find the average cpi of students
s e l e c t avg ( c p i )
from s t u d e n t ;
Example: Delete all students whose CPI is less than the average
CPI
Example: Delete all students whose CPI is less than the average
CPI
d e l e t e from s t u d e n t
where c p i < (
s e l e c t avg ( c p i )
from s t u d e n t ) ;
Example: Delete all students whose CPI is less than the average
CPI
d e l e t e from s t u d e n t
where c p i < (
s e l e c t avg ( c p i )
from s t u d e n t ) ;
Example: Delete all students whose CPI is less than the average
CPI
d e l e t e from s t u d e n t
where c p i < (
s e l e c t avg ( c p i )
from s t u d e n t ) ;
If where is empty, all tuples are updated with the new value
Example: Increase CPI of all students by 5%
If where is empty, all tuples are updated with the new value
Example: Increase CPI of all students by 5%
update s t u d e n t
set c p i = c p i * 1 . 0 5 ;
Join types: inner join, left (outer) join, right (outer) join, full (outer)
join
Join conditions: natural, on ⟨ predicate ⟩, using (⟨ attribute list ⟩)
Examples
s t u d e n t inner j o i n program on s t u d e n t . r o l l = program . r o l l ;
Join types: inner join, left (outer) join, right (outer) join, full (outer)
join
Join conditions: natural, on ⟨ predicate ⟩, using (⟨ attribute list ⟩)
Examples
s t u d e n t inner j o i n program on s t u d e n t . r o l l = program . r o l l ;
s t u d e n t n a t u r a l l e f t j o i n program ;
Join types: inner join, left (outer) join, right (outer) join, full (outer)
join
Join conditions: natural, on ⟨ predicate ⟩, using (⟨ attribute list ⟩)
Examples
s t u d e n t inner j o i n program on s t u d e n t . r o l l = program . r o l l ;
s t u d e n t n a t u r a l l e f t j o i n program ;
s t u d e n t r i g h t outer j o i n program using ( r o l l ) ;
Join types: inner join, left (outer) join, right (outer) join, full (outer)
join
Join conditions: natural, on ⟨ predicate ⟩, using (⟨ attribute list ⟩)
Examples
s t u d e n t inner j o i n program on s t u d e n t . r o l l = program . r o l l ;
s t u d e n t n a t u r a l l e f t j o i n program ;
s t u d e n t r i g h t outer j o i n program using ( r o l l ) ;
Join types: inner join, left (outer) join, right (outer) join, full (outer)
join
Join conditions: natural, on ⟨ predicate ⟩, using (⟨ attribute list ⟩)
Examples
s t u d e n t inner j o i n program on s t u d e n t . r o l l = program . r o l l ;
s t u d e n t n a t u r a l l e f t j o i n program ;
s t u d e n t r i g h t outer j o i n program using ( r o l l ) ;
is expanded at runtime to
is expanded at runtime to
select *
from
( s t u d e n t n a t u r a l j o i n program )
where dept = ‘ ‘CSE ’ ’ ;
is expanded at runtime to
select *
from
( s t u d e n t n a t u r a l j o i n program )
where dept = ‘ ‘CSE ’ ’ ;
is expanded at runtime to
select *
from
( s t u d e n t n a t u r a l j o i n program )
where dept = ‘ ‘CSE ’ ’ ;
roll name
2 CD
Arnab Bhattacharya ([email protected]) CS315: SQL 2024-25 50 / 52
Savepoint Example
roll name
1 AB
2 CD
3 EF
4 GH
set t r a ns a c t i o n read w r i t e ;
savepoint sp1 ;
d e l e t e from s t u d e n t where r o l l = 1 ;
savepoint sp2 ;
d e l e t e from s t u d e n t where r o l l = 2 ;
r o l l b a c k to sp2 ;
set t r a ns a c t i o n read w r i t e ;
savepoint sp1 ;
d e l e t e from s t u d e n t where r o l l = 1 ;
savepoint sp2 ;
d e l e t e from s t u d e n t where r o l l = 2 ;
r o l l b a c k to sp2 ;
roll name
2 CD
3 EF
4 GH