t-sql basics
DESCRIPTION
TRANSCRIPT
- 1. SQL Basics Some common queries
- 2. SELECT USE AdventureWorks GO SELECT ContactID, Title, FirstName, LastName FROM Person.Contact Comment: Person is the Schema and Contact is the Table
- 3. SELECT ALL USE AdventureWorks GO SELECT * FROM Person.Contact Comment: Good practise to explicity reference only necessary columns
- 4. FILTER USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE Title = 'Ms.'
- 5. AND USE AdventureWorks GO SELECT Title , FirstName, LastName FROM Person .Contact WHERE Title = 'Ms.' AND LastName = 'Antrim'
- 6. OR USE AdventureWorks GO SELECT Title , FirstName, LastName FROM Person .Contact WHERE Title = 'Ms.' OR LastName = 'Antrim'
- 7. NOT USE AdventureWorks GO SELECT Title, FirstName, LastName FROM Person.Contact WHERE NOT Title = 'Ms.'
- 8. WITHOUT PARENTHESIS USE AdventureWorks GO SELECT Title , FirstName, LastName FROM Person .Contact WHERE Title = 'Ms.' AND FirstName = 'Catherine' OR LastName = 'Adams'
- 9. With Parentheses SELECT ContactID, Title, FirstName, MiddleName, LastName FROM Person.Contact WHERE (Title = 'Ms.' AND FirstName = 'Catherine') OR LastName = 'Adams'
- 10. BETWEEN USE AdventureWorks GO SELECT SalesOrderID, ShipDate FROM Sales.SalesOrderHeader WHERE ShipDate BETWEEN '7/28/2002' AND '7/29/2002'
- 11. Less Than USE AdventureWorks GO SELECT ProductID , Name , StandardCost FROM Production .Product WHERE StandardCost < 110.0000
- 12. IS NULL USE AdventureWorks GO SELECT ProductID , Name , Weight FROM Production .Product WHERE Weight IS NULL
- 13. IN USE AdventureWorks GO SELECT ProductID , Name , Color FROM Production .Product WHERE Color IN ( 'Silver' , 'Black' , 'Red' )
- 14. LIKE USE AdventureWorks GO SELECT ProductID , Name FROM Production .Product WHERE Name LIKE 'B%'
- 15. Escape USE AdventureWorks GO SELECT ProductID , Name FROM Production .Product WHERE Name LIKE '%/_%' ESCAPE '/'
- 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. 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
- 18. ORDER BY Unselected Column USE AdventureWorks GO SELECT p .Name FROM Production .Product p
- 19. TOP USE AdventureWorks GO SELECT TOP 10 v .Name, v.CreditRating FROM Purchasing .Vendor v ORDER BY v .CreditRating DESC , v.Name
- 20. Variables USE AdventureWorks GO DECLARE @Percentage float SET @Percentage = 1 SELECT TOP (@Percentage) PERCENT Name FROM Production .Product ORDER BY Name
- 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. 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. 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. DISTINCT USE AdventureWorks GO SELECT DISTINCT HireDate FROM HumanResources .Employee
- 25. AVG USE AdventureWorks GO SELECT AVG (ListPrice) FROM Production .Product
- 26. AVG and DISTINCT USE AdventureWorks GO SELECT AVG ( DISTINCT ListPrice ) FROM Production .Product
- 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. INFORMATION SCHEMA USE AdventureWorks GO SELECT column_name + ' IS NULL AND ' FROM INFORMATION_SCHEMA.columns WHERE table_name = 'Employee' ORDER BY ORDINAL_POSITION
- 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. 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. SELECT INTO USE AdventureWorks GO SELECT CustomerID , Name , SalesPersonID, Demographics INTO Store_Archive FROM Sales .Store Comment: This creates a table called Store_Archive
- 32. Create a schema with SELECT USE AdventureWorks GO SELECT CustomerID , Name , SalesPersonID, Demographics INTO Store_Archive2 FROM Sales .Store WHERE 1 =0
- 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. 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. 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. 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. 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;