Vous êtes sur la page 1sur 14

LEARNING TO TALK TO

YOUR DATA
An intro to Structured Query Language (SQL)
WHO IS RICHARD
MATHIS?
CTO of CHSI Technologies

Designed and Implemented databases since 1999.

Professed all things but especially data geek

Min-maxer, constantly looking for the best new thing to eke out
1-2% more performance.
COURSE OBJECTIVES

Understand what SQL is


The Basics A few words are used in almost all queries.
Getting Data Getting the data you want
Filtering Data What you dont get is as important as what you get.
Formatting Data Stop repeating your work, get it right every time.
Updating Data update settings
STRUCTURED QUERY LANGUAGE

Used in many different applications beyond CHSI Connections

SQL Server Management Studio

Crystal Reports

Access

Custom Applications
DATABASE
LAYOUT
Each table is like a
spreadsheet.
We try to avoid
duplicating data
Almost any column
ending with the suffix ID
translates to another table.
Helpful tables included
as a supplement
THE ABCS OF
DATABASES
Tables
Columns

Rows
Primary Keys
ResultSet

CaSe InSeNsItIvItY
SELECT

SELECT
Creates a resultset, usually the contents of a table
Typical Syntax
SELECT columnlist FROM tablename
Columnlist represents one or more columns
separated by commas.
Asterisk (*) can be used as a wildcard to list all
columns.

Expert Tip: Tablename can alternatively be


replaced for other things even other SELECT
statements (in parenthesis)
CONSTANTS,
FUNCTIONS, AND
SUBQUERIES
Treat each column as if it were a
cell in Excel.

Some columns, just point to a


column in another table

Some have functions, similar to


excel functions.

Some will have formulas like


other queries (in parenthesis)
DATA SEPARATION
ANXIETY
Foreign Keys, Relationships, How to Find your
Data

Our tables tend to have many numeric


columns which point to related tables.

TableName + ID as a column usually means


there is another table with that TableName as
a stand alone table, for example contactID
and Contacts

There are many exceptions, like GroupID


refers to ClientGroups for historical purposes.
Thats why we give you a cheat sheet!
REUNITING
DATA

Most queries will need to use


JOINS

Common problems include


Duplicate Rows
Missing Data
Slow Performance

Avoid DISTINCT unless necessary


NEEDLE IN A HAYSTACK

You dont want ALL the data, just the data you need, thus, the WHERE statement.

Comparison Operators like =, <> (not equal), >, < , >=, <= all work

Like is used for wildcards, almost always with % as a wildcard operator like ch% for all words starting
with ch or %ed for all words ending with ed.

ORDER BY is used to sort your data.

Example: SELECT * FROM clients WHERE clienttype = client and startdate >= 1/1/2015 ORDER BY startdate

You can filter one query into another by using the IN command, like

SELECT * FROM clients WHERE clientid in (SELECT clientid FROM locations WHERE city=jasper)
FORMAT IN SQL

DRY Principal Dont Repeat Yourself

Most excel functions have an equivalent function


in SQL.

Get as close as possible, save your query, reuse


as needed.

Users almost always ask for changes, dont


assume any query will be used only once.
CHANGING DATA

Always type your WHERE first.

Difficult but not impossible to undo.

Save your query on test before executing on


production.
QUESTIONS?

Samples, problems, syntax?

Any future Questions can be sent to


support@chsitech.com

Vous aimerez peut-être aussi