SlideShare a Scribd company logo
1 of 30
Transact SQL (T-SQL)
for Beginners
(A New Hope)
What is it and how can I use it?
This is for people who are new to T-SQL.
We are covering SELECT, FROM, WHERE,
GROUP BY, HAVING, ORDER BY and How to
do a Backup.
About Andrea:
 Once upon a time in a kingdom not far away, Andrea was sick of
answering phones and filing forms. Her kingdom was running low on
gold and the wise wizard told her to find work that she loved and
she would never truly work a day in her life. When she found the
magical SQL Server, she knew it was the calling for her. She is
passionate about SQL Server, “works” as a Database Administrator,
is learning Sign Language, loves her #SQLFamily and finds joy in
helping others find their passion too.
 Wife to Ryan Allred and mom to two awesome teenagers.
 Loves Music, concerts, guitars, snowboarding, bouldering and acting.
 16 years of SQL Server experience
 SQL SLC User group Chapter Leader
 Allred_andrea@yahoo.com
 @RoyalSQL, www.RoyalSQL.com
What is T-SQL?
 SQL is Structured Query Language
 Transact Structured Query Language
 Microsoft Standard
 PL-SQL is Oracle
Let’s Start at the Very Beginning
 SELECT – what I want
 FROM – where I get it
 WHERE – only if it matches what I want
 GROUP BY – summarize it for me
 HAVING – used after a group by to filter
 ORDER BY – sort it the way I want it
How is it like English?
 I buy milk from the store if it is chocolate.
 SELECT – I buy milk
 FROM – from the store
 WHERE – if it is chocolate.
 SELECT milk FROM store WHERE flavor =
chocolate
SELECT- Use it Wisely
 SELECT * is a quick way to see all the
fields.
 I like to use a SELECT TOP 5 when using *
 When writing code or reports, always list
out the fields (SELECT DisplayName,
Location, Reputation, UpVotes)
 Know what is being selecting from and
how many rows to expect.
FROM – Show me the Data!
 FROM a table name (Users)
 Must also include the schema name
(dbo.Users)
 Can connect multiple tables using joins,
we will do that later.
 SELECT TOP 5 *
FROM StackOverflow.dbo.Users
WHERE – Cutting out the data that
you don’t need…one clause at a
time.
 Bringing back too much data will hurt
performance
 “WHERE” brings back only what you need
 Non-Mutants can only look at so much
data and have it be useful.
Building a Where Clause
 Column, operator, what is being
comparing against.
 WHERE DisplayName = ‘Anon’
 I only want people that have the Display
Name of Anon.
Basic Operators
(Can it really be this simple?)
 = Equals – It’s all the same.
 WHERE DisplayName = 'Anon'
 <> or != Does Not Equal – Not a match.
 WHERE DisplayName != 'Anon'
 WHERE DisplayName <> 'Anon'
Basic Operators Continued
 > Greater Than – The column has more
 WHERE UpVotes > 23793
 >= Greater Than or Equal to
 WHERE UpVotes >= 23793
 < Less Than – The column has less
 WHERE UpVotes < 45
 <= Less Than or Equal to
 WHERE UpVotes <= 45
When the Basics Won’t Do…
Super Cool Operators
 LIKE – It’s like you know
 WHERE DisplayName LIKE 'Anon%'
 NOT LIKE – It’s not like that
 WHERE DisplayName NOT LIKE 'Anon%'
Super Cool Operators
Continued
 IN – Is it in this?
 WHERE DisplayName IN ('Anon', 'Anonymous',
'Anonymoose')
 NOT IN – It’s soooo not in that.
 WHERE DisplayName IN ('Anon', 'Anonymous',
'Anonymoose')
Super Cool Operators
Continued
 BETWEEN – Stuck in the middle
 WHERE UpVotes BETWEEN 5000 AND 5045
 NOT BETWEEN – It’s outside of that.
 WHERE UpVotes NOT BETWEEN 5000 AND 5045
GROUP BY – Let’s Get Together
 Gathers similar Data together
 Used to create Aggregates
 Awesome for Totals, Averages and Counts
 GROUP BY Columns that you don’t want to
Sum, Count or Average
 Can have multiple columns in GROUP BY
 GROUP BY [DisplayName],[Location]
Having – What do you Have?
 Works with a GROUP BY
 This is another way to filter out data.
 Can have multiple columns in HAVING
