SELECT TOP (N) <column_list>
FROM <table_source>
WHERE <search_condition>
ORDER BY <order list> [ASC|DESC]
--Select all rows
SELECT *
--Select top n number of rows
SELECT TOP (N)
--Select top n percent of rows
SELECT TOP (N) PERCENT
--Select top n rows, return specified columns
SELECT TOP (N) WITH TIES <Column_list1>
--Indicate table to select rows from
FROM <table_source>
--A condition to search
WHERE
--Sort rows ascending/descending before selecting rows
ORDER BY
SELECT ProductID, ProductName, ListPrice
FROM Production.Product
ORDER BY ListPrice DESC
OFFSET 10 ROWS --Skip 10 rows
FETCH NEXT 10 ROWS ONLY; --Get the next 10
OFFSET/FETCH
ORDER BYOFFSET allows you to go to the next page of data by skipping n rowsFETCH will grab the next n rowsSELECT City, CountryRegion
FROM Production.Supplier
ORDER BY CountryRegion, City;
SELECT ALL City, CountryRegion
FROM Production.Supplier
ORDER BY CountryRegion, City;
SELECT DISTINCT City, CountryRegion
FROM Production.Supplier
ORDER BY CountryRegion, City;
SELECT inherently has ALL by defaultDISTINCT will select only unique combinations of values so that every row is differentWHERE uses search conditions which must evaluate to TRUE to return the rowSELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID = 2;
AS creates an alias to “rename” a column/tableSELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductName IS NOT NULL;
IS NULL or IS NOT NULL to allow/exclude empty valuesSELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID = 2
AND ListPrice < 10.00;
WHERE by using AND/ORAND is processed before ORSELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE (ProductCategoryID = 2 OR ProductCategoryID = 3)
AND (ListPrice < 10.00);
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID = 2
OR ProductCategoryID = 3
OR ProductCategoryID = 4;
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID IN (2, 3, 4);
IN is a shortcut for multiple equality conditions. Instead of multiple OR statements, just use IN.SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ListPrice >= 1.00
AND ListPrice <= 10.00;
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ListPrice BETWEEN 1.00 AND 10.00;
BETWEEN is a shortcut to filter the upper/lower bound. Instead of using two conditions with AND, use BETWEEN.For date fields:
WHERE ModifiedDate BETWEEN '2012-01-01' AND '2012-12-31';
For time fields:
WHERE ModifiedDate BETWEEN '2012-01-01 00:00:00.000' AND '2012-12-31 23:59:59.999';
SELECT Name, ListPrice
FROM SalesLT.Product
WHERE Name LIKE '%mountain%';
LIKE is only used for characters% is a wildcard to represent any string of 0+ characters_ is a wildcard to represent a single character
SELECT emp.FirstName, ord.Amount
FROM HR.Employee AS emp
JOIN Sales.SalesOrder AS ord
ON emp.EmployeeID = ord.EmployeeID;
FROM is done first, then JOIN, then ON then SELECTINNER JOIN which is the default for JOIN and is interchangeableINNER JOIN filters out rows that do not meet the conditions--Join 3 tables together
SELECT od.SalesOrderID, m.Name AS Model, p.Name AS ProductName, od.OrderQty
FROM Production.Product AS p
INNER JOIN Production.ProductModel AS m
ON p.ProductModelID = m.ProductModelID
INNER JOIN Sales.SalesOrderDetail AS od
ON p.ProductID = od.ProductID
ORDER BY od.SalesOrderID;

OUTER JOIN displays rows that match, plus rows that do not matchSELECT emp.FirstName, ord.Amount
FROM HR.Employee AS emp
LEFT OUTER JOIN Sales.SalesOrder AS ord
ON emp.EmployeeID = ord.EmployeeID;
--Create all combinations of employees and products
SELECT <select_list>
FROM table1 AS t1
CROSS JOIN table2 AS t2;
SELECT emp.FirstName, prd.Name
FROM HR.Employee AS emp
CROSS JOIN Production.Product AS prd;
SELECT emp.FirstName AS Employee,
mgr.FirstName AS Manager
FROM HR.Employee AS emp
LEFT OUTER JOIN HR.Employee AS mgr
ON emp.ManagerID = mgr.EmployeeID;
SELECT statements nested within another query--Scalar (Self-contained)
SELECT SalesOrderID, ProductID, OrderQty,
(SELECT AVG(OrderQty)
FROM SalesLT.SalesOrderDetail) AS AvgQty
FROM SalesLT.SalesOrderDetail
WHERE SalesOrderID =
(SELECT MAX(SalesOrderID)
FROM SalesLT.SalesOrderHeader);
--Multi-valued (Self-contained)
SELECT CustomerID, SalesOrderID
FROM Sales.SalesOrderHeader
WHERE CustomerID IN (
SELECT CustomerID
FROM Sales.Customer
WHERE CountryRegion = 'Canada');
JOIN, but depends on you. Also, more complex queries can be easier as subqueries instead of JOINsSELECT SalesOrderID, CustomerID, OrderDate
FROM SalesLT.SalesOrderHeader AS o1
WHERE SalesOrderID =
(SELECT MAX(SalesOrderID)
FROM SalesLT.SalesOrderHeader AS o2
WHERE o2.CustomerID = o1.CustomerID)
ORDER BY CustomerID, OrderDate;
WHERE clause.SELECT CustomerID, CompanyName, EmailAddress
FROM Sales.Customer AS c
WHERE EXISTS
(SELECT *
FROM Sales.SalesOrderHeader AS o
WHERE o.CustomerID = c.CustomerID);
EXISTS will check if TRUE but not return any rowsSELECT * is used because we don’t actually care about returning rowsNOT EXISTS as the opposite