Professional Documents
Culture Documents
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:
Here are some ways in which you could test your stored procedure:
These calls to your procedure should return 1 (if you're looking for an exact company name
match), 20 and 124 delegates respectively.
Optionally, save the query to generate your stored procedure as Delegates for course
and company.sql, then close it down.
2. Create a join to list out all of the Asian events in your database. This is trickier
than it looks, as you'll have to create an inner join not just to
the tblCountry table, but also to the tblContinent table so that you can put in
a WHERE clause:
The final result of running your query should look like this:
Here the events are listed in date order
3. Use a CASE statement to show the type for each event, using the following
rules:
The final results from running the query should look like this:
4. Create a query which lists out all of the events to do with Concorde (ie which
have Concordesomewhere in the Description column):
Include the CountryId in your query, for reasons which will become apparent
Now add another WHERE clause to your query so that it only shows events to do with
Concorde which took place in country number 6 (France):
Discussion for the day - is the withdrawal of fast Concorde flights the only example in history of
mankind going backwards technologically?
5. Create a query to list out the names of the first half of the countries in
the tblCountry table.
To put this a more helpful way, your query should list out the TOP 50 PERCENT of countries!
Save this query as Listing first half of countries, and close it down.
6. This exercise will create a procedure to allow you to move a person to a different
organisation. Your procedure will begin in a similar way to this:
The two arguments are the person to move, and the organisation to move them to
Existing constraints will ensure that you can only move someone to a company which already exists
Optionally, save the code to generate your stored procedure as Log person
moves.sql, then close it down.
7. Create a query listing the events in the database, starting with the closest to your
birthday and finishing with the furthest away:
The results for an imaginary Wise Owl trainer born on 4th March 1964
You will need to use the ABS function, which returns the absolute value of a number. For example,
ABS(-8) = 8.
You could use your function to show all of the characters containing BEN
When you run the sample command shown above, for example, you should see:
Now write a SELECT statement which joins the results of your function to
the tblDirector table to show the people who have directed films containing characters
with particular names!
The results for BEN again - Steven Soderbergh dominates!
Save the query containing all of this code as Character search, then close it down.
9. Create a query to list out all the films which were made by directors who are
older than any of the actors in the database:
Create a query to list out any films starring any of the 3 youngest actors in the database.
You can use SELECT TOP 3 in the subquery to list the 3 youngest actors, and DISTINCT to avoid
duplicates in the main query
10. Use a cursor to print out a list of the top 10 websites in terms of incoming links:
Declare an integer variable to hold the position number, initialise it to 0 and increase it by 1 each
time you fetch another row from the cursor.
Optionally, save your work as Websites with links.sql, then close it down.
When you run this query, it should produce the following message:
Add the following line of code (as always, no need to bother with the comment) to your
stored procedure:
Now amend your stored procedure so that it summarises the rows affected as follows
(using the hint below):
The new version looks similar, but it has a heading, and the wording is subtly different
To do this, you'll need to declare a variable and set it equal to @@rowcount - otherwise
the PRINT statements will reset the row count, and you'll get the wrong answer!
When your stored procedure is working, optionally save the query which generates it
as RowCountWebsites.sql, then close it down.
Variable detail level for website query - Ifs
12. 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 database contains a
table of websites called tblWebsite with (among others) the following columns:
Feel free to omit the comments and use different parameter names
Now write the rest of the stored procedure so that it does what the comments suggest it
should. For example, if you run the command EXEC spGooglySites 3 you should see:
The order of your rows may be different, but there should be 17 in all
Optionally, save the query to generate the stored procedure as Variable detail
level.sql, then close it down.
Websites going online between 2 variable dates
13. 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 database contains a
table of websites called tblWebsite, which has (among others) two columns
called:
From date for searching (if not passed in, this should take the default value 1st
January 1990)
To date for searching (if not passed in, this should take the default value 31st
December 1999)
The stored procedure should then show all websites registered between the two dates
passed.
Try testing your query out - here are some things to try:
A couple of examples to try - but feel free to use your own instead
Use your stored procedure to show that only one website went online in 1991. The
name might look vaguely familiar ...
Optionally, save the query to generate this stored procedure as Websites reg between
2 dates.sql, then close it down.
By linking the tblPerson table to this tblPersonStatus table, create a simple query to
show the number of people who still work at their companies:
Optionally, save this query as Count persons by status.sql, then close it down.
Import the query called Extra text function. If you try running this query, it falls over -
that's because the fnExtraText function doesn't yet exist!
This query should consider the extent to which the description of any event is longer than its short
name
Create the function (and test that it works), then save the script to create it
as CreatExtra text function.
Creating a view
USE Historical Events database.
16. Create a view using the view designer which will be a repeat of one we did in an
earlier exercise. The view should show for each country the number of events
occurring since 1990, but exclude countries with less than 5 events:
The first few rows of the 24 actors we should see at the end
Yes, you could do this all with a single SELECT statement, but using the CTE - arguably - makes the
whole thing easier to understand.
To accomplish this:
create a CTE which shows all of the films directed by Spielberg; then
link the results of this CTE to the tblCast and tblActor tables to show the results
above
When you've finished, save this query as Spielberg, then close it down.
We're going to remove all BETTING and ADULT sites, such as the one shown selected
Begins a transaction
Within this transaction, deletes all rows from the above table where
the Category column is Betting or Adult
Stores how many rows are left in the table in an integer variable
Rolls back the transaction
Stores how many rows are in the table after this roll back (in a second integer
variable)
Displays the final figures
The transaction (if not rolled back) would have deleted 9 sites
Optionally, save this code as Delete sites then roll back.sql, then close it down.
Fn to count trainer ids
19. If you haven't already done so, run the stored procedure in the above folder to
generate a database of training courses and delegates.
Each row in the tblSchedule table contains a column called TrainerIds, giving details
of who is assigned to train on the course:
We will create a function called fnCountIds to count how many trainers there are
assigned to each course (note that exactly the same function could also count how
many resources are assigned). The following Hint to help you do this:
Count the number of trainers by seeing how long the string of ids is with and without its commas,
and then taking the difference between these two numbers
The first few rows of the final query, sorted by course start date
Optionally, save the code to generate your function as Count trainers.sql, then close it
down.
Amend your query so that it shows how many events there were for each era, with the
busiest era first:
Nothing much has happened since the Sex Pistols broke up ...