sql_tutorial
sql_tutorial
- Arthur C. Clarke
What is SQL?
• Language developed by IBM in 1970s for
manipulating structured data and retrieving
said data
SELECT
• Used to retrieve data from tables
[ GROUP BY group_by_expression ]
commonly used
[ HAVING search_condition ]
alias
derived table
common table
expression (CTE)
Using SQL from R
1. Connect to database
Using SQL from R
1. Connect to database
2. Run query
Using SQL from R
1. Connect to database
2. Run query
3. There is no step 3
Connecting to SQL Server from R
# requires RODBC package to be installed
library(RODBC)
ch = odbcConnect('DSN=Inbred')
# or
q = 'select * from snp_info'
results = sqlQuery(ch, q)
References/Resources
• SQL Server Books Online T-SQL reference (main page):
http://msdn.microsoft.com/en-us/library/bb510741(SQL.100).aspx
• SQL Server Books Online T-SQL reference (SELECT statement):
http://msdn.microsoft.com/en-us/library/ms189499(v=sql.100).aspx
• Tutorial: SQL Server Management Studio:
http://msdn.microsoft.com/en-us/library/bb934498(v=sql.100).aspx
• Tutorial: Writing Transact-SQL Statements:
http://msdn.microsoft.com/en-us/library/ms365303(v=sql.100).aspx
• SQL Server Express Edition (free, requires Windows):
http://www.microsoft.com/betaexperience/pd/SQLEXP08V2/enus/
• SQL joins: http://en.wikipedia.org/wiki/Join_(SQL);
http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/
• RODBC: http://cran.r-project.org/web/packages/RODBC/RODBC.pdf
• pyodbc: http://code.google.com/p/pyodbc/
• Instant SQL Formatter (makes code easier to read):
http://www.dpriver.com/pp/sqlformat.htm