You are on page 1of 186

Introduction To SQL

Unit 1

Definitions
SQL - Structured Query Language
SQL is probably one of the simplest
languages you will ever learn
learn. It is
also very simple to underestimate.
DONT!!! This is arguably the most
powerful language you will learn
learn.
SQL is a set oriented language. It
was designed and built to manage
groups of data
data.
ER Diagram - Entity Relationship
diagram
An ER Diagram, also known as a
database schema, gives you a
graphical depiction of the database
you are working
ki with.
ith

PUBS Database

sales
stor_id
ord_num
ord_date
qty
payterms
title_id

char(4)
varchar(20)
datetime
smallint
varchar(12)
varchar(6)

stor_id = stor_id

stores
stor_id
char(4)
stor_name
varchar(40)
stor_address varchar(40)
city
varchar(20)
state
char(2)
zip
char(5)

stor_id = stor_id

title_id = title_id

titleauthor
au_id
title_id
au_ord
royaltyper

varchar(11)
varchar(6)
tinyint
int

au_id = au_id

authors
au_id
varchar(11)
au_lname
varchar(40)
au_fname
varchar(20)
phone
char(12)
address
varchar(40)
city
varchar(20)
state
char(2)
zip
char(5)
contract
bit

titles
title_id = title_id

title_id
title
type
pub_id
price
advance
royalty
ytd_sales
notes
pubdate

varchar(6)
varchar(80)
char(12)
char(4)
money
money
int
int
varchar(200)
datetime

title_id = title_id

roysched
title_id
titl
id
varchar(6)
h (6)
lorange int
hirange int
int
royalty

discounts

publishers

pub_id = pub_id

pub_id
pub_name
city
state
country

char(4)
varchar(40)
varchar(20)
char(2)
varchar(30)

discounttype
stor_id
lowqty
highqty
discount

varchar(40)
char(4)
smallint
smallint
decimal

pub_id = pub_id

employee
emp_id
fname
minit
lname
job_id
jjob_lvl
pub_id
hire_date

char(9)
varchar(20)
char
varchar(30)
smallint
tinyint
y
char(4)
datetime

pub_id = pub_id

pub_info
pub_id char(4)
logo
image
pr_info text

authors_tmp
job_id = job_id

jobs
job_id
job_desc
min_lvl
max_lvl

smallint
varchar(50)
tinyint
y
tinyint

au_lname
au_fname
phone
address
city
state
zip

varchar(40)
varchar(20)
char(12)
varchar(40)
varchar(20)
char(2)
char(5)

Unit 1

Goals

What is a database
What is a table
Rows and columns
Connecting to your database
Change databases
Overview of PUBS database
Simple select
Select all columns from a table
Select specific
p
columns from a table
Concatenate two columns
Create a query to give formatted
output

Databases
At the most basic level a database is
really just a file.
Databases come in all shapes and
sizes. Some are large and some are
small.
ll But
B t each
h database
d t b
generally
ll
serves a particular purpose.
Examples: Tracking employee
payroll, sales data on a particular
sales line, stock data for a particular
industry
All databases are made up
p of
objects. The most important object
(and the one we will learn how to use
in this class) is a table.

Tables
A table is a storage structure made up
of rows and columns. (Sort of like a
spreadsheet )
spreadsheet.)
Due to the differing terminologies,
th
there
are interchangeable
i t h
bl sets
t off
terms:
Database
Table
Row
Column

Mathematical
Relation
Tuple
Attribute

Data Processing
File
Record
Field

These terms are used interchangeably,


but we will generally use the table
row column terminology

Tables cont.
You will also hear a table referred to
as an entity. (Hence the name Entity
Relationship Diagram)
In the most basic sense, an entity is
a person, place,
l
thi
thing, or id
idea.
Entities usually become tables
Example: books, publishers, titles,
authors

Connect to a database
In this class we will use a tool called
ISQL/W. This stands for Interactive
SQL / Windows
Windows. This is where we
will execute all of our queries from.
A query as the term implies is a
question we ask the database
database.
In other environments you will see
this query tool called by different
names. It is generally referred to as
just isql.
Regardless of name, they all perform
the same purpose. This is to give
you an interface
i t f
for
f sending
di SQL
statements and receiving results.

SQL Server Login


Startup ISQL/W
Login window
A SQL Server can have many
different databases running on it at
the same time.
Database setup on the server
Assign databases
Your first SQL
Q statement
use <database>
This tells the SQL Server what
database you will be using to perform
your queries.
Example: use PUBS1

Verify your database


To check the database you are
accessing use:
select db_name()
db name()
Throughout this course some of the
things we discuss will be MS SQL
S
Server
specific.
ifi Th
The DBMS you are
using should have a command or
function similar to this, but not
necessarily the same
same.

Basic syntax rules

SQL keywords (use, select, from,


where, etc.)) are case insensitive.
select is the same as SELECT is the
same as SeLeCt
SeLeCt, etc
etc.
However depending on the DBMS
(Database Management System)
System), the
columns might be case sensitive.
select title_id from titles is not
necessarily the same as
SELECT TITLE_ID FROM TITLES
The databases we have setup on our
server are case insensitive.

Rules cont.
Spacing does not matter (for the
most part).
select title_id
is the same as
select
title_id
However
However, you must still separate
words. You can not use the
following:
selecttitle id
selecttitle_id
This will give a
syntax error, because SQL Server
must be able to find your SQL
keywords.
keywords

Rules cont.
Carriage returns are ignored
select title_id from titles
is the same as
select title_id
from titles
The spacing and carriage returns just
make reading your SQL a lot easier.
The general format used by most
people is to place the separate
clauses of the statement on different
lilines

PUBS Database

PUBS is a database for a fictitious


book distributor that sells books to
book resellers.
resellers This is the database
for which you have an ER diagram
for.
ER diagram explanation
You will generally get an ER diagram
at each client when you begin work
project.
j
If yyou dont have one,
on a p
ask for one. This will save time in
trying to determine what data is
where and how everything is linked
together. If you cant get one, dont
panic! There are ways to get the
database to tell you what it contains.

