Академический Документы
Профессиональный Документы
Культура Документы
Guy Yollin
Instructor, Applied Mathematics
University of Washington
Guy Yollin (Copyright 2012)
Introduction to SQL
1 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
2 / 58
Lecture references
Ben Forta
Sams Teach Yourself SQL in 10 Minutes
Sams, 1999
Chapter 1-12
sqlzoo.net
SQL ZOO: Interactive SQL Tutorial
http://sqlzoo.net/
sqlite.org
SQL As Understood By SQLite
http://www.sqlite.org/lang.html
Introduction to SQL
3 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
4 / 58
SQL
SQL
SQL (pronounced squeal) stands for Structured Query Language, a
special-purpose programming language designed for managing data in
relational database management systems (RDBMS)
SQL has both an ANSI and ISO standard but minor compatibility
issues are commonn
frequent updates to the standards
vendor-specific procedural extensions
vendor-specific deviations
http://en.wikipedia.org/wiki/SQL
MS Access SQL has many proprietary incompatibilities
Introduction to SQL
5 / 58
Importance of SQL
Why SQL?
Knowledge of SQL is critical because the vast majority of real data owned
by the mast majority of real companies is maintained in an SQL
compatible database
Common databases that support SQL
Microsoft SQL Server
Oracle Database
IBM DB2
Sybase
Microsoft Access
MySQL
PostgreSQL
SQLite
A stylized fact
Introduction to SQL
6 / 58
Relational Database
Relational Database
A relational database is a collection of data items organized as a set of
formally described tables from which data can be accessed easily
Relational database theory uses a set of mathematical terms, which are
roughly equivalent to SQL database terminology:
Relational Term
SQL equivalent
table
derived relvar
tuple
row
attribute
column
http://en.wikipedia.org/wiki/Relational_database
Introduction to SQL
7 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
8 / 58
SQLite
SQLite is a self-contained,
serverless, zero-configuration
SQL database engine
SQLite is the most widely
deployed SQL database engine
in the world
SQLite is open-source
Introduction to SQL
9 / 58
Introduction to SQL
10 / 58
Introduction to SQL
11 / 58
Introduction to SQL
12 / 58
Introduction to SQL
13 / 58
INTEGER
REAL
NUMERIC
NONE
SQL datatype
TEXT
CHAR
NCHAR
NVARCHAR
INTEGER
SMALLINT
TINYINT
REAL
FLOAT
NUMERIC
DECIMAL
BOOLEAN
DATE
DATETIME
BLOB
Description
variable length text
fixed length string (size specified at create time)
like CHAR but support Unicode characters
like text but with Unicode support
4-byte signed integer
2-byte signed integer
1-byte unsigned integer
4-byte floating point
floating point
fixed or floating point with specified precision
fixed or floating point with specified precision
true or false
date value
date time value
binary data
http://www.sqlite.org/datatype3.html
Guy Yollin (Copyright 2012)
Introduction to SQL
14 / 58
Database keys
The relationships between columns located in different tables are usually
described through the use of keys
Primary Key
Foreign Key
http://www.atlasindia.com/sql.htm
Introduction to SQL
15 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
16 / 58
SELECT statement
The most common operation in SQL is the query which is performed with
the SELECT statement
SELECT retrieves data from one or more tables
returned data is called a resultset or recordset
Standard SELECT queries just read from the database and do not
change any underlying data
Notes about the SQL language:
SQL is not case-sensitive
SQL is ignores whitespace
strings must use single quotes
Introduction to SQL
17 / 58
SELECT/FROM wildcard
SQL: SELECT/FROM wildcard syntax
Introduction to SQL
18 / 58
SELECT/FROM wildcard
SQL: SELECT/FROM wildcard syntax
Introduction to SQL
19 / 58
SELECT/FROM
SQL: SELECT/FROM syntax
Introduction to SQL
20 / 58
SELECT/FROM
SQL: SELECT/FROM syntax
SELECT columnName ( s )
FROM tableName
Introduction to SQL
21 / 58
SELECT/FROM/WHERE
SQL: SELECT/FROM/WHERE syntax
SELECT columnName ( s )
FROM tableName
WHERE someCondition
Introduction to SQL
22 / 58
SELECT/FROM/WHERE
SQL: SELECT/FROM/WHERE syntax
SELECT columnName ( s )
FROM tableName
WHERE someCondition
Introduction to SQL
23 / 58
Description
=
<>
!=
<
<=
!<
>
>=
!>
BETWEEN
IS NULL
equality
non-equality
non-equality
less than
less than or equal to
not less than
greater than
greater than or equal to
not greater than
non-equality
is a NULL value
Introduction to SQL
24 / 58
SELECT/FROM/WHERE
SQL: SELECT/FROM/WHERE syntax
Introduction to SQL
25 / 58
SELECT columnName ( s )
FROM tableName
WHERE someColumn IN listOfValues
Introduction to SQL
26 / 58
SELECT columnName ( s )
FROM tableName
WHERE someColumn NOT IN listOfValues
Introduction to SQL
27 / 58
SELECT columnName ( s )
FROM tableName
WHERE someColumn LIKE wildcardSting
Introduction to SQL
28 / 58
SELECT columnName ( s )
FROM tableName
WHERE someColumn LIKE wildcardSting
Introduction to SQL
29 / 58
SELECT columnName ( s )
FROM tableName
WHERE someColumn LIKE wildcardSting
Introduction to SQL
30 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
31 / 58
ORDER BY clause
SQL: ORDER BY clause
SELECT columnName ( s )
FROM tableName
WHERE someCondition
ORDER BY columnName
Introduction to SQL
32 / 58
ORDER BY clause
SQL: ORDER BY clause
Introduction to SQL
33 / 58
Description
COUNT
SUM
AVG
MAX
MIN
Introduction to SQL
34 / 58
COUNT function
SQL: COUNT function
Introduction to SQL
35 / 58
COUNT function
SQL: COUNT function
Introduction to SQL
36 / 58
Introduction to SQL
37 / 58
AVG function
SQL: COUNT function
Introduction to SQL
38 / 58
MAX function
SQL: COUNT function
Introduction to SQL
39 / 58
GROUP BY clause
SQL: GROUP BY clause
Introduction to SQL
40 / 58
Introduction to SQL
41 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
42 / 58
Solution:
Find the GenreId of the desired style (from the Genre table)
then select all of the tracks that match the GenreID (from the
Track table)
Introduction to SQL
43 / 58
Introduction to SQL
44 / 58
Subquery
SQL: Subquery syntax
SELECT columnNames
FROM tableName
WHERE someColumn IN ( SELECT / FROM / WHERE statement )
Introduction to SQL
45 / 58
Solution:
Find the GenreId of the desired style (from the Genre table)
then select all of the Tracks that match the GenreID (from the
Track table) then select all of the Titles that match the
AlbumId (from the Album table)
Introduction to SQL
46 / 58
Nested subquery
Introduction to SQL
47 / 58
Outline
Introduction to SQL
Simple queries
Introduction to SQL
48 / 58
Solution:
Create a new table with the name of the song (from the Track
table) and the name of the songs genre (from the Genre table)
Introduction to SQL
49 / 58
WHERE clause connects the keys from the two different tables
Guy Yollin (Copyright 2012)
Introduction to SQL
50 / 58
Introduction to SQL
51 / 58
Introduction to SQL
52 / 58
Introduction to SQL
53 / 58
Introduction to SQL
54 / 58
Introduction to SQL
55 / 58
Introduction to SQL
56 / 58
Introduction to SQL
57 / 58
http://depts.washington.edu/compfin
Introduction to SQL
58 / 58