Clause.
 HAVING COUNT(Id) > 50
ORDER BY – We Must
Have Order
 This is how we can sort our data
 We can sort by more than one column
 Automatically assumes Ascending (ASC)
 Can change to Descending (DESC)
 Multiple columns can be in ORDER BY
 ASC and DESC can be done by column
Order of Operations
 FROM
 WHERE
 GROUP BY
 HAVING
 SELECT
 ORDER BY
Joins – Bringing the
World Together
What kinds of Joins?
 INNER – only what matches in both
 LEFT JOIN – everything from the left table
and what matches in right
 RIGHT JOIN – everything from the right
table and what matches in the left.
 FULL JOIN – everything from both tables.
INNER JOIN – Its Picky
 Only brings back results that are in both
tables.
 INNER JOIN, JOIN are all the same.
 Caution: you won’t see what is
eliminated.
INNER JOIN
FROM StackOverflow.dbo.Users U
INNER JOIN StackOverflow.dbo.Badges B
ON U.Id = B.UserId
LEFT JOIN – Soooo one sided
 Brings in all the rows from the table on
the left and only what matches on the
right.
 LEFT OUTER JOIN, LEFT JOIN
 You will see Nulls
LEFT JOIN
FROM StackOverflow.dbo.Users U
LEFT JOIN StackOverflow.dbo.Badges B
ON U.Id = B.UserId
RIGHT JOIN – Always Look
on the Right Side of Life
 Same as a LEFT JOIN, just switched.
 RIGHT OUTER JOIN, RIGHT JOIN
 You will see nulls in the left table
RIGHT JOIN
FROM StackOverflow.dbo.Users U
RIGHT JOIN StackOverflow.dbo.Badges B
ON U.Id = B.UserId
FULL JOINS – Everything,
Everything?
 Brings back everything in both tables.
 FULL OUTER JOIN, FULL JOIN
 You will see nulls on both sides of the
tables
FULL JOINS
FROM StackOverflow.dbo.Users U
FULL JOIN StackOverflow.dbo.Badges B
ON U.Id = B.UserId
Script out a Basic Backup
 BACKUP DATABASE
 TO DISK = N‘location’
 WITH NOFORMAT
 NOINIT
 NAME = N‘’
 SKIP
 NOREWIND
 NOUNLOAD
 STATS = 10
More Resources
 http://www.sqlcourse.com
 https://blog.codinghorror.com/a-visual-
explanation-of-sql-joins/
 https://docs.microsoft.com/en-us/sql/t-sql/statements/ba
 Check out my blog www.RoyalSQL.com
 If you have questions, contact me.
Allred_andrea@yahoo.com

More Related Content

Similar to Transact SQL (T-SQL) for Beginners (A New Hope)

Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesSmitha Padmanabhan
 
Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd sessionMedhat Dawoud
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1iccma
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1Syed Asrarali
 
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...Alan Manifold
 
Zero values in Priority tables - a blessing and a curse
Zero values in Priority tables - a blessing and a curseZero values in Priority tables - a blessing and a curse
Zero values in Priority tables - a blessing and a curseNo'am Newman
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSHITIKAJAIN4
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Vidyasagar Mundroy
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfTamiratDejene1
 
SQL Tutorial for Marketers
SQL Tutorial for MarketersSQL Tutorial for Marketers
SQL Tutorial for MarketersJustin Mares
 
Building Applications with a Graph Database
Building Applications with a Graph DatabaseBuilding Applications with a Graph Database
Building Applications with a Graph DatabaseTobias Lindaaker
 
Headaches of Blocking, Locking, and Deadlocking
Headaches of Blocking, Locking, and DeadlockingHeadaches of Blocking, Locking, and Deadlocking
Headaches of Blocking, Locking, and DeadlockingBrent Ozar
 
Project Fusion Reference Guide V2
Project Fusion Reference Guide V2Project Fusion Reference Guide V2
Project Fusion Reference Guide V2dnmiller
 