Select
SELECT Statement Retrieves rows from the database.
SELECT [ALL | DISTINCT] <select_list> INTO [<new_table_name>]
[FROM <table_name> [, <table_name2> [..., <table_name16>]]
[WHERE <clause>] [GROUP BY <clause>] [HAVING <clause>] [ORDER
BY <clause>]
[COMPUTE <clause>] [FOR BROWSE]
where <table_name> | <view_name> =
[[<database>.]<owner>.]{<table_name>. | <view_name>.}
<joined_table> =
{{<table_name> CROSS JOIN <table_name> | <table_name> {{INNER | LEFT
[OUTER] | RIGHT [OUTER] |
FULL [OUTER]} JOIN <table_name> ON <search_conditions>}
<optimizer_hints>
One or more of the following, separated with a space:
[INDEX = {<index_name> | <index_id>}]
[NOLOCK] [HOLDLOCK] [UPDLOCK] [TABLOCK] [PAGLOCK]
[TABLOCKX] [FASTFIRSTROW]
WHERE <clause> =
WHERE <search_conditions>
GROUP BY <clause> =
GROUP BY [ALL] <aggregate
aggregate_free_expression
free expression> [[,
<aggregate_free_expression>]...]
[WITH {CUBE | ROLLUP}]
HAVING <clause> =
HAVING <search_conditions>
ORDER BY <clause> =
ORDER BY {{<table_name>. | <view_name>.}<column_name> |
<select_list_number> | <expression>} [ASC | DESC]
[...{{<table_name16>. | <view_name16>.}<column_name> |
<select_list_number> | <expression>} [ASC | DESC]]
COMPUTE <clause> =
COMPUTE <row
<row_aggregate>(<column_name>)
aggregate>(<column name>) [[,
<row_aggregate>(<column_name>)...]
[BY <column_name> [, <column_name>]...]

Select
A select statement is used to retrieve
data from a database. As you can
see from the syntax above
above, a select
statement can get very complicated.
D
Depending
di on th
the ttype off SQL
statement you are using most of this
is optional.

Select

In order to get information from the


database, yyou must tell the database
what you are looking for. The first
step along this journey is to get some
simple information from the
database.
se
select
ect 'Mary
a y had
ad a little
tt e lamb.'
a b
-------------Mary had a little lamb.
(1 row affected)

If you ever want to return a specific


phrase from a database, use this
construct.

Select
An asterisk (*) is used to designate
all columns in a table.
select *

We also need to tell it which table to


get the data from.
select * from authors

The main sections in every SQL


statement are called clauses. The
three clauses will will focus on are
the select,, from,, and where.

Select
select * from authors
au_id
au_lname
phone ...
----------- --------------------------172-32-1176 White
496-7223...
213-46-8915 Green
986-7020...
238 95 7766 C
238-95-7766
Carson
548-7723...
267-41-2394 O'Leary
286-2428...
274-80-9391 Straight
834-2919...
341-22-1782 Smith
843-0462...
409-56-7008 Bennet
658-9932...
427-17-2319 Dull
836-7128...
472-27-2349 Gringlesby
938-6445...
486-29-1786 Locksley
585-4620...
527-72-3246 Greene
297-2723...
297-2723
648-92-1872 Blotchet-Halls
745-6402...
672-71-3249 Yokomoto
935-4228...
712-45-1867 del Castillo
996-8275...
722-51-5454 DeFrance
547-9982...
724-08-9931 Stringer
843-2991...
724-80-9391 MacFeather

au_fname
-------------------- ---Johnson

408

Marjorie

415

Ch
Cheryl
l

415

Michael

408

Dean

415

Meander

913

Abraham

415

Ann

415

Burt

707

Charlene

415

Morningstar

615

Reginald

503

Akiko

415

Innes

615

Michel

219

Dirk

415

Stearns

415

Select
We can limit the columns returned by
specifying them instead of using *.
select au_lname,
au lname au
au_fname
fname from
authors
au_lname
----------------------------------------------White
Green
Carson
O'Leary
Straight
Smith
Bennet
Dull
Gringlesby
Locksley
G
Greene
Blotchet-Halls
Yokomoto
del Castillo
DeFrance
Stringer
MacFeather
Karsen
Panteley
Hunter
McBadden
Ringer
Ringer
(23 row(s) affected)

au_fname
-----------Johnson
Marjorie
Cheryl
Michael
Dean
Meander
Abraham
Ann
Burt
Charlene
M
Morningstar
i
t
Reginald
Akiko
Innes
Michel
Dirk
Stearns
Livia
Sylvia
Sheryl
Heather
Anne
Albert

Select
When you specify columns, you do not
have to specify them in the order they
appear in the table
table.
You could have also executed the
f ll i
following:
select au_fname, au_lname from
authors
au fname
----------------------------Johnson
Marjorie
Cheryl
Michael
Dean
Meander
Abraham
Ann
Burt
...
(23 row(s) affected)

au lname
-----------------------------White
Green
Carson
O'Leary
Straight
Smith
Bennet
Dull
Gringlesby

Concatenation
We can also combine data together.
This is called concatenation.
We really want to display the first
name and the last name separated
by a space and then the rest of the
data The plus symbol (+) is the
data.
most widely used symbol for
concatenation. (A double pipe || is
sometimes used
used, but very rarely
rarely.))
select au_fname+au_lname from authors
JohnsonWhite
MarjorieGreen
CherylCarson
y
MichaelO'Leary
DeanStraight
MeanderSmith
AbrahamBennet
AnnDull
BurtGringlesby
CharleneLocksley
MorningstarGreene
ReginaldBlotchet-Halls
AkikoYokomoto
Innesdel Castillo
...
(23 row(s) affected)

Concatenation cont.
Concatenation is used for string
(character) data. If a concatenation
operator (+) is used on numeric data
data,
the data is simply added together.
select
l t title_id,price,advance
titl id i
d
ffrom titl
titles
title id price
-------- -----------------------------BU1032
19.99
BU1111
11.95
BU2075
2.99
BU7832
19.99
MC2222
19.99
...
(18 row(s)
( ) affected)
ff
d)

advance
--------------------5,000.00
5,000.00
10,125.00
5,000.00
0.00

select title_id,price+advance from titles


title_id
-------- -------------------------BU1032
5,019.99
5 019 99
BU1111
5,011.95
BU2075
10,127.99
BU7832
5,019.99
MC2222
19.99
...
(18 row(s) affected)

Select

By combining the first select (selecting a


constant) with the select on authors we did
above, we can get some formatted output
from the database.

select au_fname+ + au_lname, city + , +


state + +zip
+zip from authors
----------------------------------------Johnson White
Marjorie Green
Ch
Cheryl
l C
Carson
Michael O'Leary
Dean Straight
Meander Smith
Abraham Bennet
Ann Dull
Burt Gringlesby
...
(23 row(s) affected)

------------------------Menlo Park,CA 94025


Oakland,CA 94618
B
Berkeley,CA
k l
CA 94705
San Jose,CA 95128
Oakland,CA 94609
Lawrence,KS 66044
Berkeley,CA 94705
Palo Alto,CA 94301
Covelo,CA 95428

One of the many things you should take


away from this class is the ability to put
together a SQL statement like the one
above. This simple principle saves DBAs
hundreds of hours and year and makes
their jobs much more simple.

Aliases

The title for our previous result set


isn't too informative, and we really
don't want to display our formula.
formula
We can rename or alias a column in
two ways
Use a space and then the alias
Specify the keyword as and then the
alias
li

select au_fname+ + au_lname, city


+ , + state + +zip Name_Address
from authors
select au_fname+ + au_lname, city
+ , + state + +zip as
Name_Address from authors

Aliases
We can apply aliases in two places
within our SQL statements
Select clause
From clause

B
By specifying
if i an alias
li iin th
the select
l t
clause we can rename the column
headers for the output
By specifying an alias in the from
clause, we can save some typing
and also perform some higher level
queries which will require this. (This
will be demonstrated in subsequent
units.)
it )

Unit 1 Review
A database is a collection of objects, the
most prominent of which is a table.
A table consists of rows/tuples/records
and columns/attributes/fields.
use <dbname> allows you to select a
d t b
database
SQL keywords are not case sensitive.
Spacing and carriage returns are not
needed.
You can include a constant in your result
set byy hadding
g it jjust as yyou would a
column
An * allows you to select all columns in a
table
A + is used for concatenating two
strings.

Unit 1 Exercises
Time allotted for exercises is 30
minutes

Introduction To SQL
UnitIntroduction
2

To TSQL
Unit 2

Modern Business
Technology

Unit 2

Goals
Limit result set with where
Use compound criteria
Grouping conditions
Comparison
p
operators
p
Ranges
Wildcards
Escape characters
Pattern Matching
Negation

Where
So far we have returned the entire
contents of a table.
This is usually not very practical
Suppose we wanted to see the
authors that live in California.
We could do a select * from authors
and scroll through the result set
looking for those where state = CA
While feasible for a small table, this
is not practical.
practical

Where
So to limit the result set to just the
data you need, we will use the third
major SQL clause: where
The where clause tells the database
which
hi h rows tto retrieve.
ti
To just retrieve those authors that
live in CA, we would use the
following:
select au_lname, aufname, state
from authors where state = 'CA'
au_lname
---------------------------------------White
Green
Carson
O'Leary
...
(15 row(s) affected)

au_fname
state
-------------------- ----Johnson
CA
Marjorie
CA
Cheryl
CA
Michael
CA

Compound Criteria
This limited our result set to just
those authors in CA
But our list of authors could begin to
get very large and were only looking
f those
for
th
authors
th
with
ith a last
l t name off
Green.
We would do this with the following:
select au
au_lname,
lname, au
au_fname,
fname, state
from authors where state = 'CA' and
lname = 'Green'
au_lname
au_fname
state
---------------------------------------- -------------------- ----Green
Marjorie
CA
(1 row(s) affected)

Compound Criteria

Make sure you are careful with the


spelling. Our database is case
insensitive, but this does not apply to
the data values.
Green does not equal GREEN

Compound Criteria

Now that we know how to get just


those authors who live in CA
CA, how do
we get the authors that live in KS
also?
We accomplish this through the use
of an OR instead of an AND
select * from authors where state =
'CA' or state = 'KS'
au_lname
au_fname
state
---------------------------------------- -------------------- ----White
Johnson
CA
Green
Marjorie
CA
Carson
Cheryl
CA
O'Leary
Michael
CA
St i ht
Straight
D
Dean
CA
Smith
Meander
KS
Bennet
Abraham
CA
Dull
Ann
CA
Gringlesby
Burt
CA
Locksley
Charlene
CA
...
(16 row(s) affected)

Compound Criteria
So, what is the difference between
using an AND and an OR?
The AND is exclusive
This means that the row must meet all of the
conditions in order to be selected

The OR is inclusive
This means that for a row to be selected
selected, it
has to meet just one of the criteria

Compound Criteria
Now we are going to get a little more
complicated.
We want to select all authors who
live in KS with a last name of Smith
and
d also
l every author
th ffrom CA
CA.
We know how to do the first part
select au_lname, au_fname, state
from authors where state = 'KS'
KS and
au_lname = 'Smith'

Compound Criteria

We also know how to do the second


partt
select au_lname, au_fname, state
from authors where state = 'CA'
We just need to put them together
select au_lname, au_fname, state
from authors where state = 'KS'
KS and
au_lname = 'Smith' or state = 'CA'
au_lname
---------------------------------------Smith
White
Green
Carson
O'Leary
Straight
Smith
B
Bennet
t
...
(17 row(s) affected)

au_fname
state
-------------------- ----George
CA
Johnson
CA
Marjorie
CA
Cheryl
CA
Michael
CA
Dean
CA
Meander
KS
Ab
Abraham
h
CA

Grouping Criteria
While this SQL statement returns the
data we want, it isnt very clear and is
sloppy
When using compound criteria in a
where
h
clause,
l
you should
h ld always
l
group the criteria to make it plain
exactly what you want.
You group by using parenthesis
The proper SQL statement is as
follows:
select * from authors where (state =
'KS' and lname = 'Smith') or state =
'CA'
CA

Comparisons
Besides using an =, you can also use
any of the other comparison
operators: >,
> <,
< <=,
<= >=.
>=
Suppose we want to return all of the
b k with
books
ith a price
i greater
t th
than
$10.00
select title_id, price from titles where price > 10

We could also write:


select title_id, price from titles where price >
$10.00
title_id price
-------- -------------------------BU1032
19.99
BU1111
11.95
BU7832
19.99
MC2222
19.99
PC1035
22.95
PC8888
20.00
...
(12 row(s) affected)

Comparisons

One other thing you can take


advantage of with comparison
operators is that they dont simply
apply to numeric types of data.
They can also be used on character
data.
To select all authors who live in
states that come after MA we could
use the following
select au_lname, state from authors where
state > 'MA'
au_lname
state
---------------------------------------- ----Greene
TN
Blotchet-Halls
OR
del Castillo
MI
Panteley
MD
Ringer
UT
Ringer
UT
(6 row(s) affected)

Range Output

Suppose we want to select all


authors who live in CA, MI, KS, and
UT
We could write the following:
select au_lname, state from authors where
state = 'CA' or state = 'MI' or state = 'KS' or
state = 'UT'

With long lists, this get get very


tedious and take a lot of typing.
yp g
Fortunately, SQL gives us something
much better

IN
Instead of using multiple ORs, we
can use an IN operator
select au_lname, state from authors where
state in ('CA','KS','MI','UT')

This will return the same list


au_lname
state
---------------------------------------- -----

Smith
White
...
Straight
Smith
Bennet
...
Yokomoto
del Castillo
Stringer
...
McBadden
Ringer
Ringer
(20 row(s) affected)

CA
CA
CA
KS
CA
CA
MI
CA
CA
UT
UT

Range Output

We now want to select all books that


have a price greater than or equal to
$10, but also less than or equal to
$20.
We could write the following:
select title
title_id,
id, price from titles where price >=
10 and price <= 20

But,
But there is a much simpler way
SQL has given us a between
operator
select title_id, price from titles where price
between 10 and 20

Wildcards
Sometimes we do not know exactly
what we are looking for
Or we are looking for the group of
data that match a certain pattern
In these cases we would use
wildcards within our where clause
SQL has two wildcard characters
The percent (%) symbol designates any
string of zero or more characters
The underscore (_) designates a single
character

Wildcards

Suppose we wanted to select all


authors whose first names start with
M
select au_fname,, au_lname from authors where
au_fname like 'M%'
au_fname
au_lname
-------------------- --------------------------------------Marjorie
Green
Michael
O'Leary
Meander
Smith
Morningstar
Greene
Michel
DeFrance
(5 row(s) affected)

Wildcards
Maybe we want to select all of the
authors whose first name is Carl.
We have to be careful here, because
it could be spelled Carl or Karl
select au_fname, au_lname from authors where
au_fname like '_arl'
au fname
-------------------Carl
Karl
(2 row(s) affected)

au lname
---------------------------------------Burns
Johnson

Wildcards
You can combine wildcards to
retrieve exactly what you need
Suppose we needed to retrieve all of
the Smiths in the database
The last name could be spelled
Smith, Smithe, or Smythe. We want
to retrieve all of the spellings
select au_fname,
_
au_lname
_
from authors where
au_lname like 'Sm_th%'
au_fname
-------------------Meander
Jim
P tti
Patti
(3 row(s) affected)

au_lname
---------------------------------------Smith
Smithe
Smythe
S th

Escape characters

But what happens when we really


want to find a % inside of the data
To find this data we will employ an
escape
p character
select notes from titles where notes like
'%@%%'
@
escape '@'
@

This tells the DBMS to treat the next


character after the escape character
(@) as a literal string
notes
------------------------------------------------------------------What happens when the data runs dry?%
(1 row(s) affected)

Pattern Matching

But what do we do when we know


what we are looking for, but know it
could have many variations.
We can employ
p y a technique
q called
pattern matching
This technique can mix wildcards
with sets of characters that required
to be present
These are designated within brackets
inside of the string we are matching

Pattern Matching
Suppose we wanted to retrieve all of
the authors whose last names
started with either an L
L, M
M, or S
select au_lname, au_fname from authors where
au lname like '[LMS]%'
au_lname
[LMS]%
au_lname
---------------------------------------Locksley
MacFeather
McBadden
Smith
Smith
Smithe
Smythe
Straight
Stringer
(9 row(s) affected)

au_fname
-------------------Charlene
Stearns
Heather
George
Meander
Jim
Patti
Dean
Dirk

Pattern Matching

Suppose we want to retrieve all five


letter first names where onlyy the first
character is uppercase
We do not want to retrieve name
names like McDayy
We also don't want names with
special characters like apostrophes
select au_lname, au_fname from authors where
au_lname like '[A-Z][a-z][a-z][a-z]'
au_lname
au_fname
---------------------------------------- -------------------Dull
Ann
(1 row(s) affected)

Pattern Matching
We want to retrieve all books with a
title of Life Without Fear, but don't
know how the word without was
stored (uppercase, lowercase, or
mixed case)
select title_id, titles from titles where title
like '%[Ww][Ii][Tt][Hh][Oo][Uu][Tt]%'
title id title
-------- -------------------------------------------------------PS2106
Life Without Fear
(1 row(s) affected)

Pattern Matching
We now want to retrieve just those
authors whose first name is four
characters long
select au_lname, au_fname from authors where
au fname like '____' (That
au_fname
(That'ss four
underscore characters)
au_lname
au_fname
---------------------------------------- -------------------Burns
Carl
Gringlesby
g
y
Burt
Johnson
Karl
Ringer
Ann
Straight
Dean
Stringer
Dirk
(6 row(s) affected)

Pattern Matching

But you ask, why do we see the entry


f Ann
for
A in
i this
thi lilist.
t It only
l h
has th
three
characters.
This is for two reasons
There was a space added to the end
Depending on how the database was set
up, it
i could
ld pad
d spaces on to the
h end
d
(Beyond scope)

Pattern Matching
To get around this we exclude the
space
select * from authors where au_fname
like '[^ ] [^ ] [^ ] [^ ]'
au_id
------------111-11-1112
111-11-1113
274-80-9391
472-27-2349
724 08 9931
724-08-9931

au_lname
au_fname
---------------------------------------- ----------------Burns
Johnson
Straight
Gringlesby
S
Stringer
i

Carl
Karl
Dean
Burt
Di
Dirk
k

(5 row(s) affected)

The caret is a negation operator


operator.
The query above says to retrieve any
first names that do not have a space
as one of the four characters

Pattern Matching
Granted, in most real world situations
you will not go to these lengths when
retrieving data
data.
But constructs like this are used
extensively
t
i l tto ensure only
l valid
lid d
data
t
is entered into tables
Rules and constraints are beyond the
scope of this unit, but the examples
below are for demonstrative
purposes to give an idea of further
applications to pattern matching

Pattern Matching
Suppose you have a column that will
accept 6 characters
You have to be careful, because
numbers and special characters like
# @ & will
#,@,&
ill also
l go iin thi
this column
l
To restrict this to just characters, use
the following
[A-z]
[A
z] [A
[A-z]
z] [A
[A-z]
z] [A
[A-z]
z] [A
[A-z]
z] [A
[A-z]
z]

Real World Example


A table we are working with stores
social security numbers (complete
with dashes)
'[0-9] [0-9] [0-9]- [0-9] [0-9]- [0-9] [0-9]
[0-9] [0-9]'
Vehicle VIN numbers have a veryy
specific format that conforms to the
following VIN: 1G2JB14KOL7569785
' [0-9][A-Z] [0-9][A-Z][A-Z] [0-9] [0-9][AZ][A-Z][A-Z] [0-9] [0-9] [0-9] [0-9] [09] [0
[0-9]
9] [0
[0-9]
9] '

Negation

We briefly touched on negation a few


slides before.
The negation operator is NOT or in
patterns a caret ((^))
p

Negation

In a query above we extracted all


four letter first names that did not
have a space in them
them.
Now we want to extract everything
but these names
select au_fname, au_lname from authors where
au_fname not like '[^ ] [^ ] [^ ] [^ ]'
au_fname
au_lname
-------------------- ---------------------------------------Abraham
Bennet
Reginald
Blotchet-Halls
Carl
Burns
Cheryl
Carson
Michel
DeFrance
Ann
Dull
Marjorie
Green
Morningstar
Greene
Burt
Gringlesby
g
y
Sheryl
Hunter
Karl
Johnson
Livia
Karsen
...
(28 row(s) affected)

Negation

Select all authors who do not live in


CA
select * from authors where state <> 'CA'

Depending on DBMS, this can also


be written as
select au
au_fname,
fname, au
au_lname
lname from authors
where state != 'CA'
au_fname
au_lname
-------------------- ---------------------------------------Carl
Burns
Karl
Johnson
Patti
Smythe
Jim
Smithe
Meander
Smith
Morningsta
Greene
TN
Reginald
Blotchet-Halls
OR
Innes
del Castillo
Michel
DeFrance
IN
Sylvia
Panteley
Anne
Ringer
Albert
Ringer
(12 row(s) affected)

state
----MA
MA
MA
MA
KS

MI
MD
UT
UT

Review

A where clause allows us to restrict the


result set
We can combine multiple
p criteria in a single
g
where clause using and/or
We can use comparison operators to specify
ranges of data
IN allows
ll
us tto easily
il specify
if a lilistt off values
l
to find
Between simplifies some range searches
and is inclusive (The value specified as the
upper and lower bound is also retrieved)
We can use % and _ as wildcards to do
sophisticated searching
We can use an escape character to cause
SQL to ignore a wildcard and treat as a
literal
These can be combined with pattern
matching to specify very specific patterns of
data
We can negate our searching by using not,
^ <>, or !!=
^,

Unit 2 Exercises
Time allotted is 30 minutes

Introduction To SQL
UnitIntroduction
3

To TSQL
Unit 3

Modern Business
Technology

Unit 3

Goals

Nulls
Group by
Order by
Distinct
Aggregates
Aggregates with grouping
Having
Compute
Unions

Null

There are times when data is missing


or incomplete
To handle this missing data, most
DBMS use th
DBMSs
the conceptt off a nullll
A null does not mean zero
A null also does not mean a blank
A null indicates that a value is
missing, unavailable, incomplete,
and inapplicable

Null

Nulls represent an unknown quantity


or value
You can't guarantee that a null does
equal some other value
You also can't guarantee that a null
doesn't equal another value
A null also might
g or might
g not equal
q
another value

Null
For example take the authors table
If we were to leave out the state data
for an author, this could bring up a
few questions
Is the author from CA?
Is the author not from CA?
Is
I the
th author
th from
f
some other
th state?
t t ?
Any or none of these questions could
be true

Null

Any question about a null could


provide three answers: yes, no, or
maybe
This could mean that using
g nulls
gives us a very serious problem,
since rows are selected based on a
criteria being true
Fortunately the DBMS manufacturers
have given us some relief

Rules for Nulls


A null does not designate an
unknown value
A null does not equal another distinct
value
A null does not equal another null
WAIT A MINUTE!!!

Nulls cont.

I can obviously test for a null and I can place


a null into a column

Since I am placing the same "value" (a null)


into a column, how can a null not equal a
null

A null represents the nonexistence of data

Something that doesn't exist can't be


compared with something else that doesn't
exist.

If it could then, this would imply that the


values being compared actually do exist.
This violates the definition of a null

Nulls (theory aside)

All of this appears to be rather deep


and theoretical. In fact entire books
have been written about nulls.
This class is based on the practical
application of SQL theory
To that end the only things you need
to remember are the following:
You can select rows that have a null
value
A null does not equal a null

Nulls Applied

Suppose we want to get the titles


that do not have an assigned royalty
Based on our previous experience
we would probably do the following:
select * from titles where royalty = null

Paradoxically, this would work in


most DBMSs
This is because most DBMS
manufacturers recognize the
problems with null and seek to
protect you from yourself. The
DBMS will convert this into it's proper
form and return what you asked for

Nulls Applied

The proper way is to be explicit in


what
h t you are asking.
ki
We want to know where the values
are null
select title, royalty
y y from titles where royalty
y y is
null
title
royalty
----------------------------------------------------------- ----------The Psychology of Computer Cooking
(null)
Net Etiquette
(null)
(2 row(s) affected)

The Basics recap


This completes all of the basics of
selecting data
To quickly recap
The select clause specifies what
columns we want to see
The from clause tells what table we
want to see data from
The where clause restricts the data
we will see

Order by

The order by clause is used to


specify a sorting order of the result
set
The sorting can be performed by
column name or by column number
select au_fname,au_lname from authors order
by au_lname,au_fname

or
select au_fname,au_lname from authors order
by 2,1

Order by

Depending upon the DBMS, the


column you are ordering by does not
need to be specified in the select
clause
select au_fname, au_lname from authors order
by state

While this does work on some


DBMSs, it is generally not advisable
The default sort order is ascending
(a-z), but you can specify a
descending order by using the
keyword desc
order by au_lname
au lname desc,
au_fname

Sort Order

If order by sorts the data, how do I


know what that order it is sorted in?
The sort order is determined by a
character set which is defined for a
database
In Sybase
S
and MS
S SQ
SQL Server,
S
this
character map can be retrieved by
executing sp_helpsort
exec sp_helpsort

Order by

An order by is not limited to actual


data columns
We can order by a calculation if we
wish
select au_fname + ' ' + au_lname name from
authors order by name
name
------------------------------------------------------------Abraham Bennet
Akiko Yokomoto
Albert Ringer
Ann Dull
...
Meander Smith
Michael O'Leary
Michel DeFrance
Morningstar Greene
Patti Smythe
Reginald
g
Blotchet-Halls
Sheryl Hunter
Stearns MacFeather
Sylvia Panteley
(27 row(s) affected)

Order by / Nulls

An order by is based upon a sort


order specified by a character set
Since nulls aren't characters, where
do these fit in?
Depending on the DBMS, you will
find the nulls at either the beginning
g
g
or the end of the result set.
Where they are depends on the way
the DBMS manufacturer has
specified

Distinct

As you have seen from some of the


queries we have run, you can get
what appear to be duplicate rows in
the result set
From the scope of the result set, they
are duplicates
From the scope of the database they
are not
This is because the select
statements we have performed up to
this point returned the row of data for
every row in a table that matched a
specific criteria

Distinct
Sometimes we do not want to see
these duplicate rows
We can eliminate them by use of the
distinct keyword
The distinct is placed immediately
after the select
There can also be only one distinct
per SQL statement
The distinct applies to all columns in
the select list

Distinct
select au_id from titleauthor
au_id
----------172-32-1176
213-46-8915
213-46-8915
238-95-7766
267-41-2394
267-41-2394
...
899-46-2035
899-46-2035
998-72-3567
998-72-3567
(25 row(s) affected)
select distinct au_id from titleauthor
au_id
----------172 32 1176
172-32-1176
213-46-8915
238-95-7766
267-41-2394
...
899 46 2035
899-46-2035
998-72-3567
(19 row(s) affected)

Aggregates

There are times when we want to


perform calculations on all of the
values in a column or table
We accomplish
p
this through
g the use
of aggregates
The three we will explore are count
count,
sum, and average

Count(*)
Count will return exactly what it's
name implies
It returns a count of the number of
rows in a table that match a certain
criteria
it i
select count(*) from authors will return the
number
b off rows iin th
the authors
th
ttable
bl
----------27
(1 row(s) affected)

select count(*) from authors where state = 'CA'


will return the number of authors living in CA
----------15
(1 row(s) affected)

Sum
The sum is used to add up all of the
values in a column
select sum(advance) from titles will return the
total amount advanced to all authors
--------------------------

95,400.00
(1 row(s) affected)

Avg

Avg will return the average value in a


column
select avg(price) from titles will return the
average price of all books
-------------------------14.77
(1 row(s) affected)

select avg(price) from titles where price > 10


will return the average price of the books
over $10
-------------------------17.94
(1 row(s) affected)

Group by
Data in a table is essentially stored
randomly
We can impose one type of order on
the result set with an order by
We can impose another type of order
on a result set by using a group by
clause

Group by

The group by will order the data into


groups that you specified and then
return the set of rows that determine
the groups
Duplicates are removed from this
result set
In this way, a group by performs a
similar operation to distinct
The distinct does not sort the data
though
You still need to specify an order by
clause to perform sorting

Group by
select type from titles group by type
type
-----------(null)
UNDECIDED
popular_comp
business
mod_cook
trad_cook
psychology
(7 row(s) affected)
select type from titles group by type order by 1
type
yp
-----------(null)
UNDECIDED
business
mod_cook
popular_comp
psychology
trad_cook
(7 row(s) affected)

Group by and Nulls

Nulls are treated specially by a group


by clause
When a group by is being evaluated,
all nulls are p
put in the same g
group
p
select type from titles group by type
type
-----------(null)
UNDECIDED
business
mod_cook
popular comp
psychology
trad_cook
(7 row(s) affected)

Group by and where


You can use a where clause to limit
the set of data that the group by will
consider
select type from titles where advance > 5000
group by type
type
-----------business
mod_cook
popular comp
popular_comp
psychology
trad_cook
(5 row(s) affected)

Group by
The true power of a group by comes
from using it in conjunction with an
aggregate
Suppose we wanted a count of each
t
type
off book
b k
At first thought you might be tempted
to do this:
select type,count(*)
yp
( ) from titles
Msg 8118, Level 16, State 1
Column 'titles.type' is invalid in the select list because it is not
contained in an aggregate function and there is no GROUP BY
clause.

Group by

This doesnt quite get what we need


select type,count(*) from titles group by type
type
-----------(null)
UNDECIDED
business
mod_cook
popular_comp
psychology
trad_cook

----------2
1
2
2
3
5
3

(7 row(s)
( ) affected)
ff
d)

