2. What is SQL
SQL stands for
Structured
Query
Language
● It is used to access, manipulate and
process data in databases.
3. What is a database?
A database is a place that stores data, derived
information and process logic.
Common Forms:
● RDBMS - Relational Database Management
System
● NoSQL - a grab bag of multiple systems to
store data
4. Parts of a RDBMS
● Database / Catalog
● Schema
● Table/ Indices
● View
● Row / Record
● Field / Column
● Stored Procedures/ Functions
5. Column / Field
A column (or field) is a single piece of data, that
has a defined type and a set purpose
● Text
● Number
● more complex types depend on the RDBMS
6. Record / Row
A record is a collection of fields grouped
together to provide context to the data
contained
7. Table / Indices
A table is a collection of records which set
operations can be applied to
8. So what?
Why is this useful, a database provides a means
to represent real world models and hence
understand through analysis more about the
system modelled
10. What were those join-ey
things
● They can relate tables to each other
● Provide links to information for context
● Minimize storage
11. Database Forms
First Normal Form
● Eliminate repeating groups in individual
tables.
● Create a separate table for each set of related
data.
● Identify each set of related data with a
primary key.
12. Database Forms
Second Normal Form
● Create separate tables for sets of values that
apply to multiple records.
● Relate these tables with a foreign key.
14. So how do I get data?
SELECT statment
● Allows you to SELECT data FROM several
tables WHERE certain constraints exist, and
GROUP BY certain fields, it can be ORDER
BY other fields
22. Recursive CTE...
WITH empTree AS (
SELECT …, 0 AS level
FROM employee
UNION ALL
SELECT …, level + 1 AS level
FROM employee INNER JOIN empTree ON ...
23. PIVOT and UNPIVOT
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
24. LAG
SELECT BusinessEntityID,
YEAR(QuotaDate) AS SalesYear,
SalesQuota AS CurrentQuota,
LAG(SalesQuota, 1,0)
OVER (ORDER BY YEAR(QuotaDate)) AS PreviousQuota
FROM Sales.SalesPersonQuotaHistory
WHERE BusinessEntityID = 275 and YEAR(QuotaDate) IN
('2005','2006');