Vous êtes sur la page 1sur 6

1. select top 3 * from Sales.

SalesPerson
order by Bonus desc

select top 1 * from(select top 2 * from Sales.SalesPerson

order by Bonus desc)a order by a.Bonus

2. select * from Sales.Store where Store.Name not like '%Bike%'

3. delete Sales.Customer output deleted.*


where customerType='S'

4. create function fnEmployeeDetails(@empid int)

returns @EmployeeData table

empid int not null,

contactid int not null,

firstname varchar(50)not null,

lastname varchar(50),

managername varchar(100),

hiredate datetime

as

begin

declare @managerID int

set @managerID=(select ManagerID from HumanResources.Employee where Employe


eID=@empid)

;with EmpDetailsCTE as

select e.EmployeeID,e.ContactID,c.FirstName,c.LastName,e.HireDate from Huma


nResources.Employee ejoin Person.Contact c

on e.ContactID=c.ContactID

),
ManagerDetailsCTE as (select e.EmployeeID,c.FirstName,c.LastName from Human
Resources.Employee ejoin Person.Contact
c on e.ContactID=c.ContactID where c.ContactID in(select ContactID fromHuma
nResources.Employee where EmployeeID=@managerID))

insert into @EmployeeData

select e.EmployeeID,e.ContactID,e.FirstName,e.LastName,m.FirstName+'
'+m.LastName as 'Manager Name',e.HireDate from EmpDetailsCTE
e,ManagerDetailsCTE m where e.EmployeeID=@empid

return

end

5. create proc procVendorDetails @ProductID int

as

select v.VendorID,v.AccountNumber,v.Name from Purchasing.Vendor


v join Purchasing.ProductVendor
pon p.VendorID=v.VendorID where p.ProductID=@ProductID

6. SET TRANSACTION ISOLATION LEVEL

READ COMMITTED or REPEATABLE READ

BEGIN TRANSACTION TR

BEGIN TRY

UPDATE Production.ProductCostHistory

SET StandardCost=15.4588

WHERE ProductID=707COMMIT TRANSACTION TR

PRINT 'Transaction Executed'

END TRY

BEGIN CATCH

ROLLBACK TRANSACTION TR

PRINT 'Transaction Roll backed'

END CATCH

7. create trigger trgUpdateEmployee


on HumanResources.Employee

for update

as

begin

select * from inserted

select * from deleted

end

update HumanResources.Employee

set MaritalStatus='M'

where EmployeeID=2

SQL SERVER SELF ASSESSMENT QUESTIONNARIE


1. Write a query to display the top three salespersons and bonus and in addition write a query
to display the id of the salesperson having second highest bonus.

Hint: Use Sales.SalesPerson

2. Write a query to display the details of those stores, which doesn’t have Bike in their
name.

Hint - Schema name – Sales, Table name- Store

3. Write a query to display the sales order ID and the maximum and minimum values
of the order based on the sales order ID. In addition, ensure that the order amount is
greater than $5,000.

4. Write a query to display the total unit price and the total amount collected after selling the
products, 774 and 777. In addition, calculate the total amount collected from these two
products.

Hint - Use Sales.SalesOrderDetail


5. Write a query to display the no of orders received for each product with the product
name and product number. And display the name of the product having number of
orders greater than 3.

Hint - Schema name – Production, Purchasing Table name- PurchaseOrderDetail,


Product

6. Write a query to display the EmployeeID, FirstName and LastName as Employee


Name, Designation and the HireDate of the employees. The month and the year need to
be displayed as separate columns and the hire date should be displayed in this format
10 Jan 2000.

Hint - Schema name –HumanResources, Person Table name- Employee, Contact

7. Create a table as EmployeeData with following details.


a. EmployeeID auto increament from 1001 onwards.
b. EmployeeName string type max 70.
c. City string type max 30 default Bangalore.
d. WorkingCity string type max 20 and should be only in these values
Bangalore, Chennai, Mumbai, Calcutta.
e. Phone string should be ###-######## in the specific format.
f. Age numeric and should be in between 20 and 60.

8. Alter the above table and implement the following features.


a. Add a new column as Email string type.
b. Remove the default from City column.
c. Remove the constraint from the WorkingCity column.

9. Write a CTE to display the vendor details and product details, where vendors those
who have supplied more than 2 products.

Hint- Schema Name – Purchasing, Production Table Name – ProductVendor, Product

10. Write a query to take the backup of the Purchasing.Vendor table to


Sales.Vender_Test. Then update the vendor name to National Bikes where the vendor
id =1, 4, 10.

Then reflect the same changes to the Sales.Vender_Test table.

Hint- Use Merge

11. Write a query to display a list of dates that orders were made along with the number
of products ordered and the total amount of those products for each date.

Hint- Schema name – Purchasing Table Name – PurchaseOrderHeader,


PurchaseOrderDetail
Hint- Use nested CTE.

12. Write a query to create table type named as EmployeeTest with the columns as
EmployeeID, ContactID, ManagerID, Title. Delete the HumanResources.Employee
table data where the title=marketing assistant. And fill those deleted records to the table
EmployeeTest. Display those deleted records.

Hint- Use output

13. Replace the following query with joins.

SELECT DISTINCT c.LastName, c.FirstName

FROM Person.Contact
c where c.ContactID in(select c.ContactID from HumanResources.Employee e

WHERE EmployeeID IN

(SELECT SalesPersonID

FROM Sales.SalesOrderHeader

WHERE SalesOrderID IN

(SELECT SalesOrderID

FROM Sales.SalesOrderDetail

WHERE ProductID IN

(SELECT ProductID

FROM Production.Product p

WHERE ProductNumber = 'BK-M68B-42'))));

14. Write a query to display each employee's first and last name for which the bonus in
the SalesPerson table is 5000 and for which the employee identification numbers match
in the Employee and SalesPerson tables.

Hint- Schema Name- HumanResources, Sales Table Name- Employee,


SalesPerson
Hint – Use Correlated Sub Query

15. Create a view to display the product details along with their sales details such as –
ProductID, name,product number, color, unit price, sales order id, ordered date, ordered
qty. Delete the product detail from the view where the product id=776 and
Salesorderid=43659. During the delete operation if any error comes then write the
solution for the same.
Hint- Schema name- Production Table name- Product, SalesOrderHeader,
SalesOrderDetail.

16. Write a query to delete the customer details where the CustomerType=’S’. And
display those deleted records.

Hint- Schema name – Sales Table Name- Customer

Hint – Use OUTPUT

17. Create a function to display the EmployeeID, ContactID, FirstName, LastName,


ManagerName, HireDate based on the EmployeeID as the input to the function.

Hint- Schema Name – HumanResources Table Name – Employee, Contact

18. Create a stored procedure to display the vendor details (VendorID,


AccountNumber, Name) based on the ProductID as an input to the procedure.

Hint- Schema Name – Purchasing Table Name – Product, Vendor

19. Suppose you want to update the product cost in the ProductCostHistory to 15.4588
where ProductID is 707. And during this transaction if any error comes then rollback
the update statement. In addition ensure that during the update process no other users
should be able to update data on the same table. How you implement the Transaction
Isolation Level to achieve the above requirement.

20. Create a trigger on the HumanResources.Employee table for update the employee
details. Then update one employee record so that the trigger should display the old
record and the new updated record as the output along with the updated data.

Vous aimerez peut-être aussi