Professional Documents
Culture Documents
Harder Than Average
Harder Than Average
Harder Than Average
1. If you haven't already done so, run the stored procedure in the above folder to
generate a database of training courses and delegates. This exercise uses the
following tables:
Write code to set the variable @CourseName to hold the name of schedule number 1
(you should find it set to VISUAL C# 2005 WINDOWS FORMS DELEGATES).
Write code to set the variable @People to hold the accumulated names of the
delegates on this course, and then print out what you've accumulated:
The output you should get
Optionally, save this query as Display accumulated delegate names.sql, then close it
down.
Within this stored procedure, create a CTE giving for each website the usage proportion
for that website for each of the two countries specified:
For Facebook, for example, 5.1% of users are French, but only 0.9% Greek
Create a SELECT statement based on this CTE to show the results with a total:
We now learn that taken together French and Greek users comprise 6% of Facebook users, for
example
Write some sort of query, somehow, which takes in any comma-delimited id string of
trainer ids as input:
Your query should, somehow, turn this into a comma-delimited string of trainer names:
You can use any method you like - the model answer combines a multi-statement table-valued
function and a stored procedure, but as with all things SQL there are probably lots of other, better
approaches!
Create a stored procedure called spNames which takes a ScheduleId as input, and
outputs a comma-delimited list of the people on this course - for example:
Creates a temporary table called #Schedules to hold the schedule id, start date,
course name and list of people for each course of interest
Uses a cursor to loop through the list of courses containing SQL in the name,
calling the spNames procedure for each and adding one row to
the #Schedules table
Uses a SELECT statement to output the rows from the #Schedules table
The final output from running this query should be something like this:
Optionally, save your work as List person names.sql, then close it down.
Create a function to return the domain suffix from any website URL (eg the Wise Owl
website URL would return .uk).
You may find it easier to use the reverse function to reverse the order of the URL, then use
the charindex function to find the first full stop. Don't forget to check for errors -
if charindex returns 0, then this isn't a valid URL!
Use your function to show all websites where the domain reported by
the DomainId column isn't the same as the actual domain suffix:
Your query should show 7 sanitised adult sites and one Mexican mistake!
Optionally, save the code to generate this function as Return country domain.sql,
then close it down.
Create a query to list the events by year and month, with the most recent event coming
first. You may find the syntax of the DatePart function useful:
Your final output should look like this:
If you get this working, try adding WITH CUBE or WITH ROLLUP immediately after
your GROUP BYclause to see the effect of this. You should find that
the ROLLUP option gives you yearly subtotals, while the CUBE option also gives you
totals by month at the end.
You'll need to create a join from the film table to the cast table, and another join from the cast table
to the actor table.
If you have time, write another query to show that there are no actors who do not
appear in any films in the database.
Save your query - somewhat pretentiously - as Filmes sans acteurs, then close it
down.
The database also contains a list of scheduled courses called tblSchedule, showing for
each course which resources it uses:
The first course, for example, uses resources 574 and 595 - ie SPECIAL EQUIPMENT and a FLIP
CHART
You can use either an output parameter or a return value to return the number (the
example below uses both, which is definitely overkill):
This is what calling your stored procedure could look like
To work out which courses use a resource, add a comma before and after both
the ResourceIds column value and the resource id that you're looking for - then it won't matter
whether the resource that you're searching for is the first, last or only one in its list.
Optionally, save the query to generate this stored procedure as Resource course
count.sql, then close it down.
9. Create a single query to show the first and last 5 events in alphabetical order:
Note that the column names have different headings
Save this query as Show top and bottom 5 events.sql, then close it down.
The database also contains a table called tblSchedule, containing courses actually
running:
Ignore the TrainerIds and ResourceIds columns for now
Create a query using an inner join between these two tables. Your query should read
the following columns for schedule number 17 (not shown above) into 3 variables:
Extend this query so that it prints out this information, using the 3 variables that you
have created and assigned values to:
Optionally, save this query as Assign course columns to variables.sql, then close it
down.
10 if this person has attended 1 or less courses (ie if the number of rows in
the tblDelegatetable for this PersonId is less than or equal to 1)
20 if this person has attended 2 to 4 courses; or
30 otherwise
Extend this query so that it deletes all "unimportant" people (ie those whose importance
is 10), and prints out a message saying how many rows have been deleted:
commits the transaction if there are still at least 500 people left in the table; or
rolls it back otherwise
Optionally, save this query as How not to run a training company.sql, then close it
down.
12. Create a query to show the number of events for each decade:
The decade names have been chosen to ensure that they appear in the correct order in our query
You'll need a fairly long CASE statement to do this, and a fair bit of patience.
13. Create a query to show for each event the full date in the format shown below:
For an encore, and only if you have the time and energy, show that:
If you run the command spEvents 'k', you should get this:
If you run the command spEvents 'k', 's' you should get this: