select count(*) from Sales.SalesOrderDetail
WHERE CarrierTrackingNumber LIKE '%-98'


select top 10 * from Sales.SalesOrderdetail


select count(*) from (SELECT * FROM Sales.SalesOrderDetail
TABLESAMPLE(2000 ROWS)) AS P
WHERE CarrierTrackingNumber LIKE '%-98'


select count(*) from (SELECT * FROM Sales.SalesOrderDetail
WHERE CarrierTrackingNumber LIKE '%-98')
TABLESAMPLE(2000 ROWS) AS P


WITH Rows
AS
(SELECT * FROM Sales.SalesOrderDetail
WHERE CarrierTrackingNumber LIKE '%-98')
SELECT COUNT(*) FROM Rows TABLESAMPLE(2000 ROWS)

SELECT AVG(UnitPrice * OrderQty) AS [Avg Line Item] 
FROM Sales.SalesOrderDetail
TABLESAMPLE SYSTEM (10)



SELECT COUNT(*) AS [Number of line items] 
FROM Sales.SalesOrderDetail
TABLESAMPLE SYSTEM (10)


select top 1 * from sales.SalesOrderheader

SELECT AVG(UnitPrice * OrderQty) FROM
Sales.SalesOrderHeader AS H TABLESAMPLE(50)
JOIN Sales.SalesOrderDetail AS D
ON H.SalesOrderID = D.SalesOrderID


SELECT AVG(UnitPrice * OrderQty) FROM
Sales.SalesOrderDetail TABLESAMPLE(10)
WHERE OrderQty = 1


SELECT AVG(UnitPrice * OrderQty) FROM
Sales.SalesOrderDetail
WHERE OrderQty = 1


SELECT COUNT(*) FROM Sales.SalesOrderDetail D1
JOIN Sales.SalesOrderDetail D2
ON D2.SalesOrderDetailID = D1.SalesOrderDetailID


SELECT COUNT(*) AS [Count] FROM 
Sales.SalesOrderDetail AS D1 TABLESAMPLE(10)
JOIN 
Sales.SalesOrderDetail D2 TABLESAMPLE(10)
ON D2.SalesOrderDetailID = D1.SalesOrderDetailID


WITH orders
AS
(
SELECT COUNT(*) [Count] FROM Sales.SalesOrderHeader AS H
TABLESAMPLE(25)
JOIN Sales.SalesOrderDetail AS D
ON H.SalesOrderID = D.SalesOrderID
GROUP BY H.SalesOrderID
)
SELECT AVG([Count]) FROM orders




AND
D2.SalesOrder

SELECT *  FROM Sales.SalesOrderDetail D1



SELECT
--TOP (100)
* from sales.salesorderdetail
TABLESAMPLE SYSTEM (1)



select top 1 * from sales.salesOrderDetail

select count(*) from sales.salesOrderDetail
where OrderQty=1


SELECT TOP 10 PERCENT  count(*) [COUNT]
FROM sales.salesOrderDetail 
where UnitPrice > 10

SELECT count(*) 
FROM sales.salesOrderDetail TABLESAMPLE(10) 
where UnitPrice > 10

WITH BigLineItems
AS
( SELECT * FROM Sales.SalesOrderDetail
WHERE UnitPrice > 10)
SELECT * FROM 
BigLineItems TABLESAMPLE(10)




SELECT COUNT(*) FROM Sales.SalesOrderDetail
TABLESAMPLE(10) REPEATABLE(1)
SELECT COUNT(*) FROM Sales.SalesOrderDetail
TABLESAMPLE(10) REPEATABLE(1)


SELECT COUNT(*) FROM Sales.SalesOrderDetail

SELECT COUNT(*) FROM Sales.SalesOrderDetail
TABLESAMPLE(12132 ROWS) REPEATABLE(1)


SELECT COUNT(*) FROM Sales.SalesOrderDetail
TABLESAMPLE(10 PERCENT) REPEATABLE(1)


SELECT COUNT(*) AS [Number of line items] 
FROM Sales.SalesOrderDetail
TABLESAMPLE SYSTEM (10)
REPEATABLE(1)
 

--DROP PROCEDURE GetPubAuth

CREATE PROCEDURE GetPubAuth(@state CHAR(2))
AS
SELECT * FROM pubs..Publishers WHERE State = @state;
SELECT * FROM pubs..Authors WHERE State = @state;

exec GetPubAuth 'CA'

--DROP INDEX pub_state ON pubs..Publishers

CREATE INDEX pub_state ON pubs..Publishers(state)


sp_recompile N'GetPubAuth'


