Download presentation
Presentation is loading. Please wait.
1
There’s a particular style to it…
SQL Style There’s a particular style to it… Rob Hatton
2
SQL is not a complete language
No way to build a user interface No way to read or write to a file Can’t print Can’t control how things happen It’s awesome at retrieving data!
3
SQL Server has 2 languages
ANSI SQL Declarative TSQL Procedural We’ll be talking about ANSI SQL Focus on sets is the most important concept of the day!
4
SQL is a different kind of language
Most languages are procedural Like telling a story It’s the reason for TSQL SQL is a declarative language Like painting a picture Based on Sets It’s conceptually intertwined with Relational Database structure. Sets are key to understanding SQL
5
Sets They’re just collections of values: Database Examples:
Customers that owe you money Students & grades Cities and temperatures Database Examples: Tables Views Sub-queries Function return Common Table Expressions (sort of)
6
Types Of SQL Statements
Data Definition Language (DDL) Create (Table, View, Proc) Alter (Table, View, Proc) Drop (Table, View, Proc) Data Manipulation Language (DML) Select Insert Delete Update Database definition is stored in a database. DDL manipulates the definition. We’re not going to focus on DDL.
7
Select Syntax SELECT field_list [ FROM set_source ]
(includes joins) [ WHERE search_expression ] (record based) [ GROUP BY group_expression ] [ HAVING aggregate_expression ] [ ORDER BY order_expression [ ASC | DESC ] ] Where clause logic is record based, not set based! ‘Where eyeColor = ‘blue’ and eyeColor = ‘green’ returns no records. ‘Group by’ requires an aggregate function in select list. ‘Having’ only works with group by. Where clause logic is record based, not set based! ‘Where eyeColor = ‘blue’ and eyeColor = ‘green’ returns no records. ‘Group by’ requires an aggregate function in select list. ‘Having’ only works with group by.
8
Functions and Expressions
UPPER() - Converts a field to upper case SUBSTR() - Returns part of a character expression LTRIM() – Remove spaces from left end of text field LEN() - Returns the length of a text field GETDATE() - Returns the system date and time CHARINDEX() – Returns start of string in another string Case When Beware of misuse
9
Comparison Operators = < <= > <> ! (not) Between Exists
In Is [not] null Remember, they are record based not set based!
10
The importance of NULL NULL is missing data
NULL is not the same as zero Needed to calculate valid aggregates Example: When calculating average dress size, it keeps the guys from spoiling the average Anything combined with a NULL is NULL Any relational operator using a NULL returns NULL The ISNULL() function helps deal with NULLs
11
Aliasing select fis.orderDateKey , max(fis.salesAmount) as maxAmt
from factInternetSales fis group by fis.orderDateKey Table Alias: fis Field Alias: maxAmt
12
Stuff you’ll want * Entity Relationship Diagram (ERD)
Picture of database structure Shows Primary/Foreign Keys Management studio Object Explorer Intellisence Drag and drop field names *
13
It’s important to understand data!
Query system tables Profile data --Profile varchar(700) varchar(50) varchar(50) varchar(100) = '' = '' varchar(200) DROP TABLE [dbo].[dbProfile] IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dbProfile]') AND type in (N'U')) --Recreate the profile table CREATE TABLE [dbo].[dbProfile]( [recordCnt] [int] NULL, [ColumnName] [varchar](50) NOT NULL, [TableName] [varchar](50) NOT NULL, [distinctCnt] [int] NULL, [fldSample] varchar(300) [NullCnt] [int] NULL, [nonNullCnt] [int] NULL, ) ON [PRIMARY] SELECT t.name AS table_name, declare dbSchema cursor for c.name AS column_name ORDER BY table_name INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID FROM sys.tables AS t while = 0 fetch next from open dbSchema begin EXEC = 'declare fldList cursor for select distinct ' + ' from ' open fldList fetch next from fldList while = 0 and < 200 + + ', ' end deallocate fldList close fldList = 'insert into dbProfile (TableName, ColumnName, recordCnt, distinctCnt, nonNullCnt, nullCnt, fldSample) ' +'count(distinct as distinctCnt, ' +'count(*) as recordCnt, ' +'select as TableName as ColumnName, ' + '''' + + '''' +'sum(case when 0 then 1 else 0 end) as NullCnt, ' +'sum(case when 0 then 0 else 1 end) as nonNullCnt, ' +' from ' begin try end try end catch begin catch deallocate dbSchema close dbSchema select * from dbProfile *
14
Joins Based on values in one table (usually a primary key) matching values in another table (usually a foreign key) Types Inner Outer Left Right Cross Full Self Exists Joins work with any comparison operator! Joins aren’t as much of a problem as data size! --Example Queries go use Fruit select * from fruit select * from colors from fruit select * --Use 'where' clause where colorKey = 1 --Dealing with null where colorKey is null --where colorKey = null --Wildcard --where fruitName like '%e ' where fruitName like '%e%' --where rtrim(fruitName) like '%e' --'In' clause where fruitName in ('apple','grape') --In with a subquery where fruitName in (select fruitName from fruit where rtrim(fruitName) like '%e') --Everything from inner join from fruit f --Relevent columns from inner join on f.colorKey = c.colorKey inner join colors c select f.fruitName, c.color --Everything from left outer join left outer join colors c --Everything from right outer join right outer join colors c --Everything from Cross join (match everything with everything - no 'on' condition) --on f.colorKey = c.colorKey cross join colors c --Everything from full join (combination of left & right outer joins) full join colors c select fruitName --Check for Exists (select * where exists from colors c --Alternate version of Exists where c.colorKey = f.colorKey) where f.colorKey is not null on c.colorKey = f.colorKey select colorKey, count(*) as cnt --Aggregations having count(*) > 1 group by colorKey use [AdventureWorksDW] --Self join from DimAccount c select c.AccountDescription, p.AccountDescription order by c.accountKey on p.ParentAccountKey = c.AccountKey left outer join DimAccount p *
15
Demo Database Fruit Database Colors table Fruit table Red Yellow Green
Orange Fruit table Banana Grape Apple Eaten
16
Common Table Expressions
CTE Create a set on the fly Can only be used in a single query Doesn’t actually create the set Think of it like an include Optimizer doesn’t see a separate set --Common Table Expressions with northAmerica as (select [SalesTerritoryKey], [SalesTerritoryRegion] from DimSalesTerritory where [SalesTerritoryGroup] = 'North America') select top 3 na.SalesTerritoryRegion, fis.SalesAmount from FactInternetSales fis inner join northAmerica na on na.SalesTerritoryKey = fis.SalesTerritoryKey *
17
Union * Combine 2 sets with matching fields Useful in replacing Case
Field count must match exactly Data types must be implicitly convertible First query dictates names Useful in replacing Case Implicitly removes duplicates Use ‘Union All’ to preserve duplicates --Union use [AdventureWorksDW2012] Go select distinct st.SalesTerritoryRegion from FactInternetSales fis inner join DimSalesTerritory st on fis.SalesTerritoryKey = st.SalesTerritoryKey union from FactResellerSales frs on frs.SalesTerritoryKey = st.SalesTerritoryKey *
18
There’s a particular style to it…
SQL Style There’s a particular style to it… Rob Hatton
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.