You are on page 1of 2

Display the Country Region Names, CountryRegionCode, SalesYTD, SalesLastYear of

regions that have last year sales higher than average sales.

SELECT [Name], [CountryRegionCode], [SalesYTD], [SalesLastYear]


FROM [Sales].[SalesTerritory]
WHERE [SalesLastYear] >
(SELECT AVG([SalesLastYear])
FROM [Sales].[SalesTerritory]);
______________________________________________________________________
The following query selects the Territory with the highest average sales bonus.
The subquery finds the average bonus for each territory, and then the main query
selects the territory with the highest average bonus.

SELECT
[Sales].[SalesTerritory].[Name], AVG([Bonus])
FROM
[Sales].[SalesPerson]
JOIN [Sales].[SalesTerritory]
ON [Sales].[SalesPerson].[TerritoryID] = [Sales].[SalesTerritory].[TerritoryID]
GROUP BY [Sales].[SalesTerritory].[Name]
HAVING AVG([Bonus]) >= ALL
(SELECT
AVG([Bonus])
FROM [Sales].[SalesPerson]
GROUP BY [TerritoryID]);

__________________________________________________________________________
Display the territory`s name, SalesLastYear, CostLastYear, Currency name where
currency code is 'USD';

SELECT
[Sales].[SalesTerritory].[Name], [SalesLastYear], [CostLastYear],[Sales].
[Currency].[Name]
FROM [Sales].[Currency]
CROSS JOIN [Sales].[SalesTerritory]
WHERE [Sales].[Currency].[Name] = ANY
(SELECT[Sales].[Currency].[Name]
FROM [Sales].[Currency]
WHERE [CurrencyCode] = 'USD');

___________________________________________________________________________
What are the number of products in each category?

SELECT B.[Name], COUNT(PP.[Name]) AS [No. of Products in Category]


FROM [Production].[Product] as PP
INNER JOIN(SELECT [Name], ProductCategoryID
FROM [Production].[ProductCategory]) B
ON PP.[ProductSubcategoryID] = B.[ProductCategoryID]
GROUP BY B.[Name];

____________________________________________________________________________
Display hire date of Employees of type 'EM'

SELECT P.[PersonType], P.[FirstName], P.[LastName],


(SELECT [HireDate]
FROM [HumanResources].[Employee] AS H
WHERE H.[BusinessEntityID] = P.[BusinessEntityID] ) AS HireDate
FROM [Person].[Person] AS P
WHERE [PersonType] = 'EM'
ORDER BY [HireDate] DESC;

____________________________________________________________________________
Set price = 30 for products that have price lower than the minimum price;

UPDATE [Production].[Product]
SET [ListPrice] = 30
WHERE [ListPrice] <
(SELECT MIN([StandardPrice])
FROM [Purchasing].[ProductVendor]);

_____________________________________________________________________________
Delete all products wich price is lower than average product last receipt cost

DELETE FROM [Production].[Product]


WHERE [ListPrice] <
(SELECT AVG([LastReceiptCost])
FROM [Purchasing].[ProductVendor]);

____________________________________________________________________________
Write the long name of person types;

SELECT CONCAT([FirstName], ' ' ,[LastName]) AS FullName,


[TypeName] = CASE [PersonType]
WHEN 'EM' THEN 'Employes'
WHEN 'SC' THEN 'Secondar'
ELSE 'Unknown'
END,
[PersonType]
FROM [Person].[Person];

You might also like