SQL
SQL
SQL
Data Transaction
Data Definition Data Query Data Control
Manipulation Control
Language Language Language
Language Language
BEGIN;
CREATE TABLE INSERT INTO
professor( SELECT name
professor GRANT select
empid integer, FROM professor
VALUES (2136, COMMIT; ON professor
name WHERE
varchar(10), 'Curie', 'C4', TO some_user;
rank = 'C4';
rank char(2)); 36);
ROLLBACK;
Data types
DDL – Create Table
Valid values of a foreign key must reference an existing value of a key in the
referenced table
CREATE TABLE course(
courseid integer PRIMARY KEY,
title varchar(30) NOT NULL,
ects integer,
taughtby integer,
FOREIGN KEY(taughtby) REFERENCES professor(empid)
);
DDL - Foreign keys
Foreign keys can also reference candidate keys enforced with UNIQUE
constraints
CREATE TABLE professor(
CREATE TABLE course( empid integer PRIMARY KEY,
courseID integer PRIMARY KEY, name varchar(10) NOT NULL,
title varchar(30) UNIQUE NOT NULL, rank char(2)
ects integer );
);
CREATE TABLE teaches(
title varchar(30) REFERENCES course(title) ON DELETE CASCASE,
empid integer REFERENCES professor(empid) ON DELETE SET NULL,
semester char(1)
);
DDL – Default values
When inserting data into a table, all values that are not explicitly stated are
set to null (standard default value).
When defining a table, we can specify another default value.
Delete a table
DROP TABLE professor;
Begin a transaction
BEGIN;
End a transaction and make all the changes made by the transaction visible
to others and become permanent
COMMIT;
Reset a transaction and discard all the changes made by the transaction
ROLLBACK;
DQL – Query Language
SFW block
DQL – Cross Product (Cartesian product)
If the FROM clause enumerates more than a single tables, the cross product
is computed
The result is the set of all combinations of tuples in the involved tables
SELECT *
FROM professor, course;
SELECT *
FROM professor CROSS JOIN course;
DQL - Standard join
SELECT *
FROM professor, course
WHERE professor.empid = course.taughtby;
DQL – Natural Join
professor course
empid name rank courseid title ects empid
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2134 Augustinus C3 5043 Theory of Cognition 3 2126
2137 Kant C4
professor
P empid name rank
2125 Socrates C4
2126 Russel C4
2128 Russel C2
2133 Popper C3
2134 Augustinus C3
2137 Kant C4 The keyword AS is optional
DQL – Renaming Tables
SELECT *
FROM professor P, course C
WHERE P.empid = C.taughtby;
Naming intermediate results
Intermediate result tables obtained from SQL operations or SFW blocks can
be assigned names (tuple variables)
Variables that rank over tuples of a table
SELECT teaching.name
FROM (professor NATURAL JOIN course) AS teaching;
The keyword AS is optional
DQL – Accessing tables multiple times
SELECT name
FROM professor;
professor
empid name rank name
2125 Socrates C4 Socrates
2126 Russel C4 Russel The results is a
2128 Russel C2 Russel bag (multiset)
2133 Popper C3 Popper
2134 Augustinus C3 Augustinus
2137 Kant C4 Kant
DQL - Duplicate Elimination
SELECT name
FROM professor
ORDER BY rank
professor
empid name rank name
2125 Socrates C4 C2 Russel The output tuples are
2126 Russel C4 C3
Popper sorted ascending by
2128 Russel C2 Augustinus default
2133 Popper C3 Socrates
2134 Augustinus C3 C4 Russel
2137 Kant C4 Kant
DQL - Ordering
Which professor teaches “Ethics”? SQL does not define the order
in which selection, projection,
SELECT DISTINCT name and join are executed
FROM professor, course
WHERE empid = taughtby AND title = 'Ethics';
πname(σempid=taughtby ∧ title='Ethics'(professor x course))
Set Operations
SELECT name
FROM professor
Uncorrelated subquery
WHERE empid IN (SELECT taughtby
FROM course);
Uncorrelated Subqueries
Determine the empid of the professors with offices in the same building as
professors from the department of Philosophy (PHI)
SELECT empid
FROM professor
WHERE building IN (SELECT building
professor FROM professor
empid name rank dept building WHERE dept = 'PHI');
2125 Socrates C4 PHI 2
empid
2126 Russel C4 SCI 7
2128 Russel C2 PHI 2 2125
2133 Popper C3 CS 9 2128
2134 Augustinus C3 THE 2 2134
2137 Kant C4 PHI 2 2137
Example
Determine the empid of the professors with offices in the same building as
professors from the department of Philosophy (PHI) without using IN
professor(empid, name, rank, dept, building)
πempid(professor) − πtaughtby(course)
professor
empid name rank
2125 Socrates C4 SELECT empid
2126 Russel C4 FROM professor
2128 Russel C2 WHERE empid NOT IN (SELECT taughtby
2133 Popper C3
2134 Augustinus C3
FROM course);
2137 Kant C4
course empid
courseid title ects taughtby 2128
5001 Basics 4 2137
2133
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 2134
Simulation of the difference operator
πempid(professor) − πtaughtby(course)
professor
empid name rank
2125 Socrates C4 (SELECT empid FROM professor)
2126 Russel C4 EXCEPT
2128 Russel C2
2133 Popper C3
(SELECT taughtby FROM course);
2134 Augustinus C3
2137 Kant C4
course empid
courseid title ects taughtby 2128
5001 Basics 4 2137
2133
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 2134
Correlated Subqueries
course name
courseid title ects taughtby Socrates
5001 Basics 4 2137 Russel
5041 Ethics 4 2125 Kant
5043 Theory of Cognition 3 2126
Correlated Subqueries
course name
courseid title ects taughtby Socrates
5001 Basics 4 2137 Russel
5041 Ethics 4 2125
5043 Theory of Cognition 3 2126 Kant
IN vs. EXISTS
SELECT name
FROM professor
WHERE empid IN (SELECT taughtby
FROM course);
Is the “left tuple” in the “right set”
SELECT name
FROM professor
WHERE EXISTS (SELECT *
FROM course c Is the “right set” nonempty?
WHERE taughtby = empid);
Expressiveness of the SQL core
List all the students that have been graded along with their grades
SELECT *
FROM student NATURAL JOIN grades;
student grades
studid name semester studid courseid empid grade
24002 Xenokrates 18 28106 5001 2126 1
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 27550 4630 2137 2
26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3 studid name semester courseid empid grade
29120 Theophrastos 2 25403 Jonas 12 5041 2125 2
29555 Feuerbach 2 27550 Schopenhauer 6 4630 2137 2
28106 Carnap 3 5001 2126 1
DQL – INNER JOIN
List all the students that have been graded along with their grades
SELECT s.studid, name, semester, courseid, empid, grade
FROM student s INNER JOIN grades g
ON s.studid = g.studid;
student grades
studid name semester studid courseid empid grade
24002 Xenokrates 18 28106 5001 2126 1
25403 Jonas 12 25403 5041 2125 2
26120 Fichte 10 27550 4630 2137 2
26830 Aristoxenos 8
27550 Schopenhauer 6
28106 Carnap 3 studid name semester courseid empid grade
29120 Theophrastos 2 25403 Jonas 12 5041 2125 2
29555 Feuerbach 2 27550 Schopenhauer 6 4630 2137 2
28106 Carnap 3 5001 2126 1
DQL – LEFT OUTER JOIN
List all the students along with the grades they have obtained
grades
SELECT *
studid courseid empid grade
FROM student s LEFT OUTER JOIN grades g 28106 5001 2126 1
ON s.studid = g.studid; 25403 5041 2125 2
27550 4630 2137 2
Aggregate functions compute new values for a column, e.g., the sum or the
average of all values in a column
Aggregate functions: AVG, MAX, MIN, COUNT, SUM
Number of professors
All tuples with the same value for column taughtby form a group
The sum is computed for each group
SELECT SUM(ects) AS teachingLoad
DQL – Grouping FROM course c
GROUP BY taughtby;
SELECT COUNT(*)
FROM course
GROUP BY taughtby;
courseid title ects taughtby empid name rank office AVG(ects) > 3
5041 Ethics 4 2125 2125 Socrates C4 226
5049 DBS 2 2125 2125 Socrates C4 226 true
4052 Logics 4 2125 2125 Socrates C4 226
5043 Theory of Cognition 3 2126 2126 Russel C4 232
2126 Russel false
5052 Theory of Science 3 2126 2126 Russel C4 232
5001 Basics 4 2137 2137 Kant C4 7
2137 Kant true
4630 Constructive Criticism 4 2137 2137 Kant C4 7
Executing a query with GROUP BY and
HAVING SELECT empid, name, SUM(ects)
FROM course, professor
WHERE taughtby = empid AND rank = 'C4'
GROUP BY taughtby, name
HAVING AVG(ects) > 3;
courseid title ects taughtby empid name rank office SUM(ects)
5041 Ethics 4 2125 C4 226
5049 DBS 2 2125 2125 Socrates C4 226 10
4052 Logics 4 2125 C4 226
5001 Basics 4 2137 C4 7
2137 Kant 8
4630 Constructive Criticism 4 2137 C4 7
SELECT COUNT(semester)
FROM student;
Do these queries produce the same result?
Yes, in both queries the tuples with NULL values in the column semester are not counted
DQL – Null values
student
SELECT COUNT(semester) AS c
FROM student; c
studid name semester
5
24002 Xenokrates ⊥
25403 Jonas 12
26120 Fichte 10
SELECT COUNT(*) AS c
FROM student; c
26830 Aristoxenos ⊥
8
27550 Schopenhauer 6
28106 Carnap ⊥
29120 Theophrastos 2 SELECT COUNT(DISTINCT semester) AS c
29555 Feuerbach 2 FROM student; c
4
DQL – Null values
SELECT studid
student FROM student
studid name semester semester > 5 WHERE semester > 5;
24002 Xenokrates ⊥ unknown
25403 Jonas 12 true
26120 Fichte 10 true studid
26830 Aristoxenos ⊥ unknown 25403
27550 Schopenhauer 6 true 26120
28106 Carnap ⊥ unknown 27550
29120 Theophrastos 2 false
29555 Feuerbach 2 false
Tuples evaluated to unknown or
false will not be part of the result
DQL – Null values
SELECT semester, COUNT(studid) as c
FROM student
student
GROUP BY semester;
studid name semester
24002 Xenokrates ⊥ semester c
25403 Jonas 12 ⊥ 3
26120 Fichte 10 12 1
26830 Aristoxenos ⊥ 10 1
27550 Schopenhauer 6 6 1
28106 Carnap ⊥ 2 2
29120 Theophrastos 2
29555 Feuerbach 2
null is interpreted as an
independent value and
Number of students per semester results in its own group
DQL – Null values
SELECT studid
FROM student
WHERE semester + 1 <= 12;
student
studid name semester semester + 1 semester + 1 <= 12
24002 Xenokrates ⊥ ⊥ unknown
25403 Jonas 12 13 false
studid
26120 Fichte 10 11 true
unknown 26120
26830 Aristoxenos ⊥ ⊥ 27550
7 true
27550 Schopenhauer 6 29120
28106 Carnap ⊥ ⊥ unknown
true 29555
29120 Theophrastos 2 3
29555 Feuerbach 2 3 true
Which courses need to be taken before taking the course “Theory of Science”
requires course
predecessor successor courseid title ects taughtby
5001 5041 5001 Basics 4 2137
5001 5043 5041 Ethics 4 2125
5001 5049 5043 Theory of Cognition 3 2126
5041 5216 5049 DBS 2 2125
5043 5052 4052 Logics 4 2125
5041 5052 5052 Theory of Science 3 2126
5052 5259 5216 Bioethics 2 2128
5259 Advanced Algorithms 2 2133
SELECT predecessor 5022 Belief and Knowledge 2 2134
4630 Constructive Criticism 4 2137
FROM requires, course
WHERE successor = courseid But this query only finds the
AND title = 'Theory of Science'; direct predecessors
What courses form the result of this query?
SELECT r2.predecessor
FROM requires r1, requires r2, course c
WHERE c.title = 'Theory of Science' AND
c.courseid = r1.successor AND
r1.predecessor = r2.successor;
Theory of
r1 Advanced
Theory of
Cognition Algorithms
r2 5043
Science 5052
5259
Bioethics
Basics 5001 Ethics 5041 5216
DBS 5049
What courses form the result of this query?
SELECT rn.predecessor
FROM requires r1, requires r2,..., requires rn, course c
WHERE c.title = 'Theory of Science' AND
c.courseid = r1.successor AND
r1.predecessor = r2.successor AND
...
rn_minus_1.predecessor = rn.successor;
Level n predecessors
How to compute the complete list of predecessors (all levels)?
Level 1 UNION level 2 UNION level 3 UNION …
DQL – Recursion in SQL
Bioethics
Basics 5001 Ethics 5041 5216
DBS 5049
DQL – Limiting the size of the results
• SQL 2008
• Supported by IBM DB2, Sybase
SQL Anywhere, PostgreSQL,…
DQL – Limiting the size of the results
conceptual
schema Logical layer
internal
schema Physical layer
REPLACE VIEW expects the same columns (same order, same types)
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126
What to do now?
Option 1: reject the update
CREATE TABLE course (
courseid integer PRIMARY KEY,
title varchar(30),
ects integer,
taughtby integer REFERENCES professor(empid)
);
UPDATE professor
SET empid = 2121 Result of the UPDATE operation:
WHERE empid = 2125; Keys in professor and course updated
professor course
empid name rank courseid title ects taughtby
2121 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2121
2137 Kant C4 5043 Theory of Cognition 3 2126
Option 2: cascading updates
professor course
empid name rank courseid title ects taughtby
2125 Socrates C4 5001 Basics 4 2137
2126 Russel C4 5041 Ethics 4 2125
2137 Kant C4 5043 Theory of Cognition 3 2126
professor course
empid name rank courseid title ects taughtby
2126 Russel C4 5001 Basics 4 2137
2137 Kant C4 5043 Theory of Cognition 3 2126
professor course
empid name rank courseid title ects taughtby
2126 Russel C4 5001 Basics 4 2137
2137 Kant C4 5041 Ethics 4 ⊥
5043 Theory of Cognition 3 2126
Complex constraints
CREATE TABLE grades (
studid integer REFERENCES student ON DELETE CASCADE,
courseid integer REFERENCES course,
grade numeric(2,1) CHECK (grade BETWEEN 0.7 AND 5.0),
PRIMARY KEY(studid, courseid)
CONSTRAINT hasTaken
CHECK (EXISTS (SELECT *
FROM takes t
WHERE t.courseid = grades.courseid AND
t.studid = grades.studid))
);
The CHECK clause is evaluated for each update or insert
Operation is rejected if the check evaluates to false
Summary
SQL is more than query language (DDL, DML, DCL, TCL, DQL)
DQL is a declarative query language, SFW block is the basis
Complex predicates, nested queries
Grouping and aggregation
Recursive queries, (materialized) views
Integrity constraints
Appendix
Basic data definitions in SQL – Data types
character(n), char(n)
integer, smallint
Real, double
...
varchar(n) vs. char(n)
Back to DDL
BETWEEN
INTERSECT and EXCEPT can also be Set operators in combination with ALL are
combined with ALL (analogous to UNION) not supported by all DBMSs
Result pid
2125
Back to advanced SQL
The CASE construct
Comparison operators
SQL has a three-valued logic: true, false, and unknown
If at least one argument is null, then the result is unknown
semester < 12 unknown whenever semester is null
Evaluation of logical expressions
NOT
true false
unknown unknown
false true
Back to recursion
SQL 2003: window function
SELECT * FROM (
SELECT ROW NUMBER() OVER (ORDER BY semester DESC) AS
number, studid, name, semester
FROM student
) AS studTmp
WHERE number <= 5;
SELECT * FROM (
SELECT RANK() OVER (ORDER BY semester DESC) AS number,
studid, name, semester
FROM student
) AS studTmp
Back to advanced SQL
WHERE number <= 5;
Other non standard syntax
Back to limit