Similar to Transact SQL (T-SQL) for Beginners (A New Hope) (20)

Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practices
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Five Common SQL Errors
Five Common SQL ErrorsFive Common SQL Errors
Five Common SQL Errors
 
Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd session
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1
 
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...
Buried Treasure: Finding Reporting Gold in the Voyager Tables (using Microsof...
 
Zero values in Priority tables - a blessing and a curse
Zero values in Priority tables - a blessing and a curseZero values in Priority tables - a blessing and a curse
Zero values in Priority tables - a blessing and a curse
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
 
Excel training
Excel  training Excel  training
Excel training
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
SQL Tutorial for Marketers
SQL Tutorial for MarketersSQL Tutorial for Marketers
SQL Tutorial for Marketers
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Building Applications with a Graph Database
Building Applications with a Graph DatabaseBuilding Applications with a Graph Database
Building Applications with a Graph Database
 
Headaches of Blocking, Locking, and Deadlocking
Headaches of Blocking, Locking, and DeadlockingHeadaches of Blocking, Locking, and Deadlocking
Headaches of Blocking, Locking, and Deadlocking
 
Project Fusion Reference Guide V2
Project Fusion Reference Guide V2Project Fusion Reference Guide V2
Project Fusion Reference Guide V2
 

Recently uploaded

Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Delhi Call girls
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlkumarajju5765
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 

Recently uploaded (20)

Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 

Transact SQL (T-SQL) for Beginners (A New Hope)

  • 1. Transact SQL (T-SQL) for Beginners (A New Hope) What is it and how can I use it? This is for people who are new to T-SQL. We are covering SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and How to do a Backup.
  • 2. About Andrea:  Once upon a time in a kingdom not far away, Andrea was sick of answering phones and filing forms. Her kingdom was running low on gold and the wise wizard told her to find work that she loved and she would never truly work a day in her life. When she found the magical SQL Server, she knew it was the calling for her. She is passionate about SQL Server, “works” as a Database Administrator, is learning Sign Language, loves her #SQLFamily and finds joy in helping others find their passion too.  Wife to Ryan Allred and mom to two awesome teenagers.  Loves Music, concerts, guitars, snowboarding, bouldering and acting.  16 years of SQL Server experience  SQL SLC User group Chapter Leader  Allred_andrea@yahoo.com  @RoyalSQL, www.RoyalSQL.com
  • 3. What is T-SQL?  SQL is Structured Query Language  Transact Structured Query Language  Microsoft Standard  PL-SQL is Oracle
  • 4. Let’s Start at the Very Beginning  SELECT – what I want  FROM – where I get it  WHERE – only if it matches what I want  GROUP BY – summarize it for me  HAVING – used after a group by to filter  ORDER BY – sort it the way I want it
  • 5. How is it like English?  I buy milk from the store if it is chocolate.  SELECT – I buy milk  FROM – from the store  WHERE – if it is chocolate.  SELECT milk FROM store WHERE flavor = chocolate
  • 6. SELECT- Use it Wisely  SELECT * is a quick way to see all the fields.  I like to use a SELECT TOP 5 when using *  When writing code or reports, always list out the fields (SELECT DisplayName, Location, Reputation, UpVotes)  Know what is being selecting from and how many rows to expect.
  • 7. FROM – Show me the Data!  FROM a table name (Users)  Must also include the schema name (dbo.Users)  Can connect multiple tables using joins, we will do that later.  SELECT TOP 5 * FROM StackOverflow.dbo.Users
  • 8. WHERE – Cutting out the data that you don’t need…one clause at a time.  Bringing back too much data will hurt performance  “WHERE” brings back only what you need  Non-Mutants can only look at so much data and have it be useful.
  • 9. Building a Where Clause  Column, operator, what is being comparing against.  WHERE DisplayName = ‘Anon’  I only want people that have the Display Name of Anon.
  • 10. Basic Operators (Can it really be this simple?)  = Equals – It’s all the same.  WHERE DisplayName = 'Anon'  <> or != Does Not Equal – Not a match.  WHERE DisplayName != 'Anon'  WHERE DisplayName <> 'Anon'
  • 11. Basic Operators Continued  > Greater Than – The column has more  WHERE UpVotes > 23793  >= Greater Than or Equal to  WHERE UpVotes >= 23793  < Less Than – The column has less  WHERE UpVotes < 45  <= Less Than or Equal to  WHERE UpVotes <= 45
  • 12. When the Basics Won’t Do… Super Cool Operators  LIKE – It’s like you know  WHERE DisplayName LIKE 'Anon%'  NOT LIKE – It’s not like that  WHERE DisplayName NOT LIKE 'Anon%'
  • 13. Super Cool Operators Continued  IN – Is it in this?  WHERE DisplayName IN ('Anon', 'Anonymous', 'Anonymoose')  NOT IN – It’s soooo not in that.  WHERE DisplayName IN ('Anon', 'Anonymous', 'Anonymoose')
  • 14. Super Cool Operators Continued  BETWEEN – Stuck in the middle  WHERE UpVotes BETWEEN 5000 AND 5045  NOT BETWEEN – It’s outside of that.  WHERE UpVotes NOT BETWEEN 5000 AND 5045
  • 15. GROUP BY – Let’s Get Together  Gathers similar Data together  Used to create Aggregates  Awesome for Totals, Averages and Counts  GROUP BY Columns that you don’t want to Sum, Count or Average  Can have multiple columns in GROUP BY  GROUP BY [DisplayName],[Location]
  • 16. Having – What do you Have?  Works with a GROUP BY  This is another way to filter out data.  Can have multiple columns in HAVING Clause.  HAVING COUNT(Id) > 50
  • 17. ORDER BY – We Must Have Order  This is how we can sort our data  We can sort by more than one column  Automatically assumes Ascending (ASC)  Can change to Descending (DESC)  Multiple columns can be in ORDER BY  ASC and DESC can be done by column
  • 18. Order of Operations  FROM  WHERE  GROUP BY  HAVING  SELECT  ORDER BY
  • 19. Joins – Bringing the World Together
  • 20. What kinds of Joins?  INNER – only what matches in both  LEFT JOIN – everything from the left table and what matches in right  RIGHT JOIN – everything from the right table and what matches in the left.  FULL JOIN – everything from both tables.
  • 21. INNER JOIN – Its Picky  Only brings back results that are in both tables.  INNER JOIN, JOIN are all the same.  Caution: you won’t see what is eliminated.
  • 22. INNER JOIN FROM StackOverflow.dbo.Users U INNER JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId
  • 23. LEFT JOIN – Soooo one sided  Brings in all the rows from the table on the left and only what matches on the right.  LEFT OUTER JOIN, LEFT JOIN  You will see Nulls
  • 24. LEFT JOIN FROM StackOverflow.dbo.Users U LEFT JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId
  • 25. RIGHT JOIN – Always Look on the Right Side of Life  Same as a LEFT JOIN, just switched.  RIGHT OUTER JOIN, RIGHT JOIN  You will see nulls in the left table
  • 26. RIGHT JOIN FROM StackOverflow.dbo.Users U RIGHT JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId
  • 27. FULL JOINS – Everything, Everything?  Brings back everything in both tables.  FULL OUTER JOIN, FULL JOIN  You will see nulls on both sides of the tables
  • 28. FULL JOINS FROM StackOverflow.dbo.Users U FULL JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId
  • 29. Script out a Basic Backup  BACKUP DATABASE  TO DISK = N‘location’  WITH NOFORMAT  NOINIT  NAME = N‘’  SKIP  NOREWIND  NOUNLOAD  STATS = 10
  • 30. More Resources  http://www.sqlcourse.com  https://blog.codinghorror.com/a-visual- explanation-of-sql-joins/  https://docs.microsoft.com/en-us/sql/t-sql/statements/ba  Check out my blog www.RoyalSQL.com  If you have questions, contact me. Allred_andrea@yahoo.com

Editor's Notes

  1. T-SQL is a lot like a spoken language and once you flip the switch in your brain it will be simple. When I was first learning T-SQL, I started to Dream in it and I ended up cleaning my entire house using T-SQL. I woke up exhausted and sad to find out that my house wasn’t clean.
  2. D
  3. Demo – Only one
  4. Demo Everything has it’s opposite.
  5. Super cool operators will affect performance. They are not the best to use if you can avoid it.
  6. Super cool operators will affect performance. They are not the best to use if you can avoid it.
  7. Demo Super cool operators will affect performance. They are not the best to use if you can avoid it.
  8. Demo
  9. Demo
  10. Demo
  11. Demo
  12. BACKUP DATABASE [StackOverflow] TO DISK = N&amp;apos;C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\StackOverflow.bak&amp;apos; WITH NOFORMAT, NOINIT, NAME = N&amp;apos;StackOverflow-Full Database Backup&amp;apos;, SKIP, NOREWIND, NOUNLOAD, STATS = 10 BACKUP DATABASE (Name) TO DISK = N‘location’ (Location) WITH NOFORMAT (Uses the existing media header and backup set FORMAT will create new ones.) NOINIT (Appends to existing backup set INIT means Overwrite) NAME = N‘’ (What you want to name it) SKIP (Doesn’t check the expiration date before overwriting – NOSKIP will check) NOREWIND (Means it will keep the tape open after backup, REWIND means SQL will release and rewind the tape) NOUNLOAD (Means the tape will stay loaded in the tape drive, UnLoad means the tape is rewound and unloaded. When the backup finishes) STATS = 10 – Displays a percentage after each section is complete. The default is 10.
  13. T-SQL Demos: /*Basic SELECT and FROM*/ SELECT TOP 5 * FROM [StackOverflow].[dbo].[Users] /* Specified Columns SELECT and FROM*/ SELECT TOP (10) [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] ******** /* WHERE = */ SELECT TOP (10) [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE DisplayName = &amp;apos;Anon&amp;apos; --********** /* WHERE != &amp;lt;&amp;gt;*/ SELECT TOP (10) [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE DisplayName != &amp;apos;Anon&amp;apos; /* WHERE &amp;gt; &amp;gt;= */ SELECT [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE UpVotes &amp;gt;= 23793 /* WHERE &amp;gt; &amp;gt;= */ SELECT TOP 10 [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE UpVotes &amp;lt; 45 --********* /*WHERE LIKE NOT LIKE */ SELECT TOP 10 [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE DisplayName LIKE &amp;apos;Anon%&amp;apos; /*WHERE IN NOT IN */ SELECT TOP 10 [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE DisplayName IN (&amp;apos;Anon&amp;apos;, &amp;apos;Anonymous&amp;apos;, &amp;apos;Anonymoose&amp;apos;) /* WHERE BETWEEN NOT BETWEEN */ SELECT TOP 15 [DisplayName] ,[Location] ,[Reputation] ,[UpVotes] FROM [StackOverflow].[dbo].[Users] WHERE UpVotes BETWEEN 5000 AND 5045 --*********** /* GROUP BY COUNT AVG SUM MIN MAX*/ SELECT [DisplayName] ,[Location] ,COUNT([UpVotes]) ,COUNT(Id) FROM [StackOverflow].[dbo].[Users] WHERE DisplayName = &amp;apos;Anon&amp;apos; GROUP BY DisplayName, [Location] --********************* /*HAVING */ SELECT [DisplayName] ,[Location] ,SUM([UpVotes]) AS UpVotes ,COUNT(Id) FROM [StackOverflow].[dbo].[Users] WHERE DisplayName = &amp;apos;Anon&amp;apos; GROUP BY DisplayName, [Location] HAVING SUM([UpVotes]) &amp;gt; 5 --************* /*ORDER BY ASC DESC*/ SELECT [DisplayName] ,[Location] ,SUM([UpVotes]) AS UpVotes ,COUNT(Id) AS Count FROM [StackOverflow].[dbo].[Users] WHERE DisplayName = &amp;apos;Anon&amp;apos; GROUP BY DisplayName, [Location] HAVING SUM([UpVotes]) &amp;gt; 5 ORDER BY Count --*********** /*INNER JOIN*/ SELECT TOP 5 U.Id, U.DisplayName, U.Location, U.Reputation, U.UpVotes,B.Name FROM StackOverflow.dbo.Users U INNER JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId WHERE DisplayName = &amp;apos;Anon&amp;apos; /*LEFT JOIN*/ SELECT U.*, B.*--U.Id, U.DisplayName, U.Location, U.Reputation, U.UpVotes,B.Name FROM StackOverflow.dbo.Users U LEFT JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId WHERE DisplayName = &amp;apos;Anon&amp;apos; AND U.Id = 3903518 /*RIGHT JOIN*/ SELECT U.Id, U.DisplayName, U.Location, U.Reputation, U.UpVotes,B.Name FROM StackOverflow.dbo.Users U RIGHT JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId WHERE B.UserId = 2253640 /*FULL JOIN*/ SELECT U.Id, U.DisplayName, U.Location, U.Reputation, U.UpVotes,B.Name FROM StackOverflow.dbo.Users U FULL JOIN StackOverflow.dbo.Badges B ON U.Id = B.UserId WHERE B.UserId = 2253640 OR U.Id = 3903518 --*********