This is for people who are new to T-SQL.
We are covering SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, simple JOINS and How to do a Backup.
Introduction Course to T-SQL. Present on June 2, 2017 at GroupBy Conference. By Andrea Allred, RoyalSQL.
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
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.
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
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
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
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
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.
D
Demo – Only one
Demo
Everything has it’s opposite.
Super cool operators will affect performance. They are not the best to use if you can avoid it.
Super cool operators will affect performance. They are not the best to use if you can avoid it.
Demo
Super cool operators will affect performance. They are not the best to use if you can avoid it.
Demo
Demo
Demo
Demo
BACKUP DATABASE [StackOverflow] TO DISK = N&apos;C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\StackOverflow.bak&apos; WITH NOFORMAT, NOINIT, NAME = N&apos;StackOverflow-Full Database Backup&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.
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 = &apos;Anon&apos;
--**********
/* WHERE != &lt;&gt;*/
SELECT TOP (10)
[DisplayName]
,[Location]
,[Reputation]
,[UpVotes]
FROM [StackOverflow].[dbo].[Users]
WHERE DisplayName != &apos;Anon&apos;
/* WHERE &gt; &gt;= */
SELECT
[DisplayName]
,[Location]
,[Reputation]
,[UpVotes]
FROM [StackOverflow].[dbo].[Users]
WHERE UpVotes &gt;= 23793
/* WHERE &gt; &gt;= */
SELECT TOP 10
[DisplayName]
,[Location]
,[Reputation]
,[UpVotes]
FROM [StackOverflow].[dbo].[Users]
WHERE UpVotes &lt; 45
--*********
/*WHERE LIKE NOT LIKE */
SELECT TOP 10
[DisplayName]
,[Location]
,[Reputation]
,[UpVotes]
FROM [StackOverflow].[dbo].[Users]
WHERE DisplayName LIKE &apos;Anon%&apos;
/*WHERE IN NOT IN */
SELECT TOP 10
[DisplayName]
,[Location]
,[Reputation]
,[UpVotes]
FROM [StackOverflow].[dbo].[Users]
WHERE DisplayName IN (&apos;Anon&apos;, &apos;Anonymous&apos;, &apos;Anonymoose&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 = &apos;Anon&apos;
GROUP BY DisplayName, [Location]
--*********************
/*HAVING */
SELECT
[DisplayName]
,[Location]
,SUM([UpVotes]) AS UpVotes
,COUNT(Id)
FROM [StackOverflow].[dbo].[Users]
WHERE DisplayName = &apos;Anon&apos;
GROUP BY DisplayName, [Location]
HAVING SUM([UpVotes]) &gt; 5
--*************
/*ORDER BY ASC DESC*/
SELECT
[DisplayName]
,[Location]
,SUM([UpVotes]) AS UpVotes
,COUNT(Id) AS Count
FROM [StackOverflow].[dbo].[Users]
WHERE DisplayName = &apos;Anon&apos;
GROUP BY DisplayName, [Location]
HAVING SUM([UpVotes]) &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 = &apos;Anon&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 = &apos;Anon&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
--*********