Group by

One thing to remember is that if you


use a group by with an aggregate,
you must specify all nonaggregate
columns in the group by clause
select city,state,count(*) from authors group by
state will return a syntax error
Msg 8120, Level 16, State 1
Column 'authors.city' is invalid in the select list because it is not
contained in either an aggregate
gg g
function or the GROUP BY clause.

select city,state,count(*) from authors group by


state,city will return a result set
city
-------------------(null)
Ann Arbor
Berkeley
Corvallis
Covelo
Gary
...
(17 row(s) affected)

state
----MA
MI
CA
OR
CA
IN

----------4
1
2
1
1
1

Group by
You can not specify an aggregate in
the group by clause
select count(*) from authors group by count(*)
will return a syntax error
Msg 144, Level 15, State 1
Cannot use an aggregate or a subquery in an expression used for the
by-list of a GROUP BY clause.

Having
The having clause works just like a
where clause
There is a fundamental difference
The where clause defines the set of
data the grouping is done on
The having defines which groups are
going to be returned to the user

Having

Having clause generally contain


aggregates as part of the selection
criteria
select pub_id,sum(advance) from titles group
b pub_id
by
b id h
having
i sum(advance)
( d
) > 10000
pub_id
-----0736
0877
1389

-------------------------24,400.00
41,000.00
30,000.00

