SlideShare a Scribd company logo
1 of 22
Download to read offline
INTRO TO SQL
Bootcamp (https://tdmdal.github.io/mma-sql/)
September 21, 2020 Prepared by Jay / TDMDAL
What’s SQL (Structured Query Language)
• Most widely used database (DB) language
• a domain specific language (managing data stored in relational DB)
• Not a proprietary language
• Open specifications/standards
• All major DBMS (DB Mgmt. System ) vendors implement ANSI Standard SQL
• However, SQL Extensions are usually DB specific
• Powerful despite simplicity
ANSI - American National Standards Institute
What’s DB and DB Management System
• What’s a database: A collection of data in an organized way
• Relational DB
• tables
• columns/fields/variables and datatypes
• rows/records/observations
• primary key, foreign key, constraints and relationships (discuss later)
• What is DBMS (DB Management System)?
• A software system that manages/maintains relational DBs
• e.g. MySQL, MariaDB, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, etc.
Connect to a DB and use SQL – DB Client
• DB specific management client
• command-line console
• GUI client (e.g. DB Browser for SQLite, MySQL Workbench, MS SSMS)
• Generic DB client can connect to different DBs through connectors
• GUI client (e.g. DBeaver, Navicat)
• Programming language (e.g. Python + SQLAlchemy + DBAPI (e.g. SQLite,
MySQL, PostgreSQL, etc.), R + dbplyr)
Beyond a relational DB language
• SAS’s PROC SQL
• Spark’s SparkSQL
• Apache Spark is a big data computing framework
• Hive’s HiveQL, an SQL-like query language
• Apache Hive is a distributed data warehouse (data warehouse?)
• Google BigQuery’s SQL
• BigQuery is Google’s data warehouse (analyze petabytes of data at ease)
ref. Database vs data warehouse; Data warehouse vs data lake
note: NoSQL DB?
SQL Hands-on Exercises (Learning-by-doing)
• Course website: https://tdmdal.github.io/mma-sql/
• Google Colab
• Google’s Jupyter Notebook
• A notebook can contain live code, equations, visualizations and narrative text
• Why SQLite?
• a small, fast, self-contained, high-reliability, full-featured, SQL DB engine
• perfect for learning SQL
Preparation For RSM8411 (MMA, Fall 2020)
• A different setup (a more advanced/powerful DBMS)
• Microsoft SQL Server Express, a mini/desktop version of MS SQL Server
• SQL Server Management Studio (SSMS), a GUI client for MS SQL Server
• Get-started resources for this setup: see our bootcamp website
• Please make sure you have the above setup installed
• Set it up before the end of this bootcamp
• Email me if you have trouble with installation
• SQL syntax difference between SQLite and MS SQL
• For 99% of what we will learn in this bootcamp, they are the same
Categories
CategoryID
CategoryName
Description
Picture
Customers
CustomerID
CompanyName
ContactName
ContactTitle
Address
City
Region
PostalCode
Country
Phone
Fax
Employees
EmployeeID
LastName
FirstName
Title
TitleOfCourtesy
BirthDate
HireDate
Address
City
Region
PostalCode
Country
HomePhone
Extension
Photo
Notes
ReportsTo
PhotoPath
OrderDetails
OrderID
ProductID
UnitPrice
Quantity
Discount
Orders
OrderID
CustomerID
EmployeeID
OrderDate
RequiredDate
ShippedDate
ShipVia
Freight
ShipName
ShipAddress
ShipCity
ShipRegion
ShipPostalCode
ShipCountry
Products
ProductID
ProductName
SupplierID
CategoryID
QuantityPerUnit
UnitPrice
UnitsInStock
UnitsOnOrder
ReorderLevel
Discontinued
Shippers
ShipperID
CompanyName
Phone
Suppliers
SupplierID
CompanyName
ContactName
ContactTitle
Address
City
Region
PostalCode
Country
Phone
Fax
HomePage
Northwind DB
Primary key, foreign key, constraints and
relationships
Hands-on Part 1: Warm up
• Retrieve data: SELECT...FROM...
• Sort retrieved data: SELECT...FROM...ORDER BY...
• Filter data: SELECT...FROM...WHERE...
• IN, NOT, LIKE and % wildcard
• Create calculated fields
• mathematical calculations (e.g. +, -, *, /)
• data manipulation functions (e.g. DATE(), ||)
Hands-on Part 2: Summarize and Group Data
• Summarize data using aggregate functions (e.g. COUNT(), MIN(), MAX(),
and AVG()).
• Group data and filter groups: SELECT...FROM...GROUP
BY...HAVING...
• SELECT clause ordering: SELECT...FROM...WHERE...GROUP
BY...HAVING...ORDER BY...
• Filter data by subquery:
SELECT...FROM...WHERE...(SELECT...FROM...)
Hands-on Part 2: Join Tables
• Inner join: SELECT...FROM...INNER JOIN...ON...
• Left join: SELECT...FROM...LEFT JOIN...ON...
• Other join variations.
Join – Inner Join
Table1 Table2
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.pk = Table2.fk;
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
pk t1c1 fk t2c1
1 a 1 c
1 a 1 d
Table1 Table2
Join – Left (Outer) Join
SELECT *
FROM Table1
LEFT JOIN Table2
ON Table1.pk = Table2.fk;
Table1 Table2
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
pk t1c1 fk t2c1
1 a 1 c
1 a 1 d
2 b null null
Table1 Table2
Join - Left (Outer) Join With Exclusion
Table1 Table2
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
Table1 Table2
SELECT *
FROM Table1
LEFT JOIN Table2
ON Table1.pk = Table2.fk
WHERE Table2.fk is NULL;
pk t1c1 fk t2c1
2 b null null
Join – Right Outer Join*
SELECT *
FROM Table2
LEFT JOIN Table1
ON Table2.fk = Table1.pk
---------------------------
SELECT *
FROM Table1
RIGHT JOIN Table2
ON Table1.pk = Table2.fk;
Table2
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
pk t1c1 fk t2c1
1 a 1 c
1 a 1 d
null null 3 e
Table1 Table2
Table1
SQLite doesn’t
support this
RIGHT JOIN key
word, but some
DBMSs do (e.g.
MySQL).
Join - Right Outer Join With Exclusion*
Table1 Table2
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
Table1 Table2
SELECT *
FROM Table2
LEFT JOIN Table1
ON Table2.fk = Table1.pk
WHERE Table1.pk is NULL;
---------------------------
SELECT *
FROM Table1
RIGHT JOIN Table2
ON Table1.pk = Table2.fk
WHERE Table1.pk is NULL;
pk t1c1 fk t2c1
2 b null null
pk t1c1 fk t2c1
null null 3 e
SQLite doesn’t
support this RIGHT
JOIN key word, but
some DBMSs do (e.g.
MySQL).
Join – Full Outer Join
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
Table1 Table2
SELECT pk, t1c1, fk, t2c1
FROM Table1
LEFT JOIN Table2
ON Table1.pk = Table2.fk
UNION
SELECT pk, t1c1, fk, t2c1
FROM Table2
LEFT JOIN Table1
ON Table2.fk = Table1.pk;
Table1 Table2
pk t1c1 fk t2c1
1 a 1 c
1 a 1 d
2 b null null
null null 3 e
Note: Some DBMS support FULL OUTER JOIN keyword (e.g. MS SQL) so you don’t need to do it the above way.
Join – Full Outer Join With Exclusion*
pk t1c1
1 a
2 b
fk t2c1
1 c
1 d
3 e
Table1 Table2
SELECT pk, t1c1, fk, t2c1
FROM Table1
LEFT JOIN Table2
ON Table1.pk = Table2.fk
WHERE Table2.fk is NULL
UNION
SELECT pk, t1c1, fk, t2c1
FROM Table2
LEFT JOIN Table1
ON Table2.fk = Table1.pk
WHERE Table1.pk is NULL;
Table1 Table2
pk t1c1 fk t2c1
2 b null null
null null 3 e
Others
• CTE and temporary table
• Self-join
• CASE keyword
• UNION keyword
Many things we didn’t cover
• Insert data (INSERT INTO…VALUES…; INSERT INTO…SELECT…FROM…)
• Update data (UPDATE…SET…WHERE…)
• Delete data (DELETE FROM…WHERE…)
• Manipulate tables (CREATE TABLE…; ALTER TABLE…; DROP TABLE…)
• Views (CREATE VIEW…AS…)
The list goes on and on
• Stored procedures
• Functions
• Transaction processing
• Cursors (going through table row by row)
• WINDOW function
• Query optimization
• DB permissions & security
• …
Ref. A stack overflow discussion on What is “Advanced” SQL.

More Related Content

Similar to sql_bootcamp.pdf

SQL Tutorial
SQL TutorialSQL Tutorial
SQL Tutorialziamd
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 
Things you should know about Oracle truncate
Things you should know about Oracle truncateThings you should know about Oracle truncate
Things you should know about Oracle truncateKazuhiro Takahashi
 
Oracle goldengate 11g schema replication from standby database
Oracle goldengate 11g schema replication from standby databaseOracle goldengate 11g schema replication from standby database
Oracle goldengate 11g schema replication from standby databaseuzzal basak
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLMariaDB plc
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptxSheethal Aji Mani
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2Alex Zaballa
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2Alex Zaballa
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorialMohd Tousif
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingAmir Reza Hashemi
 
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!Amanda Lam
 

Similar to sql_bootcamp.pdf (20)

SQL
SQLSQL
SQL
 
SQL Tutorial
SQL TutorialSQL Tutorial
SQL Tutorial
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Meetup talk
Meetup talkMeetup talk
Meetup talk
 
IR SQLite Session #3
IR SQLite Session #3IR SQLite Session #3
IR SQLite Session #3
 
Etl2
Etl2Etl2
Etl2
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
Things you should know about Oracle truncate
Things you should know about Oracle truncateThings you should know about Oracle truncate
Things you should know about Oracle truncate
 
Oracle goldengate 11g schema replication from standby database
Oracle goldengate 11g schema replication from standby databaseOracle goldengate 11g schema replication from standby database
Oracle goldengate 11g schema replication from standby database
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorial
 
SQL
SQLSQL
SQL
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / Sharding
 
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
 

Recently uploaded

办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样
办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样
办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样umasea
 
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRCall In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRjennyeacort
 
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call GirlsHi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girlsssuser7cb4ff
 
Limnology and Wetland Management 2023 NaRM.pptx
Limnology and Wetland Management 2023 NaRM.pptxLimnology and Wetland Management 2023 NaRM.pptx
Limnology and Wetland Management 2023 NaRM.pptxTesfahunTesema
 
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full NightCall Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesGwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Servicesnajka9823
 
Environmental and Social Impact Assessment
Environmental and Social Impact AssessmentEnvironmental and Social Impact Assessment
Environmental and Social Impact AssessmentTesfahunTesema
 
Thessaly master plan- WWF presentation_18.04.24.pdf
Thessaly master plan- WWF presentation_18.04.24.pdfThessaly master plan- WWF presentation_18.04.24.pdf
Thessaly master plan- WWF presentation_18.04.24.pdfTheaMan11
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Servicesnajka9823
 
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证jdkhjh
 
ENVIRONMENTAL LAW ppt on laws of environmental law
ENVIRONMENTAL LAW ppt on laws of environmental lawENVIRONMENTAL LAW ppt on laws of environmental law
ENVIRONMENTAL LAW ppt on laws of environmental lawnitinraj1000000
 
Air pollution soli pollution water pollution noise pollution land pollution
Air pollution soli pollution water pollution noise pollution land pollutionAir pollution soli pollution water pollution noise pollution land pollution
Air pollution soli pollution water pollution noise pollution land pollutionrgxv72jrgc
 
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一F dds
 
885MTAMount DMU University Bachelor's Diploma in Education
885MTAMount DMU University Bachelor's Diploma in Education885MTAMount DMU University Bachelor's Diploma in Education
885MTAMount DMU University Bachelor's Diploma in Educationz xss
 
Delivering nature-based solution outcomes by addressing policy, institutiona...
Delivering nature-based solution outcomes by addressing  policy, institutiona...Delivering nature-based solution outcomes by addressing  policy, institutiona...
Delivering nature-based solution outcomes by addressing policy, institutiona...CIFOR-ICRAF
 
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170simranguptaxx69
 
Unit 1 - introduction to environmental studies.pdf
Unit 1 - introduction to environmental studies.pdfUnit 1 - introduction to environmental studies.pdf
Unit 1 - introduction to environmental studies.pdfRajjnish1
 

Recently uploaded (20)

办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样
办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样
办理La Trobe学位证(文凭证书)拉筹伯大学毕业证成绩单原版一模一样
 
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCRCall In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
Call In girls Connaught Place (DELHI)⇛9711147426🔝Delhi NCR
 
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call GirlsHi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
Hi FI Call Girl Ahmedabad 7397865700 Independent Call Girls
 
Limnology and Wetland Management 2023 NaRM.pptx
Limnology and Wetland Management 2023 NaRM.pptxLimnology and Wetland Management 2023 NaRM.pptx
Limnology and Wetland Management 2023 NaRM.pptx
 
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full NightCall Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
Call Girls Ahmedabad 7397865700 Ridhima Hire Me Full Night
 
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesGwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Gwalior Call Girls 7001305949 WhatsApp Number 24x7 Best Services
 
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCREscort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
 
Environmental and Social Impact Assessment
Environmental and Social Impact AssessmentEnvironmental and Social Impact Assessment
Environmental and Social Impact Assessment
 
Thessaly master plan- WWF presentation_18.04.24.pdf
Thessaly master plan- WWF presentation_18.04.24.pdfThessaly master plan- WWF presentation_18.04.24.pdf
Thessaly master plan- WWF presentation_18.04.24.pdf
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
 
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证
原版1:1复刻塔夫斯大学毕业证Tufts毕业证留信学历认证
 
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls ServicesGandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
 
ENVIRONMENTAL LAW ppt on laws of environmental law
ENVIRONMENTAL LAW ppt on laws of environmental lawENVIRONMENTAL LAW ppt on laws of environmental law
ENVIRONMENTAL LAW ppt on laws of environmental law
 
Air pollution soli pollution water pollution noise pollution land pollution
Air pollution soli pollution water pollution noise pollution land pollutionAir pollution soli pollution water pollution noise pollution land pollution
Air pollution soli pollution water pollution noise pollution land pollution
 
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
办理学位证(KU证书)堪萨斯大学毕业证成绩单原版一比一
 
885MTAMount DMU University Bachelor's Diploma in Education
885MTAMount DMU University Bachelor's Diploma in Education885MTAMount DMU University Bachelor's Diploma in Education
885MTAMount DMU University Bachelor's Diploma in Education
 
Delivering nature-based solution outcomes by addressing policy, institutiona...
Delivering nature-based solution outcomes by addressing  policy, institutiona...Delivering nature-based solution outcomes by addressing  policy, institutiona...
Delivering nature-based solution outcomes by addressing policy, institutiona...
 
young call girls in Janakpuri🔝 9953056974 🔝 escort Service
young call girls in Janakpuri🔝 9953056974 🔝 escort Serviceyoung call girls in Janakpuri🔝 9953056974 🔝 escort Service
young call girls in Janakpuri🔝 9953056974 🔝 escort Service
 
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
Call Girls Sarovar Portico Naraina Hotel, New Delhi 9873777170
 
Unit 1 - introduction to environmental studies.pdf
Unit 1 - introduction to environmental studies.pdfUnit 1 - introduction to environmental studies.pdf
Unit 1 - introduction to environmental studies.pdf
 

sql_bootcamp.pdf

  • 1. INTRO TO SQL Bootcamp (https://tdmdal.github.io/mma-sql/) September 21, 2020 Prepared by Jay / TDMDAL
  • 2. What’s SQL (Structured Query Language) • Most widely used database (DB) language • a domain specific language (managing data stored in relational DB) • Not a proprietary language • Open specifications/standards • All major DBMS (DB Mgmt. System ) vendors implement ANSI Standard SQL • However, SQL Extensions are usually DB specific • Powerful despite simplicity ANSI - American National Standards Institute
  • 3. What’s DB and DB Management System • What’s a database: A collection of data in an organized way • Relational DB • tables • columns/fields/variables and datatypes • rows/records/observations • primary key, foreign key, constraints and relationships (discuss later) • What is DBMS (DB Management System)? • A software system that manages/maintains relational DBs • e.g. MySQL, MariaDB, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, etc.
  • 4. Connect to a DB and use SQL – DB Client • DB specific management client • command-line console • GUI client (e.g. DB Browser for SQLite, MySQL Workbench, MS SSMS) • Generic DB client can connect to different DBs through connectors • GUI client (e.g. DBeaver, Navicat) • Programming language (e.g. Python + SQLAlchemy + DBAPI (e.g. SQLite, MySQL, PostgreSQL, etc.), R + dbplyr)
  • 5. Beyond a relational DB language • SAS’s PROC SQL • Spark’s SparkSQL • Apache Spark is a big data computing framework • Hive’s HiveQL, an SQL-like query language • Apache Hive is a distributed data warehouse (data warehouse?) • Google BigQuery’s SQL • BigQuery is Google’s data warehouse (analyze petabytes of data at ease) ref. Database vs data warehouse; Data warehouse vs data lake note: NoSQL DB?
  • 6. SQL Hands-on Exercises (Learning-by-doing) • Course website: https://tdmdal.github.io/mma-sql/ • Google Colab • Google’s Jupyter Notebook • A notebook can contain live code, equations, visualizations and narrative text • Why SQLite? • a small, fast, self-contained, high-reliability, full-featured, SQL DB engine • perfect for learning SQL
  • 7. Preparation For RSM8411 (MMA, Fall 2020) • A different setup (a more advanced/powerful DBMS) • Microsoft SQL Server Express, a mini/desktop version of MS SQL Server • SQL Server Management Studio (SSMS), a GUI client for MS SQL Server • Get-started resources for this setup: see our bootcamp website • Please make sure you have the above setup installed • Set it up before the end of this bootcamp • Email me if you have trouble with installation • SQL syntax difference between SQLite and MS SQL • For 99% of what we will learn in this bootcamp, they are the same
  • 8. Categories CategoryID CategoryName Description Picture Customers CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax Employees EmployeeID LastName FirstName Title TitleOfCourtesy BirthDate HireDate Address City Region PostalCode Country HomePhone Extension Photo Notes ReportsTo PhotoPath OrderDetails OrderID ProductID UnitPrice Quantity Discount Orders OrderID CustomerID EmployeeID OrderDate RequiredDate ShippedDate ShipVia Freight ShipName ShipAddress ShipCity ShipRegion ShipPostalCode ShipCountry Products ProductID ProductName SupplierID CategoryID QuantityPerUnit UnitPrice UnitsInStock UnitsOnOrder ReorderLevel Discontinued Shippers ShipperID CompanyName Phone Suppliers SupplierID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax HomePage Northwind DB
  • 9. Primary key, foreign key, constraints and relationships
  • 10. Hands-on Part 1: Warm up • Retrieve data: SELECT...FROM... • Sort retrieved data: SELECT...FROM...ORDER BY... • Filter data: SELECT...FROM...WHERE... • IN, NOT, LIKE and % wildcard • Create calculated fields • mathematical calculations (e.g. +, -, *, /) • data manipulation functions (e.g. DATE(), ||)
  • 11. Hands-on Part 2: Summarize and Group Data • Summarize data using aggregate functions (e.g. COUNT(), MIN(), MAX(), and AVG()). • Group data and filter groups: SELECT...FROM...GROUP BY...HAVING... • SELECT clause ordering: SELECT...FROM...WHERE...GROUP BY...HAVING...ORDER BY... • Filter data by subquery: SELECT...FROM...WHERE...(SELECT...FROM...)
  • 12. Hands-on Part 2: Join Tables • Inner join: SELECT...FROM...INNER JOIN...ON... • Left join: SELECT...FROM...LEFT JOIN...ON... • Other join variations.
  • 13. Join – Inner Join Table1 Table2 SELECT * FROM Table1 INNER JOIN Table2 ON Table1.pk = Table2.fk; pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e pk t1c1 fk t2c1 1 a 1 c 1 a 1 d Table1 Table2
  • 14. Join – Left (Outer) Join SELECT * FROM Table1 LEFT JOIN Table2 ON Table1.pk = Table2.fk; Table1 Table2 pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e pk t1c1 fk t2c1 1 a 1 c 1 a 1 d 2 b null null Table1 Table2
  • 15. Join - Left (Outer) Join With Exclusion Table1 Table2 pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e Table1 Table2 SELECT * FROM Table1 LEFT JOIN Table2 ON Table1.pk = Table2.fk WHERE Table2.fk is NULL; pk t1c1 fk t2c1 2 b null null
  • 16. Join – Right Outer Join* SELECT * FROM Table2 LEFT JOIN Table1 ON Table2.fk = Table1.pk --------------------------- SELECT * FROM Table1 RIGHT JOIN Table2 ON Table1.pk = Table2.fk; Table2 pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e pk t1c1 fk t2c1 1 a 1 c 1 a 1 d null null 3 e Table1 Table2 Table1 SQLite doesn’t support this RIGHT JOIN key word, but some DBMSs do (e.g. MySQL).
  • 17. Join - Right Outer Join With Exclusion* Table1 Table2 pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e Table1 Table2 SELECT * FROM Table2 LEFT JOIN Table1 ON Table2.fk = Table1.pk WHERE Table1.pk is NULL; --------------------------- SELECT * FROM Table1 RIGHT JOIN Table2 ON Table1.pk = Table2.fk WHERE Table1.pk is NULL; pk t1c1 fk t2c1 2 b null null pk t1c1 fk t2c1 null null 3 e SQLite doesn’t support this RIGHT JOIN key word, but some DBMSs do (e.g. MySQL).
  • 18. Join – Full Outer Join pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e Table1 Table2 SELECT pk, t1c1, fk, t2c1 FROM Table1 LEFT JOIN Table2 ON Table1.pk = Table2.fk UNION SELECT pk, t1c1, fk, t2c1 FROM Table2 LEFT JOIN Table1 ON Table2.fk = Table1.pk; Table1 Table2 pk t1c1 fk t2c1 1 a 1 c 1 a 1 d 2 b null null null null 3 e Note: Some DBMS support FULL OUTER JOIN keyword (e.g. MS SQL) so you don’t need to do it the above way.
  • 19. Join – Full Outer Join With Exclusion* pk t1c1 1 a 2 b fk t2c1 1 c 1 d 3 e Table1 Table2 SELECT pk, t1c1, fk, t2c1 FROM Table1 LEFT JOIN Table2 ON Table1.pk = Table2.fk WHERE Table2.fk is NULL UNION SELECT pk, t1c1, fk, t2c1 FROM Table2 LEFT JOIN Table1 ON Table2.fk = Table1.pk WHERE Table1.pk is NULL; Table1 Table2 pk t1c1 fk t2c1 2 b null null null null 3 e
  • 20. Others • CTE and temporary table • Self-join • CASE keyword • UNION keyword
  • 21. Many things we didn’t cover • Insert data (INSERT INTO…VALUES…; INSERT INTO…SELECT…FROM…) • Update data (UPDATE…SET…WHERE…) • Delete data (DELETE FROM…WHERE…) • Manipulate tables (CREATE TABLE…; ALTER TABLE…; DROP TABLE…) • Views (CREATE VIEW…AS…)
  • 22. The list goes on and on • Stored procedures • Functions • Transaction processing • Cursors (going through table row by row) • WINDOW function • Query optimization • DB permissions & security • … Ref. A stack overflow discussion on What is “Advanced” SQL.