Presentation is loading. Please wait.

Presentation is loading. Please wait.

There’s a particular style to it… Rob Hatton

Similar presentations


Presentation on theme: "There’s a particular style to it… Rob Hatton"— Presentation transcript:

1 There’s a particular style to it… Rob Hatton Email: rob@convitali.com

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 Doesn’t even know about variables It’s awesome at retrieving data!

3 SQL Server has 2 languages ANSI SQL Declarative Based on Sets TSQL Procedural We’ll be talking about ANSI SQL Focus on sets is the most important concept of the day!

4 Sets They’re just collections of values Examples: Tables Views Sub-queries Function return Common Table Expressions (sort of)

5 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 The complete solution does both It’s a comic book

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

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.

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

10 Aliasing select fis.orderDateKey, max(fis.salesAmount) as maxAmt from factInternetSales fis group by fis.orderDateKey Table Alias: fis Field Alias: maxAmt

11 Select examples select 'just some text' as txt select getdate() as dt select City from DimGeography select lastName from vTargetMail select color from (select 'blue' as color) as colors *

12 Joins 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! *

13 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 *

14 Union Combine 2 sets with matching fields 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 *

15 Case when Two versions Case When expression Case when Expression Alternative Union

16 Correlated Subquery A necessary evil Appears in select list Subquery contains a join referencing the outer query Runs once for each record! Alternatives Join Creating a temp record set *

17 Nulls What size dress does George wear? Missing or inapplicable data Nulls in any relational operation result in null Nulls in string concatenation result in null When testing for True, nulls appear false Special functions Is null (in WHERE clause) Isnull() Coalesce()

18 Cursors Procedural Slow More code than SQL Can be the only solution *

19 Recursive SQL Use a CTE Create an anchor condition Union All Query again calling anchor Choose what you want outside the CTE *

20 It’s important to understand data! Query system tables Profile data *

21 There’s a particular style to it… Rob Hatton Email: rob@convitali.com


Download ppt "There’s a particular style to it… Rob Hatton"

Similar presentations


Ads by Google