(3 row(s) affected)

This will return only the set of


pub_ids that had an advance of more
then $10000.

Having/Where

select type
type,count(advance)
count(advance) from titles where
advance > 10000 group by type,advance

select type,count(advance) from titles group by


type advance having advance > 10000
type,advance

Having/Where
In both queries we want to know the
types of those books with an
advance > 10000
10000, so why the
different results
Thi
This iis d
due tto th
the way th
the where
h
and
d
having are applied
What happens is the data is selected
based on the result set
It is then p
passed to the g
group
p by
y for
grouping
Finally it goes to the having which
returns the data requested.
q

Having/Where

In the first query, only those rows


that had an advance of > $10000
The grouping is then applied to these
rows
This was only 1 book for each of two
groups (the where criteria)

Having/Where

The having processes the


aggregates and grouping first instead
of the selection like where does
The having
g clause says
y g
give me the
groups that have one or more books
with an advance of > 10000

Where/Having
The concepts of where and having
clauses can get confusing very
quickly
The best way to get comfortable with
th
them
iis tto perform
f
a few
f
and
d observe
b
the results
Then draw out each of the steps on
paper until you can duplicate the
result set
The book "The Practical SQL
Handbook" has a g
good explanation
p
on pages 180 - 185

Compute

Now that everything is about as clear


