P
P
D
Manipulating Data C
with Style in SQL
An introduction to SQL, the interface language to
most of the world’s st...
P
P
D
Relational Data C
•Relational data is organized in tables
consisting of columns and rows
•Fields (columns) con...
P
P
D
Intro to SQL C
•SQL (“Structured Query Language”) is a
declarative data definition and query
language for rela...
P
P
D
C Which database system
should I use?
1. Use the one your data is in
2. Unless you need specific things
(perf...
P
P
D
SQL: Working with Objects C
feature comparison
•Data Definition Language (DB Objects)
•CREATE (table, index, v...
P
P
D
SQL: Working with Rows C
feature comparison
•Data Manipulation Language (Records)
aka Query Language
•SELECT ...
P
P
D
SQL: SELECT Statement C
•SELECT <col_list> FROM <table> …
•Merging/Column Binding: JOIN clause
•Row binding: U...
P
P
D
C Intro to Relational Algebra
•Basic operators
SELECT WHERE, HAVING
PROJECT <COL_LIST>
RENAME AS
!
•Join op...
P
P
D
SQL Beginner Resources C
•Basic SQL Commands Reference:
http://www.cs.utexas.edu/~mitra/
csFall2013/cs329/lect...
P
P
D
C
SQL: Common Table
Expressions (CTEs)
•WITH <name> [(<col_list>)] AS (SELECT …)
•SELECT <col_list> FROM <tab...
P
P
D
SQL: Views from SELECTs C
•CREATE VIEW <name> AS …
•SELECT <col_list> FROM <table> …
•Merging/Column Binding: ...
P
P
D
SQL: Functions from Views C
•CREATE FUNCTION <name> (<params>) AS …
•SELECT … <params> …
•Merging/Column Bindi...
P
P
D
SQL: Tuning with EXPLAIN C
•EXPLAIN <options> SELECT …
Same
•rows scanned: COST option
as
•wordy response: V...
P
P
D
SQL: Tuning using Indexes C
•CREATE INDEX <name> ON <table>
(<col_list|expression>) …
•UNIQUE indices for key ...
P
P
D
C SQL in other languages
(or, accessing data in databases via sql in other languages)
•R with libraries
•RPost...
P
P
D
C SQL in other languages
(or, operating on other languages’ data structures via sql)
•R with libraries
•RSQLit...
P
P
D
C
Slides and code are
available on GitHub at
nihonjinrxs/polyglot-october2014!
P
P
D
C
Ryan B. Harvey
http://datascientist.guru
ryan.b.harvey@gmail.com
@nihonjinrxs
+ryan.b.harvey
Employment &...
of 18

Manipulating Data in Style with SQL

An introduction to SQL, the interface language to most of the world’s structured data, and practices for readable and reusable SQL code
Published on: Mar 4, 2016
Published in: Software      
Source: www.slideshare.net


Transcripts - Manipulating Data in Style with SQL

  • 1. P P D Manipulating Data C with Style in SQL An introduction to SQL, the interface language to most of the world’s structured data, and practices for readable and reusable SQL code Ryan B. Harvey ! October 14, 2014
  • 2. P P D Relational Data C •Relational data is organized in tables consisting of columns and rows •Fields (columns) consist of a column name and data type constraint •Records (rows) in a table have a common field (column) structure and order •Records (rows) are linked across tables by key fields Relational Data Model: Codd, Edgar F. “A Relational Model of Data for Large Shared Data Banks” (1970)
  • 3. P P D Intro to SQL C •SQL (“Structured Query Language”) is a declarative data definition and query language for relational data •SQL is an ISO/IEC standard with many implementations in common database management systems (a few below) Structured Query Language: ISO/IEC 9075 (standard), first appeared 1974, current version SQL:2011
  • 4. P P D C Which database system should I use? 1. Use the one your data is in 2. Unless you need specific things (performance, functions, etc.), use the one you know best 3. If you need other stuff or you’ve never used a database before: A. SQLite: FOSS, one file db, easy/limited B. PostgreSQL: FOSS, Enterprise-ready The above are my opinions based on experience. Others may disagree, and that’s OK.
  • 5. P P D SQL: Working with Objects C feature comparison •Data Definition Language (DB Objects) •CREATE (table, index, view, function, …) •ALTER (table, index, view, function, …) •DROP (table, index, view, function, …)
  • 6. P P D SQL: Working with Rows C feature comparison •Data Manipulation Language (Records) aka Query Language •SELECT … FROM … •INSERT INTO … •UPDATE … SET … •DELETE FROM …
  • 7. P P D SQL: SELECT Statement C •SELECT <col_list> FROM <table> … •Merging/Column Binding: JOIN clause •Row binding: UNION clause •Filtering: WHERE clause •Aggregation: GROUP BY clause •Aggregated filtering: HAVING clause •Sorting: ORDER BY clause feature comparison
  • 8. P P D C Intro to Relational Algebra •Basic operators SELECT WHERE, HAVING PROJECT <COL_LIST> RENAME AS ! •Join operators: inner/outer, cartesian •Set operators: union, intersect, set minus, and, or, etc. •SELECT name, id FROM t1 WHERE id<3 AND dob<DATE ‘2004-01-01’ (ΠNAME,ID σID<3 ∧ DOB<(1/1/2004) T1) For a very detailed Intro to Relational Algebra, see lecture notes from 2005 databases course, IT U of Copenhagen
  • 9. P P D SQL Beginner Resources C •Basic SQL Commands Reference: http://www.cs.utexas.edu/~mitra/ csFall2013/cs329/lectures/sql.html
  • 10. P P D C SQL: Common Table Expressions (CTEs) •WITH <name> [(<col_list>)] AS (SELECT …) •SELECT <col_list> FROM <table or CTE> … •Merging/Column Binding: JOIN clause •Row binding: UNION clause •Filtering: WHERE clause •Aggregation: GROUP BY clause •Aggregated filtering: HAVING clause •Sorting: ORDER BY clause Same as before! feature comparison
  • 11. P P D SQL: Views from SELECTs C •CREATE VIEW <name> AS … •SELECT <col_list> FROM <table> … •Merging/Column Binding: JOIN clause •Row binding: UNION clause •Filtering: WHERE clause •Aggregation: GROUP BY clause •Aggregated filtering: HAVING clause •Sorting: ORDER BY clause feature comparison
  • 12. P P D SQL: Functions from Views C •CREATE FUNCTION <name> (<params>) AS … •SELECT … <params> … •Merging/Column Binding: JOIN clause •Row binding: UNION clause •Filtering: WHERE clause •Aggregation: GROUP BY clause •Aggregated filtering: HAVING clause •Sorting: ORDER BY clause feature comparison
  • 13. P P D SQL: Tuning with EXPLAIN C •EXPLAIN <options> SELECT … Same •rows scanned: COST option as •wordy response: VERBOSE option before! •output formatting: FORMAT option •actually run it: ANALYZE option •runtime (only with ANALYZE): TIMING option •(EXPLAIN is not part of the SQL standard, but most implementations support it)
  • 14. P P D SQL: Tuning using Indexes C •CREATE INDEX <name> ON <table> (<col_list|expression>) … •UNIQUE indices for key fields •Use functions in expressions: What’s in your WHERE clause? feature comparison LOWER(<text_col>), INT(<num_col>) •Specify ordering (ASC, DESC, NULLS FIRST, etc.) and method (BTREE, HASH, GIST, etc.) •Partial indexes via WHERE clause
  • 15. P P D C SQL in other languages (or, accessing data in databases via sql in other languages) •R with libraries •RPostgreSQL, dplyr ! •Python with modules •psycopg2, SQLAlchemy
  • 16. P P D C SQL in other languages (or, operating on other languages’ data structures via sql) •R with libraries •RSQLite, sqldf ! •Python with modules •Pandas, PandaSQL Mostly, Data Frames.
  • 17. P P D C Slides and code are available on GitHub at nihonjinrxs/polyglot-october2014!
  • 18. P P D C Ryan B. Harvey http://datascientist.guru ryan.b.harvey@gmail.com @nihonjinrxs +ryan.b.harvey Employment & Affiliations* IT Project Manager Office of Management and Budget Executive Office of the President ! Thank you! ! Questions? Data Scientist & Software Architect Kitchology Inc. ! Research Affiliate Norbert Wiener Center for Harmonic Analysis & Applications College of Computer, Mathematical & Natural Sciences University of Maryland at College Park * My remarks, presentation and prepared materials are my own, and do not represent the views of my employers.

Related Documents