0% found this document useful (0 votes)
69 views71 pages

Introduction! Select-From-Where Statements! Queries Over Several Relations! Subqueries!

This document provides an overview of the SQL language. It discusses key SQL concepts like creating and modifying database schemas using CREATE TABLE and ALTER TABLE statements, performing queries with SELECT statements, and joining data from multiple tables. Some examples show how to retrieve data that matches certain conditions, project only certain columns, and combine conditions with logical operators. The document also covers topics like null values, three-valued logic in SQL query conditions, and performing queries across multiple tables.

Uploaded by

金阳
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views71 pages

Introduction! Select-From-Where Statements! Queries Over Several Relations! Subqueries!

This document provides an overview of the SQL language. It discusses key SQL concepts like creating and modifying database schemas using CREATE TABLE and ALTER TABLE statements, performing queries with SELECT statements, and joining data from multiple tables. Some examples show how to retrieve data that matches certain conditions, project only certain columns, and combine conditions with logical operators. The document also covers topics like null values, three-valued logic in SQL query conditions, and performing queries across multiple tables.

Uploaded by

金阳
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Introduction ! Select-From-Where Statements ! Queries over Several Relations ! Subqueries !

SQL is a very-high-level language.!


Expresses what to do rather than how to do it.! Avoid a lot of data-manipulation details needed in procedural languages like C++ or Java.! Called query optimization. !

Database management system gures out best way to execute query. !

SQL is primarily a query language, for getting information from a

database.!

But SQL also includes a data-denition component for describing database schemas!

IBM Sequel language developed as part of System R project at the

IBM San Jose Research Laboratory in the 1970s!


Renamed Structured Query Language (SQL)! ANSI and ISO standard SQL:!

SQL-86, SQL-89, SQL-92 ! SQL:1999, SQL:2003, SQL:2006, SQL:2008!

Commercial systems offer most, if not all, SQL-92 features, plus

varying feature sets from later standards and special proprietary features. !

Not all examples here may work on a particular system.!

Allows the specication of not only a set of relations but also information about each relation, including:!
The schema for each relation.! The domain of values associated with each attribute.! Integrity constraints!

Also (not covered in CMPT 354):!


The set of indices to be maintained for each relations.! Security and authorization information for each relation.! The physical storage structure of each relation on disk.!

Simplest form is:!

! ! ! !

!CREATE TABLE <name> (! ! !);! !DROP TABLE <name>;! !<list of elements>!

To delete a relation:!

Most basic element: an attribute and its type.! The most common types are:!

INT or INTEGER (synonyms).! REAL or FLOAT (synonyms).! CHAR(n ) = xed-length string of n characters.! VARCHAR(n ) = variable-length string of up to n characters.!

An attribute or list of attributes may be declared PRIMARY KEY or

UNIQUE.!
Either says that no two tuples of the relation may agree in all the

attribute(s) on the list.!


So keys provide a means of uniquely identifying tuples.! There can be only one PRIMARY KEY for a relation, but possible

several UNIQUE lists of attributes.!


No attribute of a PRIMARY KEY can ever be NULL. (Why?)!

Can also place PRIMARY KEY or UNIQUE after the type in the

declaration of the attribute. !


Example: Declare branch_name as the primary key for branch!