as mud, we are going to introduce
another clause that can be employed
(compute)
In a nutshell, a compute is used to
calculate grand summaries
select title_id,type,price from titles where type
like '%cook%' compute avg(price)
title_id
-------MC2222
MC3021
TC3218
TC4203
TC7777

type
-----------mod_cook
mod_cook
trad_cook
trad_cook
trad_cook

price
-------------------------19.99
2.99
20.95
11.95
14.99
avg
==========================
14.17

(6 row(s) affected)

Compute by

A compute by is used to
subsummaries
This construct must be used with an
order by
select title_id, type, price from titles where type
lik '%cook%'
like
'%
k%' order
d b
by ttype compute
t
avg(price) by type
title_id
-------MC2222
MC3021

type
-----------mod cook
mod_cook

price
-------------------------19.99
2.99
avg
==========================
11.49

title_id
-------TC3218
TC4203
TC7777

type
-----------trad_cook
trad_cook
trad cook

price
-------------------------20.95
11.95
14.99
avg
==========================
15.96

(7 row(s) affected)

Compute/Compute by
These can be used in the same
query
select title
title_id,type,price
id type price from titles where type in
('business','mod_cook') order by type
compute sum(price) by type compute
sum(price)
title id
-------BU2075
BU7832

type
-----------business
business

price
-------------------------2.99
19.99
sum
==========================
22.98

title_id
-------MC2222
MC3021

type
-----------mod_cook
mod_cook

price
-------------------------19.99
2.99
sum
==========================
22.98
sum
==========================
45.96

(7 row(s) affected)

Compute/Compute by

Restrictions
With a compute/computed
p
p
by,
y yyou
can only use columns in the select
list
select title_id,type from titlescompute
sum(price) would return a syntax error

You must order by the compute by


column
You can use any aggregate except
count(*)

Compute/Compute by
Restrictions
Columns listed after the compute by
must be in the identical order to or a
subset of those listed after the order
by
Expressions must be in the same left
- right order
Compute by must start with the same
expressions
p
as listed after order by
y
and not skip any expressions

Compute/Compute by
Legal

order by a,b,c
compute by a,b,c
compute by a,b
compute
p
avg(price)
g(p
) by
ya
Illegal

order by a
a,b,c
bc
compute by b,a,c
compute by c,a
compute avg(price) by b

Unions

There are times when we want to


return two or more sets of data within
a single select statement
Examples of this are combining data
from two different tables when they
have mutually exclusive criteria
To do this we use a union

Unions

select * from authors where state = 'CA' union select * from authors where state
= 'MA'
au lname
state
---------------------------------------- ----Bennet
CA
Carson
CA
Dull
CA
Green
CA
Gringlesby
CA
Hunter
CA
Karsen
CA
Locksley
CA
MacFeather
CA
McBadden
CA
O'Leary
CA
Straight
CA
Stringer
CA
White
CA
Yokomoto
CA
Burns
MA
Johnson
MA
Smithe
MA
Smythe
MA
(19 row(s) affected)

Unions

The only restrictions on unions are


