Harder Than Average

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Accumulate names of course delegates

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:

You'll need to join these tables

Create two variables as shown below:

These are only suggested variable names!

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

You can use char(13) + char(10) to throw a carriage return.

Optionally, save this query as Display accumulated delegate names.sql, then close it
down.

Compare usage proportions for 2 countries


2. If you haven't already done so, run the stored procedure in the above folder to
generate a database of the world's leading websites. The tables we will use are:

The tables of interest for this exercise

Create a stored procedure which takes in as input the names of 2 countries:

Here's how we might test our stored procedure

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

Optionally, save your work as Country proportions.sql, then close it down.

Convert trainer id list into trainer name list


3. If you haven't already done so, run the stored procedure in the above folder to
generate a database of training courses and delegates.

Write some sort of query, somehow, which takes in any comma-delimited id string of
trainer ids as input:

An example of a typical input

Your query should, somehow, turn this into a comma-delimited string of trainer names:

What this input would produce

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!

Optionally, save your query as Trainer names.sql, then close it down.


Cursor - build up list of people names per course
4. If you haven't already done so, run the stored procedure in the above folder to
generate a database of training courses and delegates.

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:

The output for schedule number 31, for example

Now write a query which:

 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:

The first few courses you should see listed

Optionally, save your work as List person names.sql, then close it down.

Fn to find domain name using reverse, charindex


5. If you haven't already done so, run the stored procedure in the above folder to
generate a database of the world's leading websites. This exercise refers to the
following two tables from this database:
Each country's domain (eg .UK) can have lots of corresponding websites

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.

Grouping events by year and month


6. USE Historical Events database.

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:

Don't worry about the missing eventless months

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.

Save your query as Events by date, then close it down.

List all films without actors, and vv


7. If you haven't already done so, run the script in the above folder to generate
the Movies database.

Write a query to show the 96 films for which no actors exist:

The first few actorless films, in alphabetical order

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.

List courses for given resource - output param


8. 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 database contains a
table of resources called tblResource:

There are only 5 resources - each has a name and id

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

Write a stored procedure which:

 Takes in the name of a resource as a parameter


 Works out the id of this resource (or returns prematurely if the resource name
doesn't exist in the tblResource table)
 Works out how many courses use this resource (see hint below), and returns this
number

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.

Listing top and bottom 5 events


USE the HistoricalEvents database.

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.

Read row into separate variables, and print out


10. 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 contains a table of
course products called tblCourse:

These are courses which COULD run

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:

 The course name


 The start date
 The number of days the course lasts

Extend this query so that it prints out this information, using the 3 variables that you
have created and assigned values to:

Sample output from your query

Optionally, save this query as Assign course columns to variables.sql, then close it
down.

Set importance column according to no of places


11. If you haven't already done so, run the stored procedure in the above folder to
generate a database of training courses and delegates.

Add a new integer column called Importance to the tblPerson table:

The new column


Write a query which begins a transaction, and then updates the value of
the Importance column for each person to:

 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:

You should see 274 people have been zapped

Finally, add a condition which:

 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.

Show events by decade


USE the Historical Events database

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.

Save this query as Events by decade, then close it down.


Show the full date, plus Friday 13th dates
USE the Historical Events database.

13. Create a query to show for each event the full date in the format shown below:

You will need to use DatePart or (better) DateName for this

Save this query as Full date, then close it down.

For an encore, and only if you have the time and energy, show that:

 There weren't any events which took place on Friday 13th


 The only event which took place on Thursday 13th was the instigation of the
Public Record Act of July 1967
 There were 3 events which took place on Saturday 13th, one of which wasn't a
good day for Saddam Hussein

Write an essay explaining the significance of your findings in sociological terms!

Showing eg FRANCE HAS 6 LETTERS


USE the Historical Events database.

14. Create a query to list out country names as follows:


This is harder than it looks!

Save this query as Length of country name, and close it down.

SP to return events with first/last letter


15. Create a stored procedure called spEvents to show all events which:

 Start with a given letter


 End with a separate given letter (which may be omitted)

If you run the command spEvents 'k', you should get this:

Four events start with the letter K ...

If you run the command spEvents 'k', 's' you should get this:

... but only 2 of them end with an S

Save and close your queries.

You might also like