!create table branch " " (branch_name ! ! branch_city " ! ! assets "

"char(15) primary key, "char(30), "integer)!

The bar and beer together are the key for Sells:!

! ! ! ! ! !

!CREATE TABLE Sells (! ! ! ! ! !);! !bar !beer !price ! ! ! !CHAR(20),! !VARCHAR(20),! !REAL,!

!PRIMARY KEY (bar, beer)!

An SQL relation is dened using the create table command:!

!create table r (A1 D1, A2 D2, ..., An Dn, " " "(integrity-constraint1), ! ! !..., ! ! !(integrity-constraintk))!

r is the name of the relation! each Ai is an attribute name in the schema of relation r" Di is the data type of values in the domain of attribute Ai"

Example:!

!create table branch ! !(branch_name "char(15) not null, ! !branch_city !char(30), ! !assets " "integer)!

not null! primary key (A1, ..., An ) the attributes form a primary key! unique (A1, ..., An ) the attributes together form a

candidate key! Example: Declare branch_name as the primary key for branch" .! !create table branch " " (branch_name "char(15), ! ! branch_city "char(30), ! ! assets " "integer, ! ! primary key (branch_name))!

The drop table command deletes all information about the dropped

relation from the database.!

drop table <name>;!

The alter table command is used to add attributes to an existing

relation: !

!alter table r add A D"


where A is the name of the attribute to be added to relation r and D is the domain of A.!

All tuples in the relation are assigned null as the value for the new attribute. !

The alter table command can also be used to drop attributes of a

relation:! !

!alter table r drop A

"

where A is the name of an attribute of relation r" Dropping of attributes not supported by many databases!

SQL is based on set and relational operations with certain

modications and enhancements!


A typical SQL query has the form:

SELECT desired attributes! FROM one or more tables! WHERE condition about tuples of the tables!

A typical SQL query has the form:

!select A1, A2, ..., An !from r1, r2, ..., rm !where P


Ai represents an attribute! ri represents a relation! P is a predicate.! This query is equivalent to the relational algebra expression.!

The result of an SQL query is a relation.!

A lot of SQL queries will be based on the following database schema.!

! ! ! ! ! !

Underline indicates key attributes.! ! !Beers(name, manf)! ! ! ! ! ! !Bars(name, addr, license)! !Customers(name, addr, phone)! !Likes(customer, beer)! !Sells(bar, beer, price)! !Frequents(customer, bar)!

Using Beers(name, manf), what beers are made by Molson?!

! ! !

!SELECT name! !FROM Beers! !WHERE manf = Molson;!

! ! ! ! !

!name! !Export! !Molson Dry! !Corona! ! . . .!

The answer is a relation with a single attribute, name, and tuples with the name of each beer by Molson, such as Export.!

Begin with the relation in the FROM clause.! Apply the selection indicated by the WHERE clause.! Apply the extended projection indicated by the SELECT clause.!

E.g.:

!SELECT name! !FROM Beers! !WHERE manf = Molson;!

name

manf

Export Molson
Check if! Molson!

Include t.name ! in the result, if so!

Tuple-variable t loops over all tuples!

Think of a tuple variable visiting each tuple of the relation mentioned in

FROM.!
Check if the current tuple satises the WHERE clause.! If so, compute the attributes or expressions of the SELECT clause using

the components of this tuple.!

When there is one relation in the FROM clause, * in the SELECT clause

stands for all attributes of this relation.!


Example: Using Beers(name, manf):!

!SELECT * FROM Beers WHERE manf = Molson;

! ! ! ! !

!name

! ! ! ! !

manf! !Molson! !Molson! Molson! . . .!

!Export ! !Molson Dry !Corona ! ! ... !

Now, the result has each of the attributes of Beers.!

If you want the result to have different attribute names, use AS <new name>

to rename an attribute.!
Example: Using Beers(name, manf):!

! ! !

!SELECT name AS beer, manf! !FROM Beers! !WHERE manf = Molson!

! ! ! ! !

!beer

! ! ! ! !

!manf! !Molson! !Molson! Molson! . . .!

!Export ! !Molson Dry !Corona ! ! ... !

Any expression that makes sense can appear as an element of a SELECT

clause.!
Example: Using Sells(bar, beer, price):!

!SELECT bar, beer,! ! ! !price*76 AS priceInYen! !FROM Sells;!

! ! ! !

!bar !Joes !Sues !

! ! ! !

!beer

!priceInYen! !285! 342! ! !

!Export ! Sleeman ! !

Using Likes(customer, beer):!

!SELECT customer,! ! ! !likes Export AS whoLikesExport! !FROM Likes! !WHERE beer = Export;!

! ! ! !

!customer !Sally !Fred ! ! ! !

! whoLikesExport! !likes Export! !likes Export! ! !

We often build data warehouses from the data at many sources.! Suppose each bar has its own relation Menu(beer, price) .! To contribute to Sells(bar, beer, price) we need to query each bar and

insert the name of the bar.!


For instance, at Joes Bar we can issue the query:!

SELECT Joes Bar, beer, price! FROM Menu;!

Boolean operators AND, OR, NOT.! Comparisons =, <>, <, >, <=, >=.!

And many other operators that produce Boolean-valued results.!

Using Sells(bar, beer, price), nd the price Joes Bar charges for Export:!

! ! !

!SELECT price! !FROM Sells! !WHERE bar = Joes Bar AND beer = Export;!

A condition can compare a string to a pattern by:!


<Attribute> LIKE <pattern> or ! <Attribute> NOT LIKE <pattern>! % = any string; ! _ = any character.!

Pattern is a quoted string with !


Using Customers(name, addr, phone) nd the Customers with exchange

555:! SELECT name! FROM Customers! WHERE phone LIKE %555-_ _ _ _;!

Tuples in SQL relations can have NULL as a value for one or more

components.!
Meaning depends on context. ! Two common cases:!

Missing value : e.g., we know Joes Bar has some address, but we dont know what it is.! Inapplicable : e.g., the value of attribute spouse for an unmarried person.!

The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE,

UNKNOWN.!
Comparing any value (including NULL itself) with NULL yields UNKNOWN.! A tuple is in a query answer iff the WHERE clause is TRUE (not FALSE or

UNKNOWN).!

To understand how AND, OR, and NOT work in 3-valued logic, think of

TRUE = 1, FALSE = 0, and UNKNOWN = .!


AND = MIN!

OR = MAX! NOT(x) = 1-x.!


Example:!

TRUE AND (FALSE OR NOT(UNKNOWN)) = ! MIN(1, MAX(0, (1 - ))) =! !MIN(1, MAX(0, )) = ! MIN(1, ) = .!

From the following Sells relation:!

! !

! !

!bar

!beer

!price! !NULL!

!Joes Bar

!Export !

!SELECT bar! !FROM Sells! !WHERE price < 2.00 OR price >= 2.00;! The WHERE clause evaluates to UNKNOWN.! So Joes Bar isnt SELECTed..!

Some common laws, like commutativity of AND, hold in 3-valued logic. ! But not others, e.g., the law of the excluded middle : !

p OR NOT p = TRUE in classical logic.! When p = UNKNOWN, the left side is ! MAX( , (1 )) = != 1.!

Interesting queries often combine data from more than one relation.! We can address several relations in one query by listing them all in

the FROM clause.!


Distinguish attributes with the same name by !

!<relation>.<attribute> .!

Using relations Likes(customer, beer) and Frequents(customer, bar), nd the

beers liked by at least one person who frequents Joes Bar.! !SELECT beer! !FROM Likes, Frequents! !WHERE bar = Joes Bar AND Frequents.customer = Likes.customer;!

Almost the same as for single-relation queries:!


1. 2. 3.

Start with the product of all the relations in the FROM clause.! Apply the selection condition from the WHERE clause.! Project onto the list of attributes and expressions in the SELECT clause.!

Imagine one tuple-variable for each relation in the FROM clause.!

These tuple-variables visit each combination of tuples, one from each relation.!

If the tuple-variables are pointing to tuples that satisfy the WHERE

clause, send these tuples to the SELECT clause.!

customer tv1 Sally

bar

customer Sally

beer Export tv2

Joes check for Joe check these are equal Likes to output

Frequents

Sometimes, a query needs to use two copies of the same relation.! Distinguish copies by following the relation name by the name of a

tuple-variable, in the FROM clause.!


Its always an option to rename relations this way, even when not

essential.!

From Beers(name, manf), nd all pairs of beers by the same

manufacturer.!

! ! !

Do not produce pairs like (Export, Export).! Produce pairs in alphabetic order, e.g. (Export, Sleeman), not (Sleeman, Export).! SELECT b1.name, b2.name! FROM Beers b1, Beers b2! WHERE b1.manf = b2.manf AND b1.name < b2.name;!

A parenthesized SELECT-FROM-WHERE statement (subquery ) can

be used as a value in a number of places, including FROM and WHERE clauses.!


Example: In place of a relation in the FROM clause, we can use a

subquery and then query its result.!

Must use a tuple-variable to name tuples of the result.!

Find the beers liked by at least one person who frequents Joes Bar.!

SELECT beer! FROM Likes, (SELECT customer! ! ! !FROM Frequents!

Customers who frequent Joes Bar

!WHERE bar = Joes Bar)JC!

WHERE Likes.customer = JC.customer;!

If a subquery is guaranteed to produce one tuple, then the subquery

can be used as a value.!


Usually, the tuple has one component.! A run-time error occurs if there is no tuple or more than one tuple.!

Using Sells(bar, beer, price), nd the bars that serve Sleeman for the same price Joe charges for Export.! Two queries would certainly work:!
1. 2.

Find the price Joe charges for Export.! Find the bars that serve Sleeman at that price.!

!SELECT bar! !FROM Sells! !WHERE beer = Sleeman AND! ! ! !price = (SELECT price! ! ! ! ! FROM Sells! WHERE bar = Joes Bar! !AND beer = Export);!

! ! The price at ! which Joe ! sells Export

<tuple> IN (<subquery>) is true if and only if the tuple is a member of

the relation produced by the subquery.!

Opposite: <tuple> NOT IN (<subquery>).!

IN-expressions can appear in WHERE clauses.!

Using Beers(name, manf) and Likes(customer, beer), nd the name and

manufacturer of each beer that Fred likes.! ! SELECT *! ! FROM Beers! ! WHERE name IN (SELECT beer! ! ! The set! of! ! beers Fred! likes! ! ! FROM Likes! WHERE customer = Fred);!

SELECT FROM ! WHERE SELECT FROM ! WHERE

!a! !R, S! !R.b = S.b;! !a! !R! !b IN (SELECT b FROM S);!

SELECT FROM ! WHERE

!a! !R, S! !R.b = S.b;!

Double loop, over! the tuples of R and S!

a b 1 2 3 4 R

b c 2 5 2 6 S

(1,2) with (2,5) and (1,2) with (2,6) both satisfy! the condition;! 1 is output twice.!

Two 2s! SELECT FROM ! WHERE !a! !R! !b IN (SELECT b FROM S);!

One loop, over! the tuples of R!

a b 1 2 3 4 R

b c 2 5 2 6 S

(1,2) satises the condition;! 1 is output once.

EXISTS(<subquery>) is true if and only if the subquery result is not

empty.!
Example: From Beers(name, manf) , nd those beers that are the

only beer made by their manufacturer.!

Notice scope rule: manf refers! to closest nested FROM with! a relation having that attribute.

!SELECT name! !FROM Beers b1! !WHERE NOT EXISTS (!


Set of beers! ! with the! ! same! manf as! ! b1, but! not the! same! beer!

!SELECT *! !FROM Beers! !WHERE manf = b1.manf AND! ! !name <> b1.name);!

Notice the! SQL not! equals! operator!

x = ANY(<subquery>) is a boolean condition that is true iff x equals at least one

tuple in the subquery result.!

= could be any comparison operator.!

Example: x > ANY(<subquery>) means x is not the uniquely smallest tuple

produced by the subquery.!

Note tuples must have one component only.!

x <> ALL(<subquery>) is true iff for every tuple t in the relation, x is

not equal to t.!

That is, x is not in the subquery result.!

<> can be any comparison operator.! Example: x >= ALL(<subquery>) means there is no tuple larger than x

in the subquery result.!

From Sells(bar, beer, price), nd the beer(s) sold for the highest price.!

! ! ! ! !

!SELECT beer! !FROM Sells! !WHERE price >= ALL(! ! ! !SELECT price! !FROM Sells);!

price from the outer! Sells must not be! less than any price.!

Union, intersection, and difference of relations are expressed by the

following forms, each involving subqueries:!


(<subquery>) UNION (<subquery>)! (<subquery>) INTERSECT (<subquery>)! (<subquery>) EXCEPT (<subquery>)!

Using Likes(customer, beer), Sells(bar, beer, price), and Frequents (customer, bar), nd the Customers and beers such that:!
1. 2.

The customer likes the beer, and! The customer frequents at least one bar that sells the beer.!

Notice trick:! subquery is! really a stored! table.! The customer frequents! a bar that sells the! beer.!

(SELECT * FROM Likes)! !INTERSECT! (SELECT customer, beer! FROM Sells, Frequents! WHERE Frequents.bar = Sells.bar! );!

Aside: This is an example of a query that departs from the outer SELECTFROM-WHERE template!

Although the SELECT-FROM-WHERE statement uses bag semantics,

the default for union, intersection, and difference is set semantics.!

That is, duplicates are eliminated as the operation is applied. !

Motivation: efciency! When doing projection, it is easier to avoid eliminating duplicates.!

Just work a tuple-at-a-time. !

For intersection or difference, it is most efcient to sort the relations

rst.!

At that point you may as well eliminate the duplicates anyway.!

Force the result to be a set by

SELECT DISTINCT . . .!

Force the result to be a bag (i.e., dont eliminate duplicates) by ALL,

as in

. . . UNION ALL . . .!

From Sells(bar, beer, price), nd all the different prices charged for

beers:! ! ! !SELECT DISTINCT price! !FROM Sells;!

Notice that without DISTINCT, each price would be listed as many

times as there were bar/beer pairs at that price.!

Using relations Frequents(customer, bar) and Likes(customer, beer):!

! ! !

!(SELECT customer FROM Frequents)! ! !EXCEPT ALL! !(SELECT customer FROM Likes);!

Lists Customers who frequent more bars than they like beers, and does

so as many times as the difference of those counts.!

SQL provides several versions of (bag) joins.! These expressions can be stand-alone queries or used in place of

relations in a FROM clause.!

Natural join:!

! !

!R NATURAL JOIN S;! !R CROSS JOIN S;! Likes NATURAL JOIN Sells;!

Cartesian Product:! Example:! Relations can be parenthesized subqueries, as well.!

R JOIN S ON <condition> ! Example: using Customers(name, addr) and Frequents(customer, bar):!

! !

!Customers JOIN Frequents ON! ! !name = customer;!

!gives us all (d, a, d, b) quadruples such that customer d lives at address a and frequents bar b.!

You might also like