An introduction to SQL standard language for beginners and non-technical information people. Mostly covers SELECT statement using standard clauses, Joins, Sub-Queries and ...
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
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
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
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
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?
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