that the same number of columns
must be in each separate result set
and the datatypes must match
You can not union a select statement
that returns 2 columns with a select
that returns 3 columns
You also can't union a result set
where the first column of one select
is character data and the first column
of another select is numeric data

Unit 3 Review

Nulls are used to represent the nonexistence


of data
A null doesn't equal another null
An order by can be used to sort the result
set
Th sortt order
The
d is
i determined
d t
i d by
b the
th
database's character set
To remove duplicate rows from a result set
use distinct
You can perform calculations using
aggregates count(*), sum,avg are the most
common
You can group data together by using a
group by
Group by can be combined with aggregates
to perform sophisticated calculations
A having clause performs a restriction on a
group by
Having and where behave differently due to
the order they process the row selection
Compute can be used to calculate grand
summaries

Unit 3 Review cont.

Compute by can be used to calculate sub


summaries
Unions allow us to combine multiple results
sets and return them to the user in a group

Unit 3 Exercises

Time allotted for exercises is 1 hour

Introduction To SQL
Introduction
Unit
4

To TSQL
Unit 4

Modern Business
Technology

Unit 4

Goals

Primaryy keys
y
Foreign keys
Joining tables
S b selects
Sub-selects
Advantages/disadvantages of joins
and sub-selects

Relationships

A database derives its usefulness


from containing a group of tables that
have some relationship to each other
An entity is a person, place, or thing
of importance to an organization
An entityy generally
g
y becomes a table
Relationships are the connections
between tables
Relationships are usually
i l
implemented
t d as kkeys iin a d
database
t b
design

Relationships

For example take the titles and


publishers table
Every publisher publishes a title
And every title has a publisher
Thi
This relationship
l i
hi iis iimplemented
l
db
by
means of the key pub_id

Relationships

Relationships come in three different


varieties
One to one
One row in a table is related to exactly
one row in another table

One to many
One row in a table is related to one or
more rows in another table

Many to many
Many rows in a table are related to one
or more rows in another table

Relationships
A many to many relationship is
extremely poor database design
This type of relationship can cause a
large amount of confusion
The problem is that many to many
relationships do exist and must be
stored in a database
This is usually resolved into multiple
one to many relationships also
known as an intersection table

Relationships

An intersection table is an artificial


construct that is commonly used in
RDBMSs
It does not have any physical
meaning, but instead serves to break
up a many to many relationship
The titleauthor table is an example of
an intersection table

Relationships

Relationships are implemented in a


database as keys
Keys are a logical construct; they are
not a p
physical
y
p
part of the database
This means that a key does not
represent any physically quantifiable
item
Y
You will
ill generally
ll see numbers
b
used
d
as keys in a database (au_id,
pub_id, stor_id)

Primary Key

A primary key is a special type of key


that consists of one or more columns
that uniquely identify a row
Primaryy keys
y must be unique
q and
can not contain null values
A table will only have one primary
key
A primary
i
kkey will
ill reside
id on th
the one
side of a 1 - N relationship

Primary Key
pub_id is the primary key for the
publishers table
This will uniquely identify each
publisher in the table
We do not use the publisher's name,
because this could be the same as
another publisher
Also, it is easy to control data input to
ensure it is valid
It is much easier to check 4 digits
than 40 characters. Also a name can
be null.

Foreign Key

A foreign key is one or more columns


that refer to a primary key of another
table
pub
p _id is the p
primary
y key
y of
publishers
pub_id
pub id is a foreign key in titles
Example:
A publisher can publish many titles, but
a title must have a publisher
This relationship is shown by the
primary key of the publishers table
being stored with the title that
publisher published.

Composite Keys

A primary key and a foreign key can


consist of more than one column
When a key contains more than one
column, it is known as a composite
key
The p
primary
y key
y of the titleauthor
table is a composite (au_id,title_id)

Indexes

A discussion of indexes is well


beyond the scope of this course,
there are a few naming items that
can be noted
Keys will be implemented in a
database as an object called an
index
An index could be a primary key, a
foreign key, or neither
A primary key is the same as a
primary index or unique index
A foreign key is the same as a
foreign index

Joins

Up to this point we have confined our


queries to a single table
While this is done primarily for
retrieving data into transaction
processing applications, it doesn't
represent a real world application of
data querying
All of the data in a database is
g
into tables and we
segmented
generally need data from more than
one table to show what we need
To accomplish this, we use a join

Joins
You will notice that there is no such
thing as a join clause in our SQL
syntax
A join is simply a where clause
A join is generally constructed
between one primary key and
another primary key or between a
primary key and a foreign key
(Discussion of PK/FK symbols on
the ER Diagram)

Joins
Suppose we want to view a list of
sales for each store
We could simply do the following:
select * from sales
stor_id ord_num
------- -----------6380
6871
6380
722a
7066
A2976
7066
QA7442.3
7067
D4482
7067
P2121
7067
P2121
7067
P2121
7131
N914008
7131
N914014
7131
P3087a
...
(22 row(s) affected)

ord_date
qty
payterms...
---------------------- ------ -------Sep 14 1994 12:00AM
5
Net 60...
Sep 13 1994 12:00AM
3
Net 60...
May 24 1993 12:00AM
50
Net 30...
Sep
p 13 1994 12:00AM
75
ON invoice
Sep 14 1994 12:00AM
10
Net 60...
Jun 15 1992 12:00AM
40
Net 30...
Jun 15 1992 12:00AM
20
Net 30...
Jun 15 1992 12:00AM
20
Net 30...
Sep 14 1994 12:00AM
20
Net 30...
Sep 14 1994 12:00AM
25
Net 30...
May 29 1993 12:00AM
20
Net 60...

But, the stor_id is meaningless to us

Joins

What we want to see is the store


name, city, and state along with the
sales for each order
select stor_name,ord_num,qty from
stores,sales where stores.stor_id =
sales.stor_id
stor_name
---------------------------------------Eric the Read Books
Eric the Read Books
Barnum's
Barnum's
News & Brews
News & Brews
News & Brews
News & Brews
Doc-U-Mat: Quality Laundry and Books
Doc-U-Mat: Quality Laundry and Books
Doc-U-Mat: Quality Laundry and Books
Doc-U-Mat: Quality Laundry and Books
Doc-U-Mat: Quality Laundry and Books
Doc-U-Mat:
Doc
U Mat: Quality Laundry and Books
Fricative Bookshop
Fricative Bookshop
Fricative Bookshop
Bookbeat
...
(22 row(s)
(
( ) affected)
)

ord_num
-------------------6871
722a
A2976
QA7442.3
D4482
P2121
P2121
P2121
2121
N914008
N914014
P3087a
P3087a
P3087a
P3087a
QQ2299
TQ456
X999
423LL922

qty
-----5
3
50
75
10
40
20
20
20
25
20
25
15
25
15
10
35
15

Joins
What does this mean?
The select clause simply designates
which columns we want to see. If we
were retrieving a column that had the
same name in
i th
the ttwo tables,
t bl
we
would have to specify which table the
data was coming from
select stores.stor_id,stor_name, ord_num, qty
from stores,sales
where
h
stores.stor_id
t
t id = sales.stor_id
l
t id

Joins
From clause
We are retrieving data from more
than one table, so each table must
be specified in the from clause
The from clause can be seen as the
main driver of a SQL statement
If the table isnt in the from clause,
none of itit'ss columns can be used in
any other clause

Joins
The where clause
where
h
stores.stor_id
t
t id = sales.stor_id
l
t id

This tells the DBMS to take the first


store ID in the stores table and add
the data from the corresponding
store ID in the sales table to the data
retrieved
ti
d ffrom the
th stores
t
table
t bl
It then continues with the second
store ID, etc. until it reaches the end
of the table

Joins
A join can be seen as a special type
of selection criteria.
If there is a stor_id in the stores table
that does not exist in the sales table,
th data
the
d t for
f that
th t particular
ti l store
t
will
ill
not be returned
You can also add additional selection
criteria
select stores.stor_id,stor_name,city,
state,ord_num,qty
from stores,sales
where stores.stor_id = sales.stor_id and state =
'CA'

Joins

As we have seen, you can specify


just those columns that you want to
see
For instance we are jjust concerned
with the quantity of sales in CA
select sum(qty) from stores,sales where
stores.stor_id = sales.stor_id and state =
'CA'
----------275
(1 row(s) affected)

Joins

The type of join we have examined


