Relatively Easy Questions
Relatively Easy Questions
1. Create a query to show all the people with really long names - similar to the
following - and run it twice:
When you run your query a second time, it should crash because the temporary
table #BigPeople already exists. Apply error trapping at the top of the query such that:
You can test out that this final possibility works by adding the following command at the
end of your query:
This command would ensure that the table didn't exist the next time that you run the query
Optionally, save this query as Check deleting temp table.sql, then close it down.
2. Create a function (called fnWeekDay?) to show the day of the week for any
given date.
Use your function to show the number of events for each day of the week:
Optionally, save this query as Courses between dates.sql, then close it down.
4. Create a query to list out the following columns from the tblEvent table:
EventName
EventDate
Your rows should appear in date order, with the most recent event coming first.
The last event in our database was on 14th November 2007
Make sure you've included comments in your query, and indented it sensibly
Save this query as Listing events in reverse date order, then close it down.
5. Create a query using an inner join and table aliases to list out the non-European
countries.
6. Create a query to show the number of events for each country (with the most
"eventful" country coming at the top of the list):
Optionally, save the query to generate your stored procedure as Websites for chosen
category.sql, then close it down.
8. We want to create a query which shows all companies which either are in the
retail sector or which have more than 12 people working there. To do this, first
select all of the companies in the retail sector (there are only 2) into a new
temporary table called #Org:
Now add code to insert all companies employing more than 12 people into the same
table (you could use a subquery to do this):
Select all of the records from the temporary table that you've compiled. The final
answer should look like this:
Primark and Kingfisher PLC qualify as being part of the retail sector
Optionally, save this query as Retail or big orgs.sql, then close it down.
Execute this query, then right-click on your Views to refresh them. You should now see
your new view!
Right-click on the view to modify its design, and use the View Design window to change
the criteria so that you show the events in 2001 instead (there should be 7 of them).
Each course product can have lots of instances of the course scheduled
Your function should then return the end date of the course.
Use the DateAdd function to work out the end date: the syntax is DateAdd ( day, Number of days to
add, Start date) .
Incorporate your function within a query to show the start and end dates of all courses
which start and end within January 2010:
Optionally, save this query as Course end date function.sql, then close it down.
Show length of event descriptions using LEN
USE Historical Events database.
11. Create a query to show the name of each event, together with the length
of its description, sorted so that the longest description appears first:
Why there was most to say about the founding of the UN, owl only knows
CONVERT to convert the date of each event to a more readable format; and
DATEDIFF to show the difference between each event's date and today in years
Your final output should look like this (sorted in reverse date order):
13. Use a left outer join to show all of the continents with no matching
countries:
If you have time (and energy), repeat this trick to show that Latvia and Uruguay are the
only countries with no corresponding events.
Now amend your WHERE clause so that you only see events which took place in
Germany in the 1940s (ie where the EventDate column lies between 1st January 1940
and 31st December 1949):
Write a query which uses a cursor to step through the trainers one by one
in alphabetical order, printing out the details for each:
Design a view to show all of the events occurring in Africa, in date order. As a clue,
here is what the top half of the view designer should look like:
Not much happens in Africa? Or more likely, the database is hopelessly biased
Firstly, so that it creates a new view called vwAfricaAsia, rather than altering the
existing one; and
Secondly, so that it shows the events in Africa or Asia
Rankings are from Alexa, a company which analyses the popularity of websites
Create a query which lists out the top 10 websites in the UK (ie ordered
by AlexaRankUk, where the AlexaRankUk column is not null):
Now create a stored procedure from this query called spListTopWebsites. Execute
your stored procedure to check that it gives the same answer.
Alter your stored procedure (change the word CREATE to ALTER) so that it lists out the
top 5 websites, not the top 10.
Create a query to run this stored procedure, using the example of 1970:
19. Create a query/subquery showing all of the events which happened after
the last European Unionevent (or if you prefer, which happened after any
of the European Union events). Here's what your results should look like:
Only 7 events have happened since the last EU event
20. The aim of this exercise is to create a table variable holding all of those
course ids where the course is either:
About C#; or
Given by Gabriella Montez (the techiest trainer).
To do this, first declare a table variable called @TechieCourses to hold one column
only - each course's ScheduleId.
Insert into this table all of those courses which have C# in the course name:
Now insert into the same table all those courses where the TrainerIds column contains
number 2936(that's Gabriella). To do this, add a comma to the start and end of each of
the things you're comparing:
What your WHERE condition might look like
Create a SELECT statement joining your temporary table to the table of courses to
show your results in course date order:
21. Create a query to show a list of the countries in the tblCountry table in
reverse alphabetical order. Where a country has no continent id, replace
this with 0:
Here are the tables that you will need to do the first part of this exercise:
Initially we only need to show the people having a delegate count of 6 or more
Create a query to show the ids of the people who have attended 6 or more courses:
Now turn this into a longer query to show the course names that these people have
attended, using a CTE (common table expression) (you'll need to join your CTE to
the tblDelegate, tblSchedule and tblCourse tables to accomplish this):
Optionally, save this query as Important people courses, then close it down.
23. Write out a SELECT statement to list out the names from tblDoctor in
date of birth order.
Use the ORDER BY clause to set the sort order of your results.
Your results should look something like this: