SQL Basics
Some common queries
SELECT
USE AdventureWorks
GO
SELECT ContactID,
Title,
FirstName,
LastName
FROM Person.Contact
Comment:
Person is the Schem...
SELECT ALL
USE AdventureWorks
GO
SELECT *
FROM Person.Contact
Comment: Good
practise to
explicity
reference only
necessary...
FILTER
USE AdventureWorks
GO
SELECT Title,
FirstName,
LastName
FROM Person.Contact
WHERE Title = 'Ms.'
AND
USE AdventureWorks
GO
SELECT Title,
FirstName,
LastName
FROM Person.Contact
WHERE Title = 'Ms.' AND
LastName = 'Antrim'
OR
USE AdventureWorks
GO
SELECT Title,
FirstName,
LastName
FROM Person.Contact
WHERE Title = 'Ms.' OR
LastName = 'Antrim'
NOT
USE AdventureWorks
GO
SELECT Title,
FirstName,
LastName
FROM Person.Contact
WHERE NOT Title = 'Ms.'
WITHOUT PARENTHESIS
USE AdventureWorks
GO
SELECT Title,
FirstName,
LastName
FROM Person.Contact
WHERE Title = 'Ms.' AND
Fi...
With Parentheses
SELECT ContactID,
Title,
FirstName,
MiddleName,
LastName
FROM Person.Contact
WHERE (Title = 'Ms.' AND
Fir...
BETWEEN
USE AdventureWorks
GO
SELECT SalesOrderID,
ShipDate
FROM Sales.SalesOrderHeader
WHERE ShipDate BETWEEN '7/28/2002'...
Less Than
USE AdventureWorks
GO
SELECT ProductID,
Name,
StandardCost
FROM Production.Product
WHERE StandardCost < 110.0000
IS NULL
USE AdventureWorks
GO
SELECT ProductID,
Name,
Weight
FROM Production.Product
WHERE Weight IS NULL
IN
USE AdventureWorks
GO
SELECT ProductID,
Name,
Color
FROM Production.Product
WHERE Color IN ('Silver', 'Black', 'Red')
LIKE
USE AdventureWorks
GO
SELECT ProductID,
Name
FROM Production.Product
WHERE Name LIKE 'B%'
Escape
USE AdventureWorks
GO
SELECT ProductID,
Name
FROM Production.Product
WHERE Name LIKE '%/_%' ESCAPE '/'
ORDER BY
USE AdventureWorks
GO
SELECT p.Name,
h.EndDate,
h.ListPrice
FROM Production.Product p
INNER JOIN Production.Produ...
ORDER - DESC
USE AdventureWorks
GO
SELECT p.Name,
h.EndDate,
h.ListPrice
FROM Production.Product p
INNER JOIN Production.P...
ORDER BY Unselected Column
USE AdventureWorks
GO
SELECT p.Name
FROM Production.Product p
TOP
USE AdventureWorks
GO
SELECT TOP 10 v.Name,
v.CreditRating
FROM Purchasing.Vendor v
ORDER BY v.CreditRating DESC, v.Na...
Variables
USE AdventureWorks
GO
DECLARE @Percentage float
SET @Percentage = 1
SELECT TOP (@Percentage) PERCENT
Name
FROM P...
GROUP BY
USE AdventureWorks
GO
SELECT OrderDate,
SUM(TotalDue) TotalDueByOrderDate
FROM Sales.SalesOrderHeader
WHERE Order...
GROUP BY ALL
USE AdventureWorks
GO
SELECT OrderDate,
SUM(TotalDue) TotalDueByOrderDate
FROM Sales.SalesOrderHeader
WHERE O...
HAVING
USE AdventureWorks
GO
SELECT s.Name,
COUNT(w.WorkOrderID) Cnt
FROM Production.ScrapReason s
INNER JOIN Production.W...
DISTINCT
USE AdventureWorks
GO
SELECT DISTINCT HireDate
FROM HumanResources.Employee
AVG
USE AdventureWorks
GO
SELECT AVG(ListPrice)
FROM Production.Product
AVG and DISTINCT
USE AdventureWorks
GO
SELECT AVG(DISTINCT ListPrice)
FROM Production.Product
Column ALIASES
USE AdventureWorks
GO
SELECT Color AS 'Grouped Color',
AVG(DISTINCT ListPrice) AS 'Average Distinct List Pr...
INFORMATION SCHEMA
USE AdventureWorks
GO
SELECT column_name + ' IS NULL AND '
FROM INFORMATION_SCHEMA.columns
WHERE table_...
String concatenation
USE AdventureWorks
GO
SELECT 'The ' +
p.name +
' is only ' +
CONVERT(varchar(25),p.ListPrice) +
'!'
F...
Comma Delimited List
USE AdventureWorks
GO
DECLARE @Shifts varchar(20)
SET @Shifts = ''
SELECT @Shifts = @Shifts + s.Name ...
SELECT INTO
USE AdventureWorks
GO
SELECT CustomerID,
Name,
SalesPersonID,
Demographics
INTO Store_Archive
FROM Sales.Store...
Create a schema with SELECT
USE AdventureWorks
GO
SELECT CustomerID,
Name,
SalesPersonID,
Demographics
INTO Store_Archive2...
Puzzle
I've got The Customers table : T1 .. has two columns :
ID(int) , IsDeleted(bit)
I want in a one sql query to return...
Prepare data for puzzle
USE tempdb;
IF OBJECT_ID('dbo.T1') IS NOT NULL
DROP TABLE dbo.T1;
CREATE TABLE dbo.t1
(
ID INT NOT...
Check expected answers
SELECT Count(ID) AS NumberOfAllExistingCustomers
FROM T1
SELECT Count(ID) AS NumberOfDeletedCustome...
Answer One
SELECT COUNT(*) AS NumberOfAllCustomers,
SUM(CASE IsDeleted WHEN 1 THEN 1 END)
AS NumberOfDeletedCustomers,
SUM...
Answer Two
select numberOfAllCustomers, numberOfDeletedCustomers,
numberOfAllCustomers - numberOfDeletedCustomers
AS
Numbe...
Upcoming SlideShare
Loading in …5
×

T-Sql basics

2,531 views
2,403 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,531
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Person is the Schema and Contact is the Table
  • T-Sql basics

    1. 1. SQL Basics Some common queries
    2. 2. SELECT USE AdventureWorks GO SELECT ContactID, Title, FirstName, LastName FROM Person.Contact Comment: Person is the Schema and Contact is the Table Comment: Person is the Schema and Contact is the Table
    3. 3. SELECT ALL USE AdventureWorks GO SELECT * FROM Person.Contact Comment: Good practise to explicity reference only necessary columns Comment: Good practise to explicity reference only necessary columns
    4. 4. FILTER USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE Title = 'Ms.'
    5. 5. AND USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE Title = 'Ms.' AND LastName = 'Antrim'
    6. 6. OR USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE Title = 'Ms.' OR LastName = 'Antrim'
    7. 7. NOT USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE NOT Title = 'Ms.'
    8. 8. WITHOUT PARENTHESIS USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE Title = 'Ms.' AND FirstName = 'Catherine' OR LastName = 'Adams'
    9. 9. With Parentheses SELECT ContactID, Title, FirstName, MiddleName, LastName FROM Person.Contact WHERE (Title = 'Ms.' AND FirstName = 'Catherine') OR LastName = 'Adams'
    10. 10. BETWEEN USE AdventureWorks GO SELECT SalesOrderID, ShipDate FROM Sales.SalesOrderHeader WHERE ShipDate BETWEEN '7/28/2002' AND '7/29/2002'
    11. 11. Less Than USE AdventureWorks GO SELECT ProductID, Name, StandardCost FROM Production.Product WHERE StandardCost < 110.0000
    12. 12. IS NULL USE AdventureWorks GO SELECT ProductID, Name, Weight FROM Production.Product WHERE Weight IS NULL
    13. 13. IN USE AdventureWorks GO SELECT ProductID, Name, Color FROM Production.Product WHERE Color IN ('Silver', 'Black', 'Red')
    14. 14. LIKE USE AdventureWorks GO SELECT ProductID, Name FROM Production.Product WHERE Name LIKE 'B%'
    15. 15. Escape USE AdventureWorks GO SELECT ProductID, Name FROM Production.Product WHERE Name LIKE '%/_%' ESCAPE '/'
    16. 16. ORDER BY USE AdventureWorks GO SELECT p.Name, h.EndDate, h.ListPrice FROM Production.Product p INNER JOIN Production.ProductListPriceHistory h ON p.ProductID = h.ProductID ORDER BY p.Name, h.EndDate
    17. 17. ORDER - DESC USE AdventureWorks GO SELECT p.Name, h.EndDate, h.ListPrice FROM Production.Product p INNER JOIN Production.ProductListPriceHistory h ON p.ProductID = h.ProductID ORDER BY p.Name DESC, h.EndDate DESC Comment: Although queries sometimes appear to return data properly without an ORDER BY clause, the natural ordering of results is determined by the physical key column order in the clustered index Comment: Although queries sometimes appear to return data properly without an ORDER BY clause, the natural ordering of results is determined by the physical key column order in the clustered index
    18. 18. ORDER BY Unselected Column USE AdventureWorks GO SELECT p.Name FROM Production.Product p
    19. 19. TOP USE AdventureWorks GO SELECT TOP 10 v.Name, v.CreditRating FROM Purchasing.Vendor v ORDER BY v.CreditRating DESC, v.Name
    20. 20. Variables USE AdventureWorks GO DECLARE @Percentage float SET @Percentage = 1 SELECT TOP (@Percentage) PERCENT Name FROM Production.Product ORDER BY Name
    21. 21. GROUP BY USE AdventureWorks GO SELECT OrderDate, SUM(TotalDue) TotalDueByOrderDate FROM Sales.SalesOrderHeader WHERE OrderDate BETWEEN '7/1/2001' AND '7/31/2001' GROUP BY OrderDate
    22. 22. GROUP BY ALL USE AdventureWorks GO SELECT OrderDate, SUM(TotalDue) TotalDueByOrderDate FROM Sales.SalesOrderHeader WHERE OrderDate BETWEEN '7/1/2001' AND '7/31/2001' GROUP BY ALL OrderDate
    23. 23. HAVING USE AdventureWorks GO SELECT s.Name, COUNT(w.WorkOrderID) Cnt FROM Production.ScrapReason s INNER JOIN Production.WorkOrder w ON s.ScrapReasonID = w.ScrapReasonID GROUP BY s.Name HAVING COUNT(*)>50
    24. 24. DISTINCT USE AdventureWorks GO SELECT DISTINCT HireDate FROM HumanResources.Employee
    25. 25. AVG USE AdventureWorks GO SELECT AVG(ListPrice) FROM Production.Product
    26. 26. AVG and DISTINCT USE AdventureWorks GO SELECT AVG(DISTINCT ListPrice) FROM Production.Product
    27. 27. Column ALIASES USE AdventureWorks GO SELECT Color AS 'Grouped Color', AVG(DISTINCT ListPrice) AS 'Average Distinct List Price', AVG(ListPrice) 'Average List Price' FROM Production.Product GROUP BY Color
    28. 28. INFORMATION SCHEMA USE AdventureWorks GO SELECT column_name + ' IS NULL AND ' FROM INFORMATION_SCHEMA.columns WHERE table_name = 'Employee' ORDER BY ORDINAL_POSITION
    29. 29. String concatenation USE AdventureWorks GO SELECT 'The ' + p.name + ' is only ' + CONVERT(varchar(25),p.ListPrice) + '!' FROM Production.Product p WHERE p.ListPrice between 100 AND 120 ORDER BY p.ListPrice
    30. 30. Comma Delimited List USE AdventureWorks GO DECLARE @Shifts varchar(20) SET @Shifts = '' SELECT @Shifts = @Shifts + s.Name + ',' FROM HumanResources.Shift s ORDER BY s.EndTime SELECT @Shifts
    31. 31. SELECT INTO USE AdventureWorks GO SELECT CustomerID, Name, SalesPersonID, Demographics INTO Store_Archive FROM Sales.Store Comment: This creates a table called Store_Archive Comment: This creates a table called Store_Archive
    32. 32. Create a schema with SELECT USE AdventureWorks GO SELECT CustomerID, Name, SalesPersonID, Demographics INTO Store_Archive2 FROM Sales.Store WHERE 1=0
    33. 33. Puzzle I've got The Customers table : T1 .. has two columns : ID(int) , IsDeleted(bit) I want in a one sql query to return : NumberOfAllCustomers -- NumberOfDeletedCustomers -- NumberOfExistingCustomers
    34. 34. Prepare data for puzzle USE tempdb; IF OBJECT_ID('dbo.T1') IS NOT NULL DROP TABLE dbo.T1; CREATE TABLE dbo.t1 ( ID INT NOT NULL, IsDeleted bit ); INSERT INTO dbo.T1(ID, IsDeleted) VALUES (1,0); INSERT INTO dbo.T1(ID, IsDeleted) VALUES (2,1); INSERT INTO dbo.T1(ID, IsDeleted) VALUES (3,0); INSERT INTO dbo.T1(ID, IsDeleted) VALUES (4,1); INSERT INTO dbo.T1(ID, IsDeleted) VALUES (5,0); GO
    35. 35. Check expected answers SELECT Count(ID) AS NumberOfAllExistingCustomers FROM T1 SELECT Count(ID) AS NumberOfDeletedCustomers FROM T1 WHERE IsDeleted = 1 SELECT Count(ID) AS NumberOfExistingCustomers FROM T1 WHERE IsDeleted = 0
    36. 36. Answer One SELECT COUNT(*) AS NumberOfAllCustomers, SUM(CASE IsDeleted WHEN 1 THEN 1 END) AS NumberOfDeletedCustomers, SUM(CASE IsDeleted WHEN 0 THEN 1 END) AS NumberOfExistingCustomers FROM T1 GO
    37. 37. Answer Two select numberOfAllCustomers, numberOfDeletedCustomers, numberOfAllCustomers - numberOfDeletedCustomers AS NumberOfExistingCustomers from ( select (select count(id) from t1) numberOfAllCustomers, (select count(id) from t1 where isdeleted=cast(1 as bit)) numberOfDeletedCustomers ) t;

    ×