so far
f is
i also
l referred
f
d tto as an equii
join or an inner join
In the case of stores and sales, we
could have a store that doesn't have
any sales
If we use the equi-join, we will not
see these stores that do not have
any sales
So, how do we get the list of sales for
So
all stores regardless of whether they
have any sales

Outer Joins

We accomplish this via an outer join


select stores.stor_id,stor_name, ord_num, qty
from stores,sales where stores.stor_id *=
sales.stor_id
stor_id
t
id stor_name
t
------- -----------------------------------6380
Eric the Read Books
6380
Eric the Read Books
7066
Barnum's
7066
Barnum's
7067
News & Brews
7067
News & Brews
7067
News & Brews
7067
News & Brews
7131
Doc-U-Mat: Quality Laundry and Books
7131
Doc-U-Mat: Quality Laundry and Books
7131
Doc-U-Mat: Quality Laundry and Books
7131
Doc-U-Mat: Quality Laundry and Books
7131
Doc-U-Mat: Quality Laundry and Books
7131
Doc-U-Mat: Quality Laundry and Books
7896
Fricative Bookshop
7896
Fricative Bookshop
7896
Fricative Bookshop
8042
Bookbeat
8042
Bookbeat
8042
Bookbeat
8042
Bookbeat
8042
Bookbeat
(22 row(s) affected)

ord_num
d
-------------------6871
722a
A2976
QA7442.3
D4482
P2121
P2121
P2121
N914008
N914014
P3087a
P3087a
P3087a
P3087a
QQ2299
TQ456
X999
423LL922
423LL930
756756
P723
QA879.1

qty
t
---5
3
50
75
10
40
20
20
20
25
20
25
15
25
15
10
35
15
10
5
25
30

Outer Joins
Notice the use of the asterisk (*)
where
h
stores.stor_id
t
t id **= sales.stor_id
l
t id

This tells the DBMS to return all of


the rows in the stores table with the
corresponding data in the sales table
and do not drop any store IDs that
are nott in
i th
the sales
l ttable
bl
Note: The use of an asterisk to
designate an outer join is used in
SQL Server (Sybase and MS) most
other DBMSs support a slightly
different syntax as does the ANSI-92
standard

Outer Joins
Outer joins come in three different
flavors
Left
Right
Full

A left outer join is the same thing as


a right outer join except for the order
Left: stores.stor_id *= sales.stor_id
Right:
g sales.stor_id
_ =* stores.stor_id
_
Every left outer join can also be
expressed as a right outer join and
vice versa

Full Outer Join


A full outer join is included here for
completeness
You should use a full outer join
ONLY under very specific
circumstances
i
t
A full outer join will produce a cross
product of the two tables
If you have one table with 100 rows
and another with 1000 rows, a full
outer join will produce a result set of
100,000
,
rows

Full Outer Join

This is because with a full outer join,


you are telling the database to give
every combination of rows possible
i.e. Each row is matched to every
row in the other table
This type
yp is q
query
y will almost never
be preformed and should be avoided
at all costs.
The first time you inadvertently fire
one of these off, you will get a rather
angry call from your DBA

Subqueries

Subqueries are simply a SQL


statement nested inside of another
SQL statement
The most common place to do this is
in a where or having clause.
select [distinct] select
select_list
list
from table_list
where {expression {[not] in | comparison
[[any|all]}|[not]
y| ]}|[ ] exists}}
(select [distinct] subquery_select_list from
table_list where conditions)
[group by group_by_list]
[having conditions]
[order by order_by_list]

Subqueries

Subqueries come in two basic kinds:


correlated and noncorrelated
A noncorrelated subquery is one in
which the inner q
query
y is independent,
p
,
gets evaluated first, and passes its
result set back to the outer query
A correlated subquery is one in
which the inner query is dependent
upon the results from the outer query

Subqueries

Below are examples of these two


kinds
noncorrelated:
select p
pub_name from p
publishers
where pub_id in (select pub_id from titles
where type = 'business')

correlated:
select pub_name from publishers p
where 'business'
business in (select type from titles
where oub_id = p.pub_id)

As is the case with most of the


subqueries, you can also express
them as a join

Subqueries

Subqueries also come in three


different types:
They return zero or more items
Theyy return exactlyy one item
They test for existence of a value
If you have a subquery of the first
type it must be preceeded by an IN.
where column = (select) will return
an error if the subquery returns more
than one item

Noncorrelated Subqueries

At a conceptual level, a noncorrelated


subquery is executed in two parts.

First the inner query is executed

It th
then passes itits results
lt back
b k to
t the
th outer
t
query which then finds the rows that match
the list passed back

The column names are resolved implicitly


based upon the from clause of the
corresponding query. You can always
explicitly define the table name
name.

This is recommended for complex


subqueries

Correlated Subqueries

Processing of a correlated subquery


is much more complicated, but these
can handle queries you can't easily
do with noncorrelated subqueries or
joins
A correlated subquery depends on
data from the outer query
The inner query will execute once for
each row in the outer query

Correlated Subqueries

The outer query retrieves the first


row off data
d t and
d passes the
th data
d t
values to the inner query
The inner query finds all rows that
match the data passed from the
outer query
Finally the rows from the inner query
are checked against
g
the conditions in
the outer query
If one or more rows match the
conditions, the data corresponding to
that row will be returned to the user

Joins or Subqueries

select distinct pub_name from publishers,


authors where publishers.city = authors.city

AND
select pub_name from publishers where city in
(select city from authors)

will return the same results


But if you want data from both the
publishers and authors tables, you
must use a join.
select pub_name,au_fname,au_lname
from publishers,authors
where publishers.city
publishers city = authors.city
authors city

Joins or Subqueries

select au_lname,au_fname,city from authors


where city in (select city from authors where
au_fname
f
= 'Dick'
'Di k' and
d au_lname
l
= 'Straight')
'St i ht')

can also be expressed as


select au_lname,au_fname,city from authors
a1 authors a2 where a1.city
a1,
a1 city = a1
a1.city
city and
a2.au_fname = 'Dick' and a2.au_lname =
'Straight'

This is referred to as a self join

Joins or Subqueries

Whether you use joins or subqueries


is usuallyy a matter of choice
Most joins can be expressed as
subqueries and vice versa
Calculating an aggregate and using
this in the selection criteria is an
advantage of subqueries
select title,price from
f
titles
where price = (select min(price) from titles)

Displaying data from multiple tables


is usually done with a join

Common Restrictions

The select list of a inner query


introduced by an IN can have only
one column.
column This column must also
be join compatible with the column in
the where clause of the outer query
Subqueries introduced by an
unmodified comparison operator (not
f ll
followed
db
by ANY or ALL) can nott
include a group by or having clause
unless this will force the inner query
t return
to
t
a single
i l value
l
Subqueries can not manipulate their
results internally. i.e. They can not
contain an order by or the keyword
INTO

Any and All

You use the ANY and ALL keywords


with a comparison operator in a
subquery
> ALL means g
greater than every
y
value in the results of the inner query
(> maximum value)
> ANY means greater than any value
in the results of the inner query (>
minimum value)

Any and All


ALL
Results
ANY
Results
> all (1,2,3) > 3
>any (1,2,3) > 1
< all (1,2,3) < 1
< any (1,2,3) < 3
= allll (1
(1,2,3)
2 3) = 1 and
d =2
2 and
d =3
3
= any (1,2,3)
(1 2 3)
or =2 or =3

=1
1

select title from titles where advance > all


(select advance from publishers,titles where
titles.pub_id = publishers.pub_id and
pub_name = 'New Age Books')
title
------------------------------------------------------------------------The Busy Executive's Database Guide
Cooking with Computers: Surreptitious Balance Sheets
You Can Combat Computer Stress!
Straight Talk About Computers
Silicon Valley Gastronomic Treats
The Gourmet Microwave
The Psychology of Computer Cooking
But Is It User Friendly?
Secrets of Silicon Valley
Net Etiquette
Computer Phobic AND Non-Phobic Individuals: Behavior Variations
Is Anger the Enemy?
Life Without Fear
Prolonged Data Deprivation: Four Case Studies
...
(18 row(s) affected)

Exists

The last type of subquery is used to


test for the existence of something
g
To find all of the publishers who
publish business books we would do
the following:
select distinct pub
pub_name
name from publishers where
exists (select 1 from titles where pub_id =
publishers.pub_id and type = 'business')
pub_name
---------------------------------------Algodata Infosystems
New Moon Books
(2 row(s) affected)

