You are on page 1of 11

WEEKLY TEST

Question1. What does this return?

createtable Mytrantest
(
OrderId intPrimarykeyIdentity

Begintran Transtart
Insertinto Mytrantest
defaultvalues
Savetran firstpoint

insertinto Mytrantest
defaultvalues

rollbacktran firstpoint

Committran transtart

Select*from Mytrantest

Question2. You are creating a stored procedure that will be called by an application and
will access data stored in one of the databases. The stored procedures must be
configured to return the current value of a parameter to the calling application. What
must you do?

A. Specify an output parameter in the stored procedure using the OUTPUT keyword.
B. Include a select statement in the stored procedure to return the necessary value
C. Specify a return code for the stored procedure using the RETURN statement.
D. Declare a variable using the DECLARE statement

Question3. You have created a view that reference columns from multiple base tables.
You want to modify data in the underlying tables through the view using an UPDATE
statement. What must be done to allow this modification to occur?

A. Create an AFTER trigger to execute after the UPDATE trigger.


B. Alter the view definition to include the SCHEMABINDING clause.
C. Create an INSTEAD OF UPDATE Trigger to make the required changes.
D. Nothing; executing the UPDATE statement with the required changes is all needed.

Question4.Company policy requires that all sessions accessing the database be


insulated from dirty reads. Non Repeatable reads and phantom reads are acceptable.
What is the lowest isolation level you can set to meet theses requirement ?

A. Read Uncommited
B. Read Commited
C. Repeatable Read
D. Serializable

Scenario Based Question: -There are two tables (User&UserHistory) as shown below:-

User
user_id
name
phone_num

UserHistory
user_id
date
action

Given the tables above, find the following:

Question5.Given the tables above, write a SQL query to determine which user_ids in the User
table are not contained in the UserHistory table (assume the UserHistory table has a subset of
the user_ids in User table).

Question6. What does this return?

createtable Mytrantest
(
OrderId intPrimarykeyIdentity

Begintran Transtart
Insertinto Mytrantest
defaultvalues
Savetran firstpoint

insertinto Mytrantest
defaultvalues

rollback

Select*from Mytrantest

Question7. Scenario Based Question: - There are two tables (Salesperson,


Customer&Orders) as shown below:-
Salesperson Customer
ID Name Age Salary ID Name City Industry Type
1 Abe 61 140000 4 Samsonic pleasant J
2 Bob 34 44000 6 Panasung oaktown J
5 Chris 34 40000 7 Samony jackson B
7 Dan 41 52000 9 Orange Jackson B
8 Ken 57 115000
11 Joe 38 38000
Orders
Number order_date cust_id salesperson_id Amount
10 8/2/96 4 2 2400
20 1/30/99 4 8 1800
30 7/14/95 9 1 460
40 1/29/98 7 2 540
50 2/3/98 6 7 600
60 3/2/98 6 7 720
70 5/6/98 9 7 150

Question7. Find the largest order amount for each salesperson and the associated order number,
along with the customer to whom that order belongs to.

Question8. Write syntax to create a View name CustomerOrders_vw to display amount of each
salesperson and the associated order number, along with the salesperson name.

Question9. Write syntax to encrypt the above View name CustomerOrders_vw.

Question10. Write syntax to drop the above View name CustomerOrders_vw.

Question11. Write syntax to alter the above View name CustomerOrders_vw in order to bind with
underlying tables

Question12. You have created the stored procedure, shown below, to report the year-to-date sales
for a specific book title.
Createprocedure get_sales_for_title
@title varchar(80), @ytd_sales intoutput
AS
SELECT @ytd_sales= ytd_sales from titles where title=@title
IF@@rowcount=0
Return (-1)
Else
Return 0

You are now creating a script that will perform this stored
procedure. The stored procedure should report the year-to-date
sales for the book title if it executes successfully. If,
however, the stored procedures fails to execute, it should
report following message:
‘ No Sales Found’
Which of the following illustrates how the script required to
achive this should be created?
A.Declare @retval int
Decalre @ytd int
Exec get_sales_for_title‘Net Etiquette’,@ytd
IF @retval <0
Print ‘No Sales found’
ELSE
Print ‘Yeartodatesales: ‘+STR(@ytd)
GO

B.Declare @retval int


Decalre @ytd int
Exec get_sales_for_title‘Net Etiquette’,@ytd OUTPUT
IF @retval <0
Print ‘No Sales found’
ELSE
Print ‘Yeartodatesales: ‘+STR(@ytd)
GO

C. Declare @retval int


Decalre @ytd int
Exec get_sales_for_title‘Net Etiquette’,@retval OUTPUT
IF @retval <0
Print ‘No Sales found’
ELSE
Print ‘Yeartodatesales: ‘+STR(@ytd)
GO

D. Declare @retval int


Decalre @ytd int
Exec @retval=get_sales_for_title‘Net Etiquette’,@ytd OUTPUT
IF @retval <0
Print ‘No Sales found’
ELSE
Print ‘Yeartodatesales: ‘+STR(@ytd)
GO
Question13. What does this return?

createtable UnionTest1
(
idcol intIDENTITY,
col2 char(3)
)

createtable UnionTest2
(
idcol intIDENTITY,
col4 char(3)
)

Insertinto UnionTest1
VALUES ('AAA')
Insertinto UnionTest1
VALUES ('BBB')
Insertinto UnionTest1
VALUES ('CCC')

Insertinto UnionTest2
VALUES ('CCC')

Insertinto UnionTest2
VALUES ('DDD')

Insertinto UnionTest2
VALUES ('EEE')

SELECT col2 from UnionTest1


UNIONALL
SELECT col4 from UnionTest2

Question14. What would happen when you execute the code given below in query
Analyzer?

createPROCEDUREsp_who
AS
PRINT'SURPRISE'
GO
EXECUTEsp_who

Question15. On Friday, you issued several INSERT statements using Query Analyzer.
You then verified the data had been correctly entered with a SELECT statement. On
Monday, your users report that data is not there. What happened?

Scenario based question

There are two tables (Employee_Test, Employee_Test_Audit) as shown below:-


Employee_Test Employee_Test_Audit
Emp_ID Emp_Name Emp_Sal Emp_ID Emp_Name Emp_Sal Audit_Action DateTime
1 Anees 1000.00
2 Rick 1200.00
3 John 1100.00
4 Stephen 1300.00

Question16. Now write syntax to create AFTER INSERT Trigger which will insert the
rows inserted in Employee_Test table into Employee_Test_Audit table.

Question17. Now write syntax to create AFTER DELETE Trigger (name it as


trgAfterDelete) which will insert the rows into Employee_Test_Audit table that has
been deleted from Employee_Test table.

Question18. Write syntax to disable trigger trgAfterDelete on Employee_Test table

Question19. Write syntax to enable trgAfterDelete trigger on Employee_Test table

Question20. Write syntax to create INSTEAD OF DELETE Trigger (name it as


trgInsteadOfDelete) so that whenever user try should not be able to delete the record
from Employee_Test table whose salary is greater than 1200 and the delete operation
should be rolled back.

Scenario based question

There are two tables (EmployeeInfo, EmpProjInfo) as shown below:-

EmployeeInfo
EmpId EmpName EmpLogin Emppassword EmploymentDate

1 Vivek Johari Vivek VikJoh 29/01/2006

2 Virender Singh Virender Virender 06/02/2007

3 Raman Thakur Raman Raman 14/05/2007

4 Uma Dutt Sharma Uma Uma 30/03/2008

5 Ravi Kumar Thakur Ravi Ravi 30/06/2007

EmpProjInfo

EmpId Projectname

1 IBS
EmpId Projectname

2 Tata Steel

3 Godrej

4 OITDS

5 Godrej

Question21. Write syntax to create a view which gives the empid, empname,
employmentdate as the output.

Question22. Write syntax to create a view which gives the information about the
employees and its projects as the output.

Question23. Write syntax to encrypt the above View name

Question24. Write syntax to ALTER a view which gives the information about the
employees and its projects who are working on Godrej Project.

Question25. Write syntax to alter the above View name in order to bind with underlying
tables

Question26. Write syntax to delete the view?

Scenario Based: - there is a table called tbl_Students whose structure is given below:

CREATE TABLE tbl_Students

(
[Studentid] [int] IDENTITY(1,1) NOT NULL,
[Firstname] [nvarchar](200) NOT NULL,
[Lastname] [nvarchar](200) NULL,
[Email] [nvarchar](100) NULL
)
Studentid Firstname Lastname Email
1 Vivek Johari vivek@abc.com
2 Pankaj Kumar pankaj@abc.com
3 Amit Singh amit@abc.com
4 Manish Kumar manish@abc.comm
5 Abhishek Singh abhishek@abc.com

Question27. Write syntax to create a stored procedure which will returns student name
whose Studentid is given as input parameter.
Question28. Write syntax to execute the above stored procedure

Question29. Write syntax to create a stored procedure which will returns student name
in the output parameter.

Question30. Write syntax to execute the above stored procedure.

Question31. Write a syntax to alter a stored procedure which will returns student email
address along with student name in the output parameter.

Question32. Write syntax to execute the above stored procedure.

Question33. Write syntax to drop the stored procedure.

Scenario based Question:- You are working as a database developer at CMC. CMC has
a database named HumanResources that contain information about all employees and
office locations. The database also includes information about potential employees and
office locations, which are stored in the tables as shown in the exhibit below:

Existing employees are assigned to a location and existing location have one or more
employees assigned to them. Potential employees are not assigned to a location, and
potential office locations do not have any employees. You have received instruction
from CEO to create a report that displays all existing and potential employees and office
locations. The report has to list every existing and potential location, followed by any
employees who have been assigned to that location. Also, potential employees have to
be listed together.

Question34. Of the following script, which is the one you should employee to achieve
this objective?

A.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e LEFT OUTER JOIN Location as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName

B.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Location AS l LEFT OUTER JOIN Employee as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName
C.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e FULL OUTER JOIN Location as l
ON e. LocationId = l. LocationId
ORDER BY l.LocationName, e.FirstName, e.LastName

D.
SELECT l.LocationName, e.FirstName, e.LastName
FROM Employee AS e FULL OUTER JOIN Location as l
ORDER BY l.LocationName, e.FirstName, e.LastName

Question35. Which of the above script you should use to create a list of all possible
location and employee combinations?

Question36. There are two tables as shown in the exhibit below:-

Create table Countries


(
CountryID int IDENTITY(2,2) NOT NULL,
CountryName char(20) NOT NULL,
)

Create table Instructors


(
InstructorID int NOT NULL,
FirstName char(20) NOT NULL,
LastName char(20) NOT NULL,
Title char(5) Null,
Country char(20) NULL
)

You must populate the Countries table by moving the country information from the
Instructors table to the Countries Table. You must accomplish this task by using cursor.
Write Transact-SQL query to accomplish the job.

Question37. All customer companies that purchase significant amount of products are
offered discounts. Prices of the products with a category ID of 15 have been increased
by 10%. To compensate for customer companies that placed there orders on the day of
the price increase the discounts for those products must also be increased by 10%.
Both UPDATE statements must be implemented together. In the event of either update
failing, then the other update must not be implemented.

What should you do?

A. You should use the following script:

BEGIN TRAN
UPDATE Products SET Unitprice = Unitprice * 1.1 WHERE CategoryID =15
UPDATE OrderDetails SET Discount=Discount*1.1
FROM OrderDetails od JOIN Products p ON od.PRODUCTID= p.PRODUCTID
JOIN orders o ON o.OrderID= od. OrderID
WHERE CategoryID =15 AND OrderDate= '18 NOV 2013'
COMMIT
B. You should use the following script:

UPDATE Products SET Unitprice = Unitprice * 1.1 WHERE CategoryID =15


UPDATE OrderDetails SET Discount=Discount*1.1
FROM OrderDetails od JOIN Products p ON od.PRODUCTID= p.PRODUCTID
JOIN orders o ON o.OrderID= od. OrderID
WHERE CategoryID =15 AND OrderDate= '18 NOV 2013'

C. You should use the following script:

BEGIN TRAN
UPDATE Products SET Unitprice = Unitprice * 1.1 WHERE CategoryID =15
COMMIT

BEGIN TRAN
UPDATE OrderDetails SET Discount=Discount*1.1
FROM OrderDetails od JOIN Products p ON od.PRODUCTID= p.PRODUCTID
JOIN orders o ON o.OrderID= od. OrderID
WHERE CategoryID =15 AND OrderDate= '18 NOV 2013'
COMMIT

Question38. There is an employee table as shown in the exhibit below:-


CREATE TABLE Employee
(
EmpID int PRIMARY KEY,
EmpName varchar (50) NOT NULL,
Salary int NOT NULL,
Address varchar (200) NOT NULL,
)

EmpID EmpName Salary Address


1 Mohan 12000 Noida
2 Pavan 25000 Delhi
3 Amit 22000 Dehradun
4 Sonu 22000 Noida
5 Deepak 28000 Gurgaon

Write a Transact SQL Script to produce the result as shown below. You must use the
CURSOR.

ID : 1, Name : Mohan, Salary : 12000


ID : 2, Name : Pavan, Salary : 25000
ID : 3, Name : Amit, Salary : 22000
ID : 4, Name : Sonu, Salary : 22000
ID : 5, Name : Deepak, Salary : 28000
Question39. What are the results of running this script?
Create Proc Test2
AS
Create table #t(x int primary key)
insert into #t values(2)
select test2col= x from #t
GO

Create Proc Test1


AS
Create table #t(x int primary key)
insert into #t values(1)
select test2col= x from #t
exec test2
GO

Create table #t(x int primary key)


insert into #t values(99)
go

exec test1
go

Question40. Which is correct in the following T-SQL statement?

A. UPDATE customer
set c.name =c.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid

B. UPDATE c
set c.name =u.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid

C.UPDATE customer
set name =c.name
FROM customer c, customerdetail cd
WHERE c.custid=cd.custid
D.None of the Above

You might also like