Computational Finance and Risk Management
Financial Data Access
with SQL, Excel & VBA
Guy Yollin
Instructor, Applied Mathematics
University of Washington
Outline
1
Introduction to SQL
2
SQLite and sample databases
3
Simple queries
4
Queries with additional clauses
5
Querying multiple tables with subqueries
6
Querying multiple tables with join
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
Outline
1
Introduction to SQL
2
SQLite and sample databases
3
Simple queries
4
Queries with additional clauses
5
Querying multiple tables with subqueries
6
Querying multiple tables with join
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
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
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
relation, base relvar
table
derived relvar
view, query result, result set
tuple
row
attribute
column
†
http://en.wikipedia.org/wiki/Relational_database
Outline
1
Introduction to SQL
2
SQLite and sample databases
3
Simple queries
4
Queries with additional clauses
5
Querying multiple tables with subqueries
6
Querying multiple tables with join
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
Chinook sample database
The Chinook data model represents a digital media store, including tables
for artists, albums, media tracks, invoices and customers.
Chinook sample database
SQLite Manager for Firefox
SQLite Manager for Firefox
SQL and SQLite data types
SQLite storage mode
SQL datatype
Description
TEXT
TEXT
CHAR
variable length text
fixed length string (size specified at create time)
NCHAR
like CHAR but support Unicode characters
NVARCHAR
like text but with Unicode support
INTEGER
INTEGER
SMALLINT
4-byte signed integer
2-byte signed integer
TINYINT
1-byte unsigned integer
REAL
REAL
FLOAT
4-byte floating point
floating point
NUMERIC
NUMERIC
fixed or floating point with specified precision
DECIMAL
fixed or floating point with specified precision
BOOLEAN
true or false
DATE
date value
DATETIME
date time value
NONE
BLOB
binary data
http://www.sqlite.org/datatype3.html
Database keys
The relationships between columns located in different tables are usually
described through the use of keys
†
Primary Key
A column (or set of columns) whose values uniquely
identify every row in a table
Foreign Key
A column in a table which is also the Primary Key in
another table
†