Additional Restrictions

A subquery that test for existence will


either contain one column or an
asterisk in the select list. It makes no
sense to include a column list,
because this type of query simply
tests to see if a row exists and does
not return any data

Nesting Subqueries

A subquery may contain another


subquery
In fact you can nest as many levels
as you need.
d H
However, ffor mostt
applications more than four levels is
an indication of poor database
design
select au_lname,au_fname from authors where
au id in (select au
au_id
au_id
id from titleauthors where
title_id in (select title_id from titles where
type = 'popular_comp'))

This will return the list of authors who


have written at least one popular
computing book

Unit 4 Review

A relationships are connections between


tables
Ap
primary
y key
y is that set of columns that
define a unique row
You can have only one primary key per table
A foreign key is one or more columns that
refer
f to
t a primary
i
key
k off the
th same or another
th
table
You can have up to 255 foreign keys per
table
A composite key consists of more than one
column
Joins are used when you need to retrieve
data from more than one table
The two kinds of joins are: equijoin and outer
join
An outer join comes in three flavors: left
left,
right, full
A full outer join will produce the cross
product of the two tables
Subqueries are nested SQL statements

Unit 4 Review

Subqueries come in two kinds: correlated


and noncorrelated
Subqueries are also of three different types:
return exactly one item, return zero or more
items, and test for existence
Most jjoins can be written as a subquery
q y and
vice versa
A subquery is used when you need to
include an aggregate in the where conditions
A join
j i is
i used
d when
h you wantt tto retrieve
ti
d
data
t
from more than one table
All means every value (> all means greater
than every
y value))
Any means at least one value (> any means
greater than at least one value)
Exists allows us to test to see if a value
exists
Exists queries are used with correlated
subqueries
Subqueries can be nested any number of
levels

Unit 4 Exercises
Time allotted for exercises is 1 hour

Introduction
To SQL
Introduction
Unit 9

To TSQL
Unit 5

Modern Business
Technology

Data Manipulation
Up until this point we have covered
numerous ways to get data out of
tables
But, this is only a quarter of the basic
SQL picture
i t
We must have also have a way to
add new data to a table, modify
existing data, and delete data
To accomplish this, we will use the
insert, update, and delete statements
These statements are also referred
to as DML, data manipulation
language

Insert
Partial syntax:
insert [into] table_name [(column_list)] {values
(expression [[,expression]}|
expression] }| {select
statement}

The insert statement is used to add


data to a table
It has
h two
t
clauses,
l
insert
i
t and
d values
l
with an optional columns clause
The insert specifies which table the
data is added to and the values
clause specifies what that data is

Insert
insert into authors
values('409-56-7008', 'Bennet', 'Abraham',
'415
415 658-9932'
658 9932 , '6223
6223 Bateman St
St.', 'Berkeley'
Berkeley ,
'CA', '94705')

Any character or date data must be


enclosed within single quotes
If you are specifying
if i a numeric
i or
monetary amount, do not enclose
these in quotes
If you do not have a value for a
particular column, specify the
k
keyword
d nullll

Insert
Because the previous statement
does not have a column_list, the data
is inserted into the table in the order
of the columns
F
For example
l the
th result
lt off this
thi
statement would be a new row in the
table that had the following:
au_id
id = 409
409-56-7008
56 7008
au_lname = Bennet
au_fname = Abraham
phone = 415 658
658-9932
9932
address = 6223 Bateman St.
city = Berkeley
state = CA
zip = 94705

Insert
If you did not have a value for the
address, the insert statement would
be as follows:
insert into authors
values ('409-56-7008', 'Bennet', 'Abraham',
'415
415 658-9932'
658 9932 ,null,
null 'Berkeley'
Berkeley , 'CA'
CA , '94705')
94705 )

The word into is optional, but it is


recommended for clarity

Insert

You do not always have to insert


data in the exact order of the
col mns in the table
columns
table, b
butt you
o m
must
st
then specify the column list
insert into authors (au_lname,city,state,au_id,
au_fname,zip,address,phone)
values('Bennet', 'Berkeley', 'CA',
'409
409-56-7008
56 7008', 'Abraham'
Abraham , '94705'
94705 ,
'6223 Bateman St.','415 658-9932',)

Thi
This will
ill produce
d
the
th sett result
lt as th
the
previous insert statement

Insert
You also must use a column list if
you do not specify all of the values
In the authors table, au_id,
au_lname, and au_fname are the
only columns that require a value
(not null)
We
W could
ld also
l write:
it
insert into authors (au_id,au_lname, au_fname)
values ('409-56-7008', 'Bennet', 'Abraham')

Insert
Finally, the values clause can be
replaced by a select statement
The way this works is that the first
column of the result set from the
select
l t statement
t t
t is
i placed
l
d iin th
the fifirstt
column of the column list in the insert
The second to the second, etc.
insert into authors ((au_id,au_lname,
_
_
au_fname)
_
)
select ID,LastName,FirstName from
authors_tmp

Insert
You can also do this same type of
insert without specifying a column list
The result set from the select
statement must match the table you
are inserting
i
ti iinto
t iin th
the number
b off
columns, and datatype of columns
The column names do not have to
match
insert into authors select * from authors_tmp

Insert
The select statement that you use for
inserting data can be of any variety
You can use a subquery(s), group
by, order by, where, having, multiple
t bl
tables,
etc.
t
You can not use a compute or
compute by

Update
update table_name set column_name1 =
{expression1/null | (select statement)}
[,column_name2 = {{expression2/null
[,
p
| (select
(
statement)}]
from table_name [where search_conditions]

An update statement is used to


modify existing data in a table
If you wanted to give a 10% discount
on all titles:
update titles set price = price * .9

Update
You use the where clause to restrict
which rows are updated
If you only wanted to discount those
books published before 1/1/87:
update titles set price = price * .9
where pubdate < '1/1/87'

Update
You can also change multiple
columns at the same time
update authors set city = 'Oakland West', zip =
'94611' where city = 'Oakland' and address
like
e '%College%'
%Co ege%

There is a special type of column in


some DBMSs called an identity
identity,
autoincrement, sequence, etc. This
is beyond the scope of this class, but
it is important to know you can not
update these columns
update titles
titles_ident
ident set title
title_id
id = 200 where
title_id = 1 will return an error

Update
When you need to restrict the update
to a set of rows that are based upon
more than one table
table, you must use
the from clause
W
We wantt to
t discount
di
t only
l those
th
books that are from publishers in CA
update titles set price = price * .9 from titles t,
publishers p where t.pub_id = p.pub_id and
p.state = 'CA'

Delete

Finally to delete data from a table,


we use a delete statement
delete table_name [from table_name,
table_name] [where search_conditions]

If you do not specify any search


conditions, yyou will delete all rows
from the specified table
delete authors

Delete
To restrict the rows you delete use a
where clause
delete from authors where state = 'CA'

To delete rows where the criteria is


based on multiple tables, use the
from clause
delete titles from titles t, publishers p where
t.pub_id = p.pub_id and p.state = 'CA'

Transaction Logs
The full discussion of transaction
logs is beyond the scope of this class
Every change to data (insert, update,
and delete) is logged to a special file
called
ll d a ttransaction
ti llog.
This is why insert, update, and delete
are referred to as logged operations

Truncate
You can also delete all of the data in
a table without using a delete.
This is accomplished via a truncate
command
truncate table table_name
truncate table authors

is the same as
delete authors

as far as the effect on the authors table

Truncate
The difference is in how these two
commands are handled
Delete is a logged operation
Truncate is a nonlogged operation
This becomes important in
recovering a server from a crash

Truncate
Generally you should not use a
truncate command
In some client sites, this is a
command that is reserved for the
DBA (d
(database
t b
administrator)
d i i t t )
The truncate will perform faster,
because it does not write to the
transaction log, but this command
should be avoided at all costs by
anyone other than a DBA
It is included here for completeness
p

Unit 9 Review

An insert statement allows you to add data


to a table
The insert can contain a list of columns
An insert can also obtain it's values from
another table
An update statement is used to change
existing data
An update can not be performed on an
identity/autoincrement/sequence column
A delete will delete data from a table
Insert, update, and delete part of the DML
(Data Manipulation Language)
Insert update
Insert,
update, and delete are logged
operations
Truncate will also remove all data from a
table, but should be avoided at all costs

Unit 9 Exercises
Time allotted for exercises is 1/2
hour

You might also like