You are on page 1of 2

3. Write at least 8 such queries.

Each query should include more complex operations


like different types of joins, grouping sorting and aggregate functions in all the
possible places that were mentioned in the lesson

SELECT Production.ProductCategory.ProductCategoryID,
SUM( Sales.SalesOrderDetail.LineTotal) AS TotalSalesForProductCategory
FROM Sales.SalesOrderDetail CROSS JOIN
Production.ProductCategory
GROUP BY Production.ProductCategory.ProductCategoryID
HAVING Production.ProductCategory.ProductCategoryID = 1;

_____________________________________________________________

SELECT MAX([DaysToManufacture]) AS LongestProductionTime


FROM[Production].[Product];

_____________________________________________________________

SELECT [Name],
AVG([ReorderPoint]) AS AverageReorderPoint
FROM[Production].[Product]
GROUP BY [Name]
ORDER BY [Name] DESC;

_____________________________________________________________

SELECT [Name],MIN([ProductListPriceHistory].[ListPrice]) AS MinimalPrice


FROM [Production].[ProductListPriceHistory]
JOIN [Production].[Product]
ON [ProductListPriceHistory].[ProductID] = [Product].[ProductID]
GROUP BY [Product].[Name];

_________________________________________________________________

SELECT
[Person].[Person].BusinessEntityID,
[Bonus],
[SalesLastYear],
[SalesQuota],
SUM([SalesLastYear]) OVER (PARTITION BY [SalesQuota])
FROM [Sales].[SalesPerson]
JOIN [Person].[Person]
ON[Person].[Person].BusinessEntityID = [Sales].[SalesPerson].BusinessEntityID

_____________________________________________________________________

SELECT
[Person].[Person].BusinessEntityID,
[Bonus],
[SalesLastYear],
[SalesQuota],
COUNT(*) OVER (PARTITION BY [SalesQuota])
FROM [Sales].[SalesPerson]
JOIN [Person].[Person]
ON[Person].[Person].BusinessEntityID = [Sales].[SalesPerson].BusinessEntityID
__________________________________________________________________________

SELECT customerid, salespersonid, AVG(subtotal) AS avg_subtotal,


SUM(subtotal) AS sum_subtotal
FROM sales.salesorderheader
GROUP BY customerid, salespersonid
ORDER BY customerid DESC;

______________________________________________________________________

SELECT a.City, COUNT(b.AddressID) NoOfEmployees


FROM Person.BusinessEntityAddress AS b
INNER JOIN Person.Address AS a
ON b.AddressID = a.AddressID
GROUP BY a.City
ORDER BY a.City;

You might also like