1.3. Introduction To SQL
1.3. Introduction To SQL
1.3. Introduction To SQL
not null
primary key (A1, ..., An )
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 list 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 names of all departments with instructor, and remove
duplicates
select distinct dept_name
from instructor
The keyword all specifies that duplicates not be removed.
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.’
Keyword as is optional and may be omitted
instructor as T ≡ instructor T
Keyword as must be omitted in Oracle
String Operations
SQL includes a string-matching operator for comparisons on
character strings. The operator “like” uses patterns that are
described using two special characters:
percent (%). The % character matches any substring.
underscore (_). The _ character matches any character.
Find the names of all instructors whose name includes the substring
“dar”.
select name
from instructor
where name like '%dar%'
Match the string “100 %”
like ‘100 \%' escape '\'
String Operations (Cont.)
Patterns are case sensitive.
Pattern matching examples:
‘Intro%’ matches any string beginning with “Intro”.
‘%Comp%’ matches any string containing “Comp” as a substring.
‘_ _ _’ matches any string of exactly three characters.
‘_ _ _ %’ matches any string of at least three characters.
(select course_id from section where sem = ‘Fall’ and year = 2009)
union
(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 and in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
intersect
(select course_id from section where sem = ‘Spring’ and year = 2010)
Find courses that ran in Fall 2009 but not in Spring 2010
(select course_id from section where sem = ‘Fall’ and year = 2009)
except
(select course_id from section where sem = ‘Spring’ and year = 2010)
Set Operations
Set operations union, intersect, and except
Each of the above operations automatically eliminates
duplicates
To retain all duplicates use the corresponding multiset versions
union all, intersect all and except all.
select name
from instructor
where salary > some (select salary
from instructor
where dept_name = ’Biology’);
Definition of Some Clause
0
(5 < some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5 < some 5 ) = false
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
Example Query
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 = Ø
Correlation Variables
Yet another way of specifying the query “Find all courses
taught in both the Fall 2009 semester and in the Spring 2010
semester”
select course_id
from section as S
where semester = ’Fall’ and year= 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010
and S.course_id= T.course_id);
Correlated subquery
Correlation name or correlation variable
Not Exists
Find all students who have taken all courses offered in the
Biology department.
The unique construct tests whether a subquery has any duplicate tuples
in its result.
(Evaluates to “true” on an empty set)
Find all courses that were offered at most once in 2009
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id
and R.year = 2009);
Subqueries in the From Clause
SQL allows a subquery expression to be used in the from clause
Find the average instructors’ salaries of those departments where the average
salary is greater than $42,000.
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
Note that we do not need to use the having clause
Another way to write above query
select dept_name, avg_salary
from (select dept_name, avg (salary)
from instructor
group by dept_name)
as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;
Subqueries in the From Clause (Cont.)
And yet another way to write it: lateral clause
select name, salary, avg_salary
from instructor I1,
lateral (select avg(salary) as avg_salary
from instructor I2
where I2.dept_name= I1.dept_name);
Lateral clause permits later part of the from clause (after the lateral
keyword) to access correlation variables from the earlier part.
Note: lateral is part of the SQL standard, but is not supported on many
database systems; some databases such as SQL Server offer
alternative syntax
With Clause
The with clause provides a way of defining a temporary view
whose definition is available only to the query in which the with
clause occurs.
Find all departments with the maximum budget
or equivalently
insert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);