SlideShare une entreprise Scribd logo
1  sur  29
Introduction toSQL
Ref: https://www.w3schools.com/sql/default.asp
What isSQL?
 SQL stands for Structured Query Language
 SQL lets you access and manipulate databases
 SQL is anANSI (American National Standards Institute) standard
WhatCanSQL
do?
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
SQL is a
Standard -
BUT....
 There are different versions of the SQL language
 They all support at least the major commands
 Note: Most of the SQL database programs also have their own
proprietary extensions in addition to the SQL standard!
What is
RDBMS?
 RDBMS stands for Relational Database Management System.
 RDBMS is the basis for SQL, and for all modern database systems
such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.
 The data in RDBMS is stored in database objects called tables. A
table is a collection of related data entries and it consists of
columns and rows.
 Look at the "Customers" table:
SELECT * FROM Customers;
SQLSyntax
 DatabaseTables
Columns or Fields, Rows or Records, Indexes, etc…
 SQL Statements
SELECT * FROM Customers;
It’s not Case Sensitive
 SQL Expressions
Column Names, Formula, Sub-Query, etc…
 Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL statement
 Some ofThe Most Important SQLCommands
 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
SQLSELECT
Statement
The SELECT statement is used to select data from a database
SELECT
Syntax
SELECT column1, column2, ...
FROM table_name;
SELECT *
FROM table_name;
SELECT CustomerKey, FirstName, LastName
FROM DimCustomer;
MoreSELECT
Stuff
 TOP N
 ORDER BY
 ORDER BY DESC
 DISTINCT
 Alias Names
 Concatenation
SELECT
WHERE
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT *
FROM DimCustomer
WHERE Gender = ‘F’;
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
MoreWHERE
Stuff
 Dealing with NULL values
 AND, OR, NOT
 LIKE
 %
 _
 IN
SELECT
WHERE
Quiz
 Female Customers
 Female Customers younger than 40
 Female Customers younger than 40 and married
 MaleCustomers with middle name
 MaleCustomers who their names start with ‘Jon’
 Customers with income between 30000-50000
 Single female customers between 25-40 years old how make more
than $100K a year
Aggregation
 MIN
 MAX
 AVG
 COUNT
 SUM
 COUNT(DISTINCT)
GROUP BY  After WHERE Clause
 Before Order By Clause
 HAVING Clause
 Aggregations in GROUP BY
SELECT column1, column2, ...
FROM table_name
WHERE condition
GROUP BY column1, column2, ...;
GROUP BY
Quiz
 Customers by Gender
 Customers by Marital Status
 Customers by Age
 Customer Count by Gender
 Customer Count by Gender and Marital Status
 Average Income by Gender
 Female Customers count in 25-40 years old by Age
 …
SQLJOINs
 When do we join?
 Different types of JOINs
 CROSS JOIN
 How to choose which type is appropriate?
 Deal with NULL values
INNER JOIN
SELECT DimCustomer.CustomerKey, DimCustomer.FirstName,
DimCustomer.LastName,
DimGeography.EnglishCountryRegionName, DimGeography.City
FROM DimCustomer INNER JOIN
DimGeography ON DimCustomer.GeographyKey =
DimGeography.GeographyKey
SELECT DimGeography.EnglishCountryRegionName,
DimGeography.City, Customer.CustomerKey,
Customer.FirstName, Customer.LastName,
Sales.SalesOrderNumber, Sales.SalesAmount
FROM FactInternetSales Sales INNER JOIN
DimCustomer Customer ON Customer.CustomerKey =
Sales.CustomerKey INNER JOIN
DimGeography ON Customer.GeographyKey =
DimGeography.GeographyKey
LEFT/RIGHT
JOIN
SELECT P.EnglishProductName, PSC.EnglishProductSubcategoryName
FROM DimProductSubcategory PSC LEFT OUTER JOIN
DimProduct P ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey
SELECT P.EnglishProductName, PSC.EnglishProductSubcategoryName
FROM DimProductSubcategory PSC RIGHT OUTER JOIN
DimProduct P ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey
FULLJOIN
SELECT *
FROM DimProductCategory PC FULL OUTER JOIN
DimProductMarginTarget PCT ON PC.ProductCategoryKey =
PCT.ProductCategoryKey
SQLJOINs
Quiz
 Join DimProduct, DimProductSubcategory, DimProductCategory and
