Lecture 2 - SQL - template
Lecture 2 - SQL - template
Topics:
Overview of Querying in SQL
Joins
Set Operations
Renaming
Predicates
Ordering Tuples
Aggregate Functions
Null Values
Nested subqueries
Scalar subqueries
Join Expressions
Instance Modification
Tuple Deletion
Tuple Insertion
Tuple Modification
Reading:
Chapter 3
Querying in SQL
• the select clause: lists the attributes that will appear in the result relation. (select is
like project in relational algebra.)
o select * is a shorthand for selecting all attributes of relations appearing in the
from clause.
o select customer.* is a shorthand for selecting all attributes of the customer
relation (assuming that customer appears in the from clause).
• the from clause: lists the relations whose cartesian product is needed in the query
• the where clause: like select in relational algebra, although it has slightly different
syntax. An omitted where clause is equivalent to where true.
2. Apply the predicates specified in the where clause on the result of Step 1.
3. For each tuple in the result of Step 2, output the attributes (or results of expressions)
specified in the select clause.
Recall Natural Join: Operates on two relations and produces a relation as a result, considering
only pairs of tuples with the same values on those attributes that appear in the schemas of
both relations.
Example: Given the schema of a University Database from the textbook, list the names of
instructors along with the titles of courses that they teach.
Is this correct?
Because removing duplicates is expensive, SQL does not remove duplicates by default.
student
ID name dept_name tot_cred
1 Alice math 15
2 Chuck cmpsc 9
3 Bob math 45
select dept_name
from student
Would be:
dept_name
math
cmpsc
math
select dept_name
from student
Set Operations
SQL includes the set operations union (), intersection () and except (-). These operations can
be applied to the results of queries of the basic form.
Example: find the IDs, names, and departments of students and instructors.
Notes:
Tuple variables can be used to rename relations in much the same way that the rename
operation is used in relational algebra.
The keyword as can also be used to rename attributes in the result of a query.
Example: we wanted to compute the Cartesian product of student id’s and instructor id’s and
have unique attribute names in the result
SQL Predicates
SQL uses different syntax for predicates (in the where clause) than that used in selection
predicates in relational algebra
P or Q
not P
x between a and b
Pattern Matching in SQL: Use keyword like and wild card characters % (any string) and _ (any
character).
Example: Find the names of all students whose name has a q in it.
If you need % or _ to be treated as a regular character and not a metacharacter, use the
escape keyword after the string literal to specify an escape character.
Example: find the names of all students whose name has a q as the second character.
Ordering Tuples
Use the order by clause at the end of a query statement to order the tuples in a relation, as
in:
...
order by A1 [desc][asc], A2 [desc][asc], ..., An [desc][asc]
The keywords asc and desc can be used to control whether ascending or descending order is
used, respectively. The default is ascending.
Example: List (in alphabetic order) all departments that offer courses. Do allow duplicates in
the result.
Note: Multiple arguments to order by can be used to specify how to break ties.
Example: For students with more than 120 credits, list the students in descending order by
tot_cred. If two students have the same tot_cred, list them in order by their name.
Join Expressions
SQL provides various kinds of joins including the natural join that can be used in the from
clause of a query.
Inner Join
An inner join is a join in which tuples from one or both of the argument relations may not
participate in the result.
Outer Join
An outer join is a join that guarantees that all tuples from one or both of the argument relations
appear in the result of the join.
Example: Find the names all students who have never enrolled in a class.
Join Conditions
• natural
Attributes with same name must have same value for tuples to be matched.
• on P
where P is a predicate
only tuples for which the predicate is true will be matched
Note:
The using condition provides a join similar to a natural join, except that joining occurs only on
the attributes listed.
Example: Rewrite the following query using a using condition instead of the natural join.
select *
from (customer natural inner join purchased2);
Example: Find the students and instructors whose IDs match, even if they have different names
or dept_names:
The on Condition
The on condition provides a theta join - only tuples that satisfy the specified condition are
joined.
Example: Find the natural join of student and takes using an on condition.
Note:
Example: Rewrite previous query using the Cartesian product of customer and purchased and
no join conditions.
Final Notes on Joins
Defaults:
When the join clause is used, the default join type is an inner join. Thus the following are
equivalent:
Aggregate functions allow SQL queries to compute composite information about an entire
relation.
• avg
• min
• max
• sum
• count
Notes:
For queries with no group by clause, no other attributes can be used with an aggregate in the
select clause. However, multiple aggregates can be used.
Example: Find the maximum and minimum tot_cred of students in the math department.
Count and the distinct Keyword
With the count aggregate, you can use the distinct keyword before the attribute name to
get a count of unique values for an attribute.
The group by clause can be used with aggregate functions to compute information about
subsets of a relation.
Notes:
Example: Find the number of different courses taken by each student.
Example: Find the names of students and the number of courses (not necessarily unique) that
they have enrolled in.
If we’d prefer to have the names of customers rather than customer numbers:
Notes:
The having clause is similar to the where clause, except that conditions in a having clause
apply to groups created by a group by clause, rather than individual tuples.
The having clause can contain aggregates and the attributes listed in the group by clause, but
no other attributes.
Example: Find the number of classes enrolled in by each student in the math department for
students who have enrolled in at least 5 classes.
Notes:
Null Values
SQL uses a special value, called the null value, to mean that a particular value isn’t known or
doesn’t exist.
select *
from student
where tot_cred > 10.0;
Note:
is null and is not null Keywords
The keywords is null and is not null can be used in the where clause to explicitly test for the
presence and absence, respectively, of null values.
The clauses is known and is unknown can be used to test whether the result of a comparison is
unknown. In Oracle, these are not supported
coalesce function
Example: The person table has the schema (id, name, spouse), where spouse is null for
unmarried individuals. Return a relation with id’s, name’s, and spouse’s, where spouse is “NA”
rather than null for unmarried individuals.
Example: Calculate the sum of all credits for the courses in the “ABCD” department. If there are
no courses yet defined for this department, the sum should be 0.
Nested Subqueries
The in and not in connectives test the membership of a tuple in the result of an SQL query. This
allows SQL queries to be nested.
Example: Find the name and ID of every student who has taken course ID 1.
Question: How could we write the previous query without a nested subquery?
Constructing Tuples
Tuples can be constructed using (). That is, (v1, v2, . . . , vn) is a tuple of arity n containing values
v1, v2, . . . , vn.
Example: Express the previous query as a nested subquery with a tuple variable in the where
clause:
Set Comparison
Nested queries can be used with a comparison operator and keywords some or all to compare
an attribute against all values appearing in some other relation.
Q: What would be returned by the query above if there are null values for tot_cred?
Example: Find all students who have tot_cred greater than the minimum tot_cred.
Test for Empty Relations
The keywords exists and not exists are used to test whether or not the result of a nested
query is an empty relation. (a relation containing no tuples)
Example: Find all course ID’s models that no one has taken.
Note:
The with clause of SQL:1999 (and Oracle) provides a temporary relation for use within one
query. This is useful for simplifying complex queries and in cases where the same subquery is
used multiple times.
A scalar subquery is a subquery that returns a relation with one column and one row, that is
treated as a single value.
SQL:2003 (and Oracle) allow scalar subqueries to be used wherever a value of the same type
can be used.
Examples: Find all students who have more than the average tot_cred:
Database Modification
Deletion
Insertion
Where r is the target relation, Ai's are the attributes, and vi's are the value
Notes: The attributes of relation r must be (A1,A2, . . . ,An), and each vi must be a value in the
domain of Ai.
The attributes and values need not be listed in the order of the attributes of r.
Example: Add a new student with ID of 1, name of Bob, department of math, and tot_credit of
0.
If the attribute values in the values clause are ordered in the same order as the attributes of the
relation schema, the attribute list in the insert clause can be omitted.
Example:
Inserting Multiple Tuples
The values can be replaced with a select statement to insert the result of a query into a
relation.
Example: Insert all instructors into the student relation, keeping their ID’s, names, and
dept_names the same, and giving them 0 tot_cred.
PROBLEM!
Fix:
Updates
update r
set A1 = v1, A2 = v2, ...,An = vn
where P;
where …
Example: Increase the budget of all departments in Olmsted by 5%, and change their name to
CMPSC.
Updates and Nested Queries
The where clause of an update statement can contain a nested query, and the nested query
can reference the relation being updated.
Example: Increase the budget of all departments with less than the average budget by 5%.
SQL Merge Statement
Syntax
merge_insert:
merge_update:
merge_delete:
DELETE
Example:
Suppose that there is a temporary table new_student with the schema (id, name, dept). Ensure that any
students in this table also appear in student, and any existing students whose name or dept has changed
have these changes appear in the student table.