Sunteți pe pagina 1din 6

How to take this test

1) Download and install the FREE Microsoft SQL 2005 Express edition from here:
http://msdn2.microsoft.com/en-gb/express/bb410791.aspx

2) Having installed it, open the SQL Server 2005 Management Studio Express and
connect to you new local DB server.

3) Create a database called “mdsl”. In a query window, run all the code found in
sqltest.sql. This will create all the parts necessary for this test.

4) In a new query window begin trying to answer the questions. Once you have
answered a question please copy and paste the command into this document under the
question you are answering.

5) Please provide your name in the space provided.

The Test

YOUR NAME

For all the following questions, please provide answers using a single query
where possible.
In the database there are two tables.
Table Name TEQUIPMENT
Description A table of equipment records and prices.
Fields
equipment record pointer whose number is assigned by the
database server
vendor record pointer to TVENDOR vendor field
model String of 128 characters
serialno String of 128 characters
price Number with 2 decimal places. This is the Monthly
price in US Dollars.

Table Name TVENDOR


Description A table of vendors
Fields
vendor record pointer whose number is assigned by the
database server
vendorname String of 128 characters
parentvendor record pointer to TVENDOR vendor
Data
TVENDOR
Field
vendor vendorname parentvendor
1 Bloomberg NULL
2 Bloomberg Asia 1
3 Reuters Thomson NULL
4 Reuters 3
5 Thomsom Financial 3
6 4CAST NULL
TEQUIPMENT
Field
equipment vendor model serialno
price
1 1 Open Bloomberg ABCD1234 1500
2 1 Open Bloomberg ABCD1240 1450
3 1 Bloomberg Anywhere ABCD9000 100
4 2 Open Bloomberg NULL 1350
5 4 Reuters Terminal REUT8888 250
6 4 Reuters Terminal REUT9999 125.55
7 4 Reuters Terminal REUT TBA 0
Q0 After completing this test please answer honestly how long you did it take you to
answer the questions you attempted. How long did you take?

Q1 Write SQL commands to create the TVENDOR and TEQUIPMENT tables.


These tables must be owned by the database owner.

Q2 Write an SQL command to find out the definition of the TEQUIPMENT table.

Q3 Write an SQL command to add new column installdate to the TEQUIPMENT.


This is a date field to store when the equipment was installed. The field is to be
added when there are already records in the table.
Q4 Write an SQL command to populate the existing TEQUIPMENT data with the
installdate of 1st January 2007.

Q5 Write a Constraint on the TEQUIPMENT table to stop vendor pointers which do


not exist in the TVENDOR table.
The query should be written to work whatever data was actually in the two tables.

Q6 Write an SQL command to add a new equipment record for


Vendor = Reuters
Model = Reuters Terminal
SerialNo = REUT0000
Price = 125.55
Installdate = Todays Date – Please use a SQL function to get this.

Q7 Write an SQL command to return the Vendor Name and Parent Vendor NAME of
all vendors only if they have one. Note: The query should return the Parent
Vendor Name NOT the Parent Vendor record pointer number.

Q8 Write an SQL command to return the Vendor Name and Parent Vendor Name of
all vendors. If they do not have a parent vendor return the text ‘No Parent
Vendor’.

Q9 Write an SQL command to return the Vendor Name, Model and SerialNo where
there is NO serialNo or the 5th character in the serialNo is not a number.

Q10 Write an SQL command to return the Vendor Name, Model and number of pieces
of equipment they have, even if they do not have any.

Q11 Write an SQL command to return the Vendor Name, Model, SerialNo and Price
of the 3rd most expensive item ONLY, whatever records are in TEQUIPMENT.
Note: This query should ONLY return one record.

Q12 Write an SQL command to create a View called ‘VEQUIPMENT’ which only
returns the Vendor Name, Model, SerialNo, Price and Install Date fields.

Q13 Write an SQL command to write the view ‘VEQUIPMENT’ into a temporary table.

Q14 Write a Trigger to cause an error if an installdate before 31st December 1999 is
attempted to be entered.

Q15 What is the difference between the following commands?


truncate table TEQUIPMENT
delete from TEQUIPMENT

Q16 Write an SQL function or stored procedure that will reverse the string
‘Hello World’. Please do not use any existing internal SQL functions e.g.reverse()

Q17 a) Write a statement to find out ALL the current users connected to the database
server.
b) Write a statement to find out the database user name of your connection to a
database, assuming you had an active connection.
There is a table called ‘Personnel’ in it.
Field
personnel Record Pointer
lastname String of 60 characters
firstname String of 60 characters

There is a ‘Login’ table of logins for these users. These logins are the different
login names the user has to different systems within their organization.
Field
login Record Pointer
loginname String of 60 characters. This is the login name. Each user can
have multiple login names
personnel Pointer to the ‘Personnel’ table.

The data was entered to allow duplicate lastname+firstname into the ‘Personnel’
table. This data is included in the sqltext.sql script. For example there are
multiple lastname=’Allen’ firstname=’David’ records.

Q18 Write a stored procedure to remove the duplicate ‘Personnel’ records and update
the ‘Login’ table so the ‘Login.personnel’ data is still valid.
Please write the procedure so it has quickest execution time possible.

If you need to restore the tables to their initial state run the command
exec RestoreQuestion

Optional Questions.
Q19 Please correct all the potential errors with the following SQL command
Select personnel, uppercase(B.loginname)
from Personnel A,Login b
where A.personnel=B.personnel

Q20 Write the create statements from Question 1 so that Japanese characters could
be entered into the text strings. For example

Q21 Write a stored procedure to carry out the following actions in the most efficient
execution time for a mythical client. The client expects the best solution but has
given you the requirements as they are written below and gone into an
uninterruptable meeting. They have set you a maximum 1 hour deadline
otherwise they will go to another vendor.
a. Create a table if it is not already there called ‘TMAGIC’. This table has three
fields a text string called ‘magicname’ of 20 characters, a field of absolute whole
numbers called ‘price’ and a field ‘department’ of 20 characters.
b. Insert the table with exactly 150,002 records. The text strings are to be made
up of random length A-Z characters but never the letter M. However there MUST
always be a least 1 record of all possible field lengths i.e. 0 to 20.
The price number is a random between 0-16000.
The departments are random from the following list
Sales
Support
Development
Accounts
Cleaning
HR
Transportation
Research and Market Analysis
c. Return the departments total summed price at 20% discount grouped by
department. This report is for the department managers. For example:-
Sales 536543.23
Support 452323.54
d. Return the average price for each department for the project manager.
e. Add a transaction date field to table and assign a random date between 1st
January 2007 and 1st January 2008.
f. Return results for your employer summing the totals per department per month.
g. Return results for your company working out 1% of the total costs per
department. This report is for the accounts department and will be used as the
invoices between your company and the client.
h. Return the total summed price for the departments based on the length of the
magicname field. This report is for client’s managing director. The project
manager gave you the following hand written suggestion of the style of the
output.
String Accounts Cleaning Development HR Sale Total % of
Length of Total
magicname
0 20 50 20 0 10 100 5
1 20 40 30 90 20 200 10
2 20 20 200 30 30 300 30
Etc to 20

Total 60 110 250 120 60 2000 100

Q22 Please correct all the potential errors with the following SQL command
update TEQUIPMENT set price=11 from
TEQUIPMENT A
where vendor in
(select top 1 vendor,vendorname from TVENDOR
Where vendorname=Bloomberg order by vendorname)

S-ar putea să vă placă și