COUNT

SELECT COUNT(*) -- All Records
FROM [Person].[Person]

SELECT COUNT([Title]) -- All Values
FROM [Person].[Person];

SELECT COUNT(DISTINCT [Title]) -- Distinct Values
FROM [Person].[Person]

Counting the Rows of Big Tables

CREATE VIEW [Production].[Product_RowCount]
WITH SCHEMABINDING
AS
  SELECT COUNT_BIG(*) AS [RowsCount]
  FROM [Production].[Product];
GO

CREATE UNIQUE CLUSTERED INDEX [IX_Production_Product_RowCount]
ON [Production].[Product_RowCount]([RowsCount]);

OVER [Clause

SELECT
	ROW_NUMBER() OVER(PARTITION BY [PostalCode] ORDER BY [SalesYTD] DESC) AS '#',   
	a.PostalCode,
	s.SalesYTD,
	p.LastName
FROM Sales.SalesPerson AS s
INNER JOIN Person.Person AS p
    ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address AS a
    ON a.AddressID = p.BusinessEntityID
WHERE TerritoryID IS NOT NULL
    AND SalesYTD <> 0
-- If you change the order, it changes the order of the final results.
-- ORDER BY SalesYTD ASC
SELECT DISTINCT
	[d].[DepartmentID],
	[Name] AS 'Department Name',
	COUNT([edh].BusinessEntityID) OVER (PARTITION BY [edh].[DepartmentID]) AS EmployeesPerDept,
	MIN([eph].[Rate]) OVER (PARTITION BY [edh].[DepartmentID]) AS [MinSalary],
	MAX([eph].[Rate]) OVER (PARTITION BY [edh].[DepartmentID]) AS [MaxSalary],
	AVG([eph].[Rate]) OVER (PARTITION BY [edh].[DepartmentID]) AS [AvgSalary]
FROM HumanResources.EmployeePayHistory AS [eph]
INNER JOIN HumanResources.EmployeeDepartmentHistory AS [edh]
    ON eph.BusinessEntityID = edh.BusinessEntityID
INNER JOIN HumanResources.Department AS d
	ON d.DepartmentID = edh.DepartmentID
ORDER BY [d].[DepartmentID]

SELECT
	[d].[DepartmentID],
	[Name] AS 'Department Name',
	COUNT([edh].BusinessEntityID) AS EmployeesPerDept,
	MIN([Rate]) AS [MinSalary],
	MAX([Rate]) AS [MaxSalary],
	AVG([Rate]) AS [AvgSalary]
FROM HumanResources.EmployeePayHistory AS [eph]
INNER JOIN HumanResources.EmployeeDepartmentHistory AS [edh]
    ON eph.BusinessEntityID = edh.BusinessEntityID
INNER JOIN HumanResources.Department AS d
	ON d.DepartmentID = edh.DepartmentID
GROUP BY [d].[DepartmentID], [Name]
ORDER BY [d].[DepartmentID]

Producing a "Moving Average" and "Cumulative Total"