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
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
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
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
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
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
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;