SQL Presentation
SQL Presentation
SQL Presentation
SQL
Outline
Overview of The SQL Query Language
Data Definition
Data Query
Additional Basic Operations
Set Operations
Null Values
Aggregate Functions
Nested Subqueries
Data Manipulation
Data Control
Transaction Control
History
Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
Integrity Constraints in Create Table
not null
primary key (A1, ..., An )
foreign key (Am, ..., An ) references r
Example:
Ai represents an attribute
Ri represents a relation
P is a predicate.
The result of an SQL query is a relation.
The select Clause
The select clause lists the attributes desired in the result of a query
corresponds to the projection operation of the relational algebra
Example: find the names of all instructors:
select name
from instructor
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
E.g., Name ≡ NAME ≡ name
Some people use upper case wherever we use bold font.
The select Clause (Cont.)
SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword distinct
after select.
Find the department names of all instructors, and remove
duplicates
select distinct dept_name
from instructor
The keyword all specifies that duplicates should not be removed.
Find the names of all instructors in the Art department who have
taught some course and the course_id
select name, course_id
from instructor , teaches
where instructor.ID = teaches.ID and instructor. dept_name = ‘Art’
The Rename Operation
The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
Find the names of all instructors who have a higher salary than
some instructor in ‘Comp. Sci’.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’
person supervisor
Bob Alice
Mary Susan
Alice David
David Mary
avg_sala
ry
Aggregation (Cont.)
as follows:
Ai can be replaced be a subquery that generates a single value.
ri can be replaced by any valid subquery
P can be replaced with an expression of the form:
B <operation> (subquery)
Where B is an attribute and <operation> to be defined later.
Subqueries in the Where Clause
Subqueries in the Where Clause
0
(5 < 5 )=
some true (read: 5 < some tuple in the relation)
6
0
(5 < 5 ) = false
some
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0
(= some) in 5)
However, ( some) not in
Set Comparison – “all” Clause
Find the names of all instructors whose salary is greater than the
salary of all instructors in the Biology department.
select name
from instructor
where salary > all (select salary
from instructor
where dept name =
’Biology’);
Definition of “all” Clause
F <comp> all r t r (F <comp> t)
0
(5 < all 5 ) = false
6
6
(5 < all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
Test for Empty Relations
The exists construct returns the value true if the argument
subquery is nonempty.
exists r r Ø
not exists r r = Ø
Use of “exists” Clause
or equivalently
The select from where statement is evaluated fully before any of its
results are inserted into the relation.
Otherwise queries like
insert into table1 select * from table1
would cause problem
Updates
Increase salaries of instructors whose salary is over $100,000
by 3%, and all others by a 5%
Write two update statements:
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
The order is important
Can be done better using the case statement (next slide)
Case Statement for Conditional Updates
66
Example of users 67
End of the course