Professional Documents
Culture Documents
GO
SELECT *
FROM Production.Product
-- Alternate way.
USE AdventureWorks;
GO
SELECT p.*
FROM Production.Product AS p
GO
USE AdventureWorks;
GO
FROM Production.Product
GO
USE AdventureWorks;
GO
FROM Production.Product
GO
USE AdventureWorks;
GO
FROM Production.Product AS p
ON p.ProductID = sod.ProductID
GO
USE AdventureWorks;
GO
SELECT 'Total income is', ((OrderQty * UnitPrice) * (1.0 - UnitPriceDiscount)), ' for ',
p.Name AS ProductName
FROM Production.Product AS p
ON p.ProductID = sod.ProductID
GO
USE AdventureWorks;
GO
FROM HumanResources.Employee
ORDER BY JobTitle;
GO
USE tempdb;
GO
GO
SELECT *
INTO #Bicycles
FROM AdventureWorks.Production.Product
GO
USE AdventureWorks;
GO
GO
GO
GO
GO
USE AdventureWorks;
GO
FROM Production.Product AS p
WHERE EXISTS
(SELECT *
FROM Production.ProductModel AS pm
GO
-- OR
USE AdventureWorks;
GO
FROM Production.Product
WHERE ProductModelID IN
(SELECT ProductModelID
FROM Production.ProductModel
GO
USE AdventureWorks;
GO
FROM Person.Person AS p
JOIN HumanResources.Employee AS e
(SELECT Bonus
FROM Sales.SalesPerson AS sp
GO
USE AdventureWorks;
GO
SELECT p1.ProductModelID
FROM Production.Product AS p1
GROUP BY p1.ProductModelID
(SELECT AVG(p2.ListPrice)
FROM Production.Product AS p2
GO
USE AdventureWorks;
GO
(SELECT SalesPersonID
FROM Sales.SalesOrderHeader
WHERE SalesOrderID IN
(SELECT SalesOrderID
FROM Sales.SalesOrderDetail
WHERE ProductID IN
(SELECT ProductID
FROM Production.Product p
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
ORDER BY SalesOrderID;
GO
USE AdventureWorks;
GO
SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
ORDER BY ProductID;
GO
USE AdventureWorks;
GO
FROM Production.Product
GROUP BY ProductModelID
ORDER BY ProductModelID;
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GROUP BY ProductID
ORDER BY AVG(UnitPrice);
GO
USE AdventureWorks;
GO
SELECT ProductID
FROM Sales.SalesOrderDetail
GROUP BY ProductID
ORDER BY ProductID;
GO
USE AdventureWorks ;
GO
FROM Sales.SalesOrderDetail
ORDER BY SalesOrderID ;
GO
USE AdventureWorks;
GO
SELECT ProductID
FROM Sales.SalesOrderDetail
GROUP BY ProductID
ORDER BY ProductID;
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GROUP BY ProductID
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $2000000.00;
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GROUP BY ProductID
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
ORDER BY ProductID
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
COMPUTE SUM(OrderQty), SUM(LineTotal);
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
ORDER BY ProductID
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
WHERE UnitPrice < $5.00
GROUP BY ProductID
ORDER BY ProductID;
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
COMPUTE SUM(SUM(LineTotal));
GO
USE AdventureWorks;
GO
GO
USE AdventureWorks;
GO
ON e.BusinessEntityID = pp.BusinessEntityID
GO
USE AdventureWorks;
GO
FROM Sales.SalesOrderDetail
GO
USE AdventureWorks;
GO
SELECT *
FROM HumanResources.Employee AS e1
UNION
SELECT *
FROM HumanResources.Employee AS e2
GO
USE AdventureWorks;
GO
GO
INTO dbo.Gloves
FROM Production.ProductModel
GO
USE AdventureWorks;
GO
FROM Production.ProductModel
UNION
FROM dbo.Gloves
ORDER BY Name;
GO
USE AdventureWorks;
GO
GO
GO
INTO dbo.Gloves
FROM Production.ProductModel
GO
USE AdventureWorks;
GO
INTO dbo.ProductResults
FROM Production.ProductModel
UNION
FROM dbo.Gloves;
GO
SELECT *
FROM dbo.ProductResults;
USE AdventureWorks;
GO
GO
INTO dbo.Gloves
FROM Production.ProductModel
GO
/* INCORRECT */
USE AdventureWorks;
GO
FROM Production.ProductModel
ORDER BY Name
UNION
FROM dbo.Gloves;
GO
/* CORRECT */
USE AdventureWorks;
GO
FROM Production.ProductModel
UNION
FROM dbo.Gloves
ORDER BY Name;
GO
USE AdventureWorks;
GO
GO
GO
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle
INTO dbo.EmployeeOne
ON e.BusinessEntityID = pp.BusinessEntityID
GO
INTO dbo.EmployeeTwo
ON e.BusinessEntityID = pp.BusinessEntityID
GO
INTO dbo.EmployeeThree
ON e.BusinessEntityID = pp.BusinessEntityID
GO
-- Union ALL
FROM dbo.EmployeeOne
UNION ALL
FROM dbo.EmployeeTwo
UNION ALL
GO
FROM dbo.EmployeeOne
UNION
FROM dbo.EmployeeTwo
UNION
FROM dbo.EmployeeThree;
GO
FROM dbo.EmployeeOne
UNION ALL
FROM dbo.EmployeeTwo
UNION
FROM dbo.EmployeeThree
);
GO