select Keys and Names from each table
 Join FactInternetSales, DimCustomer, DimProduct and show which
Product is Sold toWhich Customer with all the measures
 ProductCount by Category
 SalesAmount By Country
 OrderQuantity by Product Color
 SalesAmount and OrderQuantity byYear, Country, ProductCategory
 How many Bikes have we sold to each Country in 1386
 SalesAmount by Customer Gender
 SalesAmount by Customer Gender, MaritalStatus in the US
 Which one bought more Accessories, Men orWomen?
Sub-Queries
 Select Statements within Select Statement
 Used in:
 SELECT Clause
 FROM Clause
 JOINs
 WHERE Clause
Sub-Queries in
SELECT
 One row, One column
 Relates to the main query by usingWHERE
 Needs an Alias Name
 Could use internal joins as many times as needed
SELECT P.ProductKey, P.EnglishProductName, (
SELECT SUM(S.OrderQuantity) FROM FactInternetSales S WHERE
S.ProductKey = P.ProductKey
) AS SalesQuantity
FROM DimProduct P
Sub-Queries in
FROM
 Multiple rows, Multiple columns
 Treated just like aTable
 Needs an Alias Name
 Could use internal joins as many times as needed
SELECT MonthlySales.Year, Sum(TotalSales) ASTotalSalesSummary,
Min(TotalSales) ASTotalSalesMinimum, Max(TotalSales) AS
TotalSalesMaximum, Avg(TotalSales) ASTotalSalesAvg
FROM (
SELECT D.Year, D.MonthKey, Sum(OrderQuantity) ASTotalSales
FROM FactInternetSales S INNER JOIN
DimDatePersian D ON S.OrderDateKey = D.DateKey
Group By D.Year, D.MonthKey
) MonthlySales
Group By MonthlySales.Year
Sub-Queries in
JOINs
 Same as FROM Sub-Queries
 Relate to each other with JOIN
SELECT PRODUCTS.ProductCategoryKey,
PRODUCTS.EnglishProductCategoryName, SUM(OrderQuantity) AS
TotalSales
FROM FactInternetSales S INNER JOIN
(
SELECT PC.ProductCategoryKey, PC.EnglishProductCategoryName,
P.ProductKey
FROM DimProduct P INNER JOIN
DimProductSubCategory PSC ON P.ProductSubcategoryKey =
PSC.ProductSubcategoryKey INNER JOIN
DimProductCategory PC ON PC.ProductCategoryKey =
PSC.ProductCategoryKey
) PRODUCTS ON PRODUCTS.ProductKey = S.ProductKey
GROUP BY PRODUCTS.ProductCategoryKey,
PRODUCTS.EnglishProductCategoryName
Sub-Queries in
FROM
 Multiple rows, One column
 Usually used for IN or NOT IN conditions
 Doesn’t need an Alias Name
 Could use internal joins as many times as needed
SELECT *
FROM FactInternetSales
WHERE ProductKey IN (
SELECT P.ProductKey
FROM DimProduct P INNER JOIN
DimProductSubCategory PSC ON P.ProductSubcategoryKey =
PSC.ProductSubcategoryKey INNER JOIN
DimProductCategory PC ON PC.ProductCategoryKey =
PSC.ProductCategoryKey
WHERE PC.ProductCategoryKey = 1
)
CROSSJOIN
 Cartesian product of theTables involved in the JOIN
 Result rows count =Table1.Rows xTable2.Rows x …
 Used for computing Measures from unrelated Dimensions
 If aWHERE clause is added, the cross join behaves as an inner join
SELECT CUSTOMERS.CustomerKey, PRODUCTS.ProductKey,
(
SELECT ISNULL(SUM(S.OrderQuantity), 0) FROM FactInternetSales S
WHERE S.ProductKey = PRODUCTS.ProductKey and S.CustomerKey =
CUSTOMERS.CustomerKey
)TotalSales
FROM (
SELECT TOP(10) CustomerKey FROM DimCustomer
) CUSTOMERS CROSS JOIN
(
SELECT TOP(10) ProductKey FROM DimProduct ORDER BY ListPrice DESC
) PRODUCTS
ORDER BYTotalSales DESC
Sub-Queries
Quiz
 Select Customers withTotal Purchased Amount
 Select only the Products which have been sold
 Find out OrderQuantity sold in each country from each product
color
 Show monthly SalesAmount distribution in each country
 CombineYears and Customer Gender, and find each combination
Sales
 Yearly Sales for Customers in the US (Use Sub-Query not JOIN)
About Me
Amin Choroomi
CTO & Co-Founder at vdash
Software Developer, Teacher and Consultant
DataVisualization, Analytics, Dashboards
Data Warehousing, Integration, Business Intelligence
http://www.vdash.ir
choroomi@live.com
choroomi@vdashonline.com
https://linkedin.com/in/choroomi
@aminchoroomi
ThankYou

Contenu connexe

Tendances (20)

SQL
SQLSQL
SQL
 
Computer Science:Java jdbc
Computer Science:Java jdbcComputer Science:Java jdbc
Computer Science:Java jdbc
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 

Similaire à Introduction to SQL

Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic ConceptsTony Wong
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresSteven Johnson
 
Practical guide to SQL basics
Practical guide to SQL basicsPractical guide to SQL basics
Practical guide to SQL basicsPavani Ganti
 
Sql basics
Sql basicsSql basics
Sql basicsKumar
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for BeginnersProduct School
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)iceolated
 
Database optimization
Database optimizationDatabase optimization
Database optimizationEsraaAlattar1
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
James Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science PortfolioJames Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science Portfoliocolbydaman
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfoliogregmlewis
 
Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Gyanendra Kumar
 
SSRS Report with Parameters and Data Filtration
SSRS Report with Parameters and Data FiltrationSSRS Report with Parameters and Data Filtration
SSRS Report with Parameters and Data FiltrationNaji El Kotob
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 

Similaire à Introduction to SQL (20)

Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome Measures
 
Practical guide to SQL basics
Practical guide to SQL basicsPractical guide to SQL basics
Practical guide to SQL basics
 
Sql basics
Sql basicsSql basics
Sql basics
 
Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)
 
Database optimization
Database optimizationDatabase optimization
Database optimization
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
James Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science PortfolioJames Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science Portfolio
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfolio
 
Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01
 
SQL for ETL Testing
SQL for ETL TestingSQL for ETL Testing
SQL for ETL Testing
 
SSRS Report with Parameters and Data Filtration
SSRS Report with Parameters and Data FiltrationSSRS Report with Parameters and Data Filtration
SSRS Report with Parameters and Data Filtration
 
Sq lite module6
Sq lite module6Sq lite module6
Sq lite module6
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 

Dernier

办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...ssuserf63bd7
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSINGmarianagonzalez07
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 

Dernier (20)

办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 

Introduction to SQL

  • 2. What isSQL?  SQL stands for Structured Query Language  SQL lets you access and manipulate databases  SQL is anANSI (American National Standards Institute) standard
  • 3. WhatCanSQL do?  SQL can execute queries against a database  SQL can retrieve data from a database  SQL can insert records in a database  SQL can update records in a database  SQL can delete records from a database  SQL can create new databases  SQL can create new tables in a database  SQL can create stored procedures in a database  SQL can create views in a database  SQL can set permissions on tables, procedures, and views
  • 4. SQL is a Standard - BUT....  There are different versions of the SQL language  They all support at least the major commands  Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!
  • 5. What is RDBMS?  RDBMS stands for Relational Database Management System.  RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.  The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.  Look at the "Customers" table: SELECT * FROM Customers;
  • 6. SQLSyntax  DatabaseTables Columns or Fields, Rows or Records, Indexes, etc…  SQL Statements SELECT * FROM Customers; It’s not Case Sensitive  SQL Expressions Column Names, Formula, Sub-Query, etc…  Semicolon after SQL Statements? Some database systems require a semicolon at the end of each SQL statement  Some ofThe Most Important SQLCommands  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database
  • 7. SQLSELECT Statement The SELECT statement is used to select data from a database
  • 8. SELECT Syntax SELECT column1, column2, ... FROM table_name; SELECT * FROM table_name; SELECT CustomerKey, FirstName, LastName FROM DimCustomer;
  • 9. MoreSELECT Stuff  TOP N  ORDER BY  ORDER BY DESC  DISTINCT  Alias Names  Concatenation
  • 10. SELECT WHERE SELECT column1, column2, ... FROM table_name WHERE condition; SELECT * FROM DimCustomer WHERE Gender = ‘F’; Operator Description = Equal <> Not equal. Note: In some versions of SQL this operator may be written as != > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN To specify multiple possible values for a column
  • 11. MoreWHERE Stuff  Dealing with NULL values  AND, OR, NOT  LIKE  %  _  IN
  • 12. SELECT WHERE Quiz  Female Customers  Female Customers younger than 40  Female Customers younger than 40 and married  MaleCustomers with middle name  MaleCustomers who their names start with ‘Jon’  Customers with income between 30000-50000  Single female customers between 25-40 years old how make more than $100K a year
  • 13. Aggregation  MIN  MAX  AVG  COUNT  SUM  COUNT(DISTINCT)
  • 14. GROUP BY  After WHERE Clause  Before Order By Clause  HAVING Clause  Aggregations in GROUP BY SELECT column1, column2, ... FROM table_name WHERE condition GROUP BY column1, column2, ...;
  • 15. GROUP BY Quiz  Customers by Gender  Customers by Marital Status  Customers by Age  Customer Count by Gender  Customer Count by Gender and Marital Status  Average Income by Gender  Female Customers count in 25-40 years old by Age  …
  • 16. SQLJOINs  When do we join?  Different types of JOINs  CROSS JOIN  How to choose which type is appropriate?  Deal with NULL values
  • 17. INNER JOIN SELECT DimCustomer.CustomerKey, DimCustomer.FirstName, DimCustomer.LastName, DimGeography.EnglishCountryRegionName, DimGeography.City FROM DimCustomer INNER JOIN DimGeography ON DimCustomer.GeographyKey = DimGeography.GeographyKey SELECT DimGeography.EnglishCountryRegionName, DimGeography.City, Customer.CustomerKey, Customer.FirstName, Customer.LastName, Sales.SalesOrderNumber, Sales.SalesAmount FROM FactInternetSales Sales INNER JOIN DimCustomer Customer ON Customer.CustomerKey = Sales.CustomerKey INNER JOIN DimGeography ON Customer.GeographyKey = DimGeography.GeographyKey
  • 18. LEFT/RIGHT JOIN SELECT P.EnglishProductName, PSC.EnglishProductSubcategoryName FROM DimProductSubcategory PSC LEFT OUTER JOIN DimProduct P ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey SELECT P.EnglishProductName, PSC.EnglishProductSubcategoryName FROM DimProductSubcategory PSC RIGHT OUTER JOIN DimProduct P ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey
  • 19. FULLJOIN SELECT * FROM DimProductCategory PC FULL OUTER JOIN DimProductMarginTarget PCT ON PC.ProductCategoryKey = PCT.ProductCategoryKey
  • 20. SQLJOINs Quiz  Join DimProduct, DimProductSubcategory, DimProductCategory and select Keys and Names from each table  Join FactInternetSales, DimCustomer, DimProduct and show which Product is Sold toWhich Customer with all the measures  ProductCount by Category  SalesAmount By Country  OrderQuantity by Product Color  SalesAmount and OrderQuantity byYear, Country, ProductCategory  How many Bikes have we sold to each Country in 1386  SalesAmount by Customer Gender  SalesAmount by Customer Gender, MaritalStatus in the US  Which one bought more Accessories, Men orWomen?
  • 21. Sub-Queries  Select Statements within Select Statement  Used in:  SELECT Clause  FROM Clause  JOINs  WHERE Clause
  • 22. Sub-Queries in SELECT  One row, One column  Relates to the main query by usingWHERE  Needs an Alias Name  Could use internal joins as many times as needed SELECT P.ProductKey, P.EnglishProductName, ( SELECT SUM(S.OrderQuantity) FROM FactInternetSales S WHERE S.ProductKey = P.ProductKey ) AS SalesQuantity FROM DimProduct P
  • 23. Sub-Queries in FROM  Multiple rows, Multiple columns  Treated just like aTable  Needs an Alias Name  Could use internal joins as many times as needed SELECT MonthlySales.Year, Sum(TotalSales) ASTotalSalesSummary, Min(TotalSales) ASTotalSalesMinimum, Max(TotalSales) AS TotalSalesMaximum, Avg(TotalSales) ASTotalSalesAvg FROM ( SELECT D.Year, D.MonthKey, Sum(OrderQuantity) ASTotalSales FROM FactInternetSales S INNER JOIN DimDatePersian D ON S.OrderDateKey = D.DateKey Group By D.Year, D.MonthKey ) MonthlySales Group By MonthlySales.Year
  • 24. Sub-Queries in JOINs  Same as FROM Sub-Queries  Relate to each other with JOIN SELECT PRODUCTS.ProductCategoryKey, PRODUCTS.EnglishProductCategoryName, SUM(OrderQuantity) AS TotalSales FROM FactInternetSales S INNER JOIN ( SELECT PC.ProductCategoryKey, PC.EnglishProductCategoryName, P.ProductKey FROM DimProduct P INNER JOIN DimProductSubCategory PSC ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey INNER JOIN DimProductCategory PC ON PC.ProductCategoryKey = PSC.ProductCategoryKey ) PRODUCTS ON PRODUCTS.ProductKey = S.ProductKey GROUP BY PRODUCTS.ProductCategoryKey, PRODUCTS.EnglishProductCategoryName
  • 25. Sub-Queries in FROM  Multiple rows, One column  Usually used for IN or NOT IN conditions  Doesn’t need an Alias Name  Could use internal joins as many times as needed SELECT * FROM FactInternetSales WHERE ProductKey IN ( SELECT P.ProductKey FROM DimProduct P INNER JOIN DimProductSubCategory PSC ON P.ProductSubcategoryKey = PSC.ProductSubcategoryKey INNER JOIN DimProductCategory PC ON PC.ProductCategoryKey = PSC.ProductCategoryKey WHERE PC.ProductCategoryKey = 1 )
  • 26. CROSSJOIN  Cartesian product of theTables involved in the JOIN  Result rows count =Table1.Rows xTable2.Rows x …  Used for computing Measures from unrelated Dimensions  If aWHERE clause is added, the cross join behaves as an inner join SELECT CUSTOMERS.CustomerKey, PRODUCTS.ProductKey, ( SELECT ISNULL(SUM(S.OrderQuantity), 0) FROM FactInternetSales S WHERE S.ProductKey = PRODUCTS.ProductKey and S.CustomerKey = CUSTOMERS.CustomerKey )TotalSales FROM ( SELECT TOP(10) CustomerKey FROM DimCustomer ) CUSTOMERS CROSS JOIN ( SELECT TOP(10) ProductKey FROM DimProduct ORDER BY ListPrice DESC ) PRODUCTS ORDER BYTotalSales DESC
  • 27. Sub-Queries Quiz  Select Customers withTotal Purchased Amount  Select only the Products which have been sold  Find out OrderQuantity sold in each country from each product color  Show monthly SalesAmount distribution in each country  CombineYears and Customer Gender, and find each combination Sales  Yearly Sales for Customers in the US (Use Sub-Query not JOIN)
  • 28. About Me Amin Choroomi CTO & Co-Founder at vdash Software Developer, Teacher and Consultant DataVisualization, Analytics, Dashboards Data Warehousing, Integration, Business Intelligence http://www.vdash.ir choroomi@live.com choroomi@vdashonline.com https://linkedin.com/in/choroomi @aminchoroomi