The Table Tennis Olympics Database: Ttms Games Color Who Country
The Table Tennis Olympics Database: Ttms Games Color Who Country
The table ttms shows the Olympic medal winners for Table Tennis (Men's Singles). The country of
given by a three letter code. To get the actual country name you must JOIN this table to the countr
The two tables country and ttms are
ONE TO MANY. One country has
many winners, each winner has only
one country.
ttms
games color who country
1988 gold Yoo Nam-Kyu KOR
1988 silver Kim Ki Taik KOR
.. .. .. ..
How to do joins.
The phrase FROM ttms JOIN country ON ttms.country=country.id represents the join of the tables
one row for every medal winner. In addition to the ttms fields (games, color, who and country) it inclu
corresponding country (id, name ).
1a. Show the athelete (who) and the country name for medal winners in 2000. Results
ttms(games, color, who, country)
country(id, name)
SELECT w ho, country.name
FROM ttms JOIN country
ON (ttms.country=country.id)
WHERE games = 2000
1b. Show the who and the color of the medal for the medal winners from Results
'Sweden'.
ttms(games, color, who, country)
country(id, name)
SELECT w ho, color
FROM ttms JOIN country
ON (ttms.country=country
WHERE country.name = 'Sw ed
1c. Show the years in which 'China' won a 'gold' medal. Results
ttms(games, color, who, country)
country(id, name)
SELECT games
FROM ttms JOIN country
ON (ttms.country=country
WHERE name='China' AND color
SELECT w ho
FROM ttw s JOIN games
ON (ttw s.games=games
WHERE city = 'Barcelona'
2b. Show which city 'Jing Chen' won medals. Show the city and the medal Results
color.
ttws(games, color, who, country)
games(yr, city, country)
2c. Show who won the gold medal and the city. Results
ttws(games, color, who, country)
games(yr, city, country)
3a. Show the games and color of the medal won by the team that includes Results
'Yan Sen'.
ttmd(games, color, team, country)
team(id, name)
SELECT name
FROM ttmd JOIN team ON team
WHERE color='gold' AND games
3c. Show the name of each medal winner country 'FRA'. Results
ttmd(games, color, team, country)
team(id, name)
SELECT name
FROM ttmd JOIN team ON team
WHERE country = 'FRA'
The next tutorial about the Movie database involves some slightly more complicated joins.
Movie Database
This tutorial introduces the notion of a join. The database con
movie , actor and casting .
1c. List all of the Star Trek movies, include the id title and yr. (All of these Results
movies include the words Star Trek in the title.)
movie(id, title, yr, score, votes, director)
2b. What id number does the actor 'Glenn Close' have? Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
3b. Obtain the cast list for the film 'Alien' Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
SELECT name
FROM movie, casting, actor
WHERE title='Alien'
AND movieid=movie.id
3c. List the films in which 'Harrison Ford' has appeared Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
SELECT title
FROM movie, casting, actor
WHERE name='Harrison Ford'
AND movieid=movie.id
3d. List the films where 'Harrison Ford' has appeared - but not in the star role. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
SELECT title
FROM movie, casting, actor
WHERE name='Harrison Ford'
AND movieid=movie.id
3e. List the films together with the leading star for all 1962 films. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
4b. List the film title and the leading actor for all of 'Julie Andrews' films. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
4c. Obtain a list of actors in who have had at least 10 starring roles. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
SELECT name
FROM casting JOIN actor
ON actorid = actor.id
WHERE ord=1
4d. List the 1978 films by order of cast list size. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
4e. List all the people who have worked with 'Art Garfunkel'. Results
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
teacher dept
id dept name phone mobile id name
101 1 Shrivell 2753 07986 555 1234 1 Computing
102 1 Throd 2754 07122 555 1920 2 Design
103 1 Splint 2293 3 Engineering
104 Spiregrain 3287
105 2 Cutflower 3212 07996 555 6574
106 Deadyawn 3345
1b. Note the INNER JOIN misses the teacher with no department and the Results
department with no teacher.
SELECT teacher.name, dept.name
FROM teacher INNER JOIN dept
ON (teacher.dept=dept.id)
1d. Use a different JOIN so that all departments are listed. Results
2a. Use COALESCE to print the mobile number. Use the number '0131 444 Results
2266' there is no number given.
2b. Use the COALESCE function and a LEFT JOIN to print the name and Results
department name. Use the string 'None' where there is no department.
SELECT teacher.name, COALES
FROM teacher LEFT JOIN dept
ON teacher.dept=dept.
2c. Use COUNT to show the number of teachers and the number of mobile Results
phones.
SELECT COUNT(teacher.name),
FROM teacher
2d. Use COUNT and GROUP BY dept.name to show each department and Results
the number of staff. Use a RIGHT JOIN to ensure that the Engineering
department is listed..
Using CASE
3a. Use CASE to show the name of each teacher followed by 'Sci' if the the Results
teacher is in dept 1 or 2 and 'Art' otherwise.
SELECT name, CASE WHEN dep
ELSE 'Art' END
FROM teacher
3b. Use CASE to show the name of each teacher followed by 'Sci' if the the Results
teacher is in dept 1 or 2 show 'Art' if the dept is 3 and 'None' otherwise.
Edinburgh Buses
Details of the database. Looking at the data.
stops(id, name)
route(num,company,pos, stop)
1c. Give the id and the name for the stops on the '4' 'LRT' service. Results
SELECT id, name FROM stops, r
WHERE id=stop
AND company='LRT'
AND num='4'
2a. The query shown gives the number of routes that visit either London Results
Road (149) or Craiglockhart (53). Run the query and notice the two services
that link these stops have a count of 2.
Add a HAVING clause to restrict the output to these two routes.
SELECT company, num, COUNT(*)
FROM route WHERE stop=149 OR stop=53
GROUP BY company, num
2b. Execute the self join shown and observe that b.stop gives all the places Results
you can get to from Craiglockhart.
Change the query so that it shows the services from Craiglockhart to London
Road.
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop=53
2c. The query shown is similar to the previous one, however by joining two Results
copies of the stops table we can refer to stops by name rather than by
number.
Change the query so that the services between 'Craiglockhart' and 'London
Road' are shown. If you are tired of these places try 'Fairmilehead' against
'Tollcross'
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart'
3a. Give a list of all the services which connect stops 115 and 137 Results
('Haymarket' and 'Leith')
3b. Give a list of the services which connect the stops 'Craiglockhart' and Results
'Tollcross'
SELECT R1.company, R1.num
FROM route R1, route R2, stop
WHERE R1.num=R2.num AND
AND R1.stop=S1.id AND R2.s
3c. Give a list of the stops which may be reached from 'Craiglockhart' by taking one Results
bus. Include the details of the appropriate service.
Edinburgh Buses
Details of the database. Looking at the data.
stops(id, name)
route(num,company,pos, stop)
1c. Give the id and the name for the stops on the '4' 'LRT' service. Results
2a. The query shown gives the number of routes that visit either London Results
Road (149) or Craiglockhart (53). Run the query and notice the two services
that link these stops have a count of 2.
Add a HAVING clause to restrict the output to these two routes.
SELECT company, num, COUNT(*)
FROM route WHERE stop=149 OR stop=53
GROUP BY company, num
2b. Execute the self join shown and observe that b.stop gives all the places Results
you can get to from Craiglockhart.
Change the query so that it shows the services from Craiglockhart to London
Road.
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop=53
2c. The query shown is similar to the previous one, however by joining two Results
copies of the stops table we can refer to stops by name rather than by
number.
Change the query so that the services between 'Craiglockhart' and 'London
Road' are shown. If you are tired of these places try 'Fairmilehead' against
'Tollcross'
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart'
3a. Give a list of all the services which connect stops 115 and 137 Results
('Haymarket' and 'Leith')
3b. Give a list of the services which connect the stops 'Craiglockhart' and Results
'Tollcross'
SELECT R1.company, R1.num
FROM route R1, route R2, stop
WHERE R1.num=R2.num AND
AND R1.stop=S1.id AND R2.s
3c. Give a list of the stops which may be reached from 'Craiglockhart' by Results
taking one bus. Include the details of the appropriate service.
FUNCTIONS
How to use string functions, logical functions and mathematical functions.
• Concatenate strings.
Specific to Oracle
none
Related links:
• SELECT
o Concatenate two or more fields.
o Make union between different tables to build one single view or
request?
o Use 'like' in a sql select statement
o How to build a statement on a word with an Apostrophe such as
WHERE name='Tom's Book'
o Full text search
o Display a column name for an aggregate function.
o How do you use Equi Join to join two tables with the same name.
o Use SELECT for a column whose name contains spaces.
o use NULL
USERS
How to create users, give them access, get at other peoples tables. How to find
processes and kill them.
META DATA
Information about the database: what tables exist, what are the column headings,
what database is in use.
Error Messages
Error messages, how they are caused and what can be done about them.
MySQL
• Error 1052 Column 'name' in field list is ambiguous
SQL Server
• Msg 1013, Ambiguous column name 'name'.
• Msg 147 An aggregate may not appear in the WHERE clause unless it is
in a subquery contained in a HAVING clause or a select list, and the
column being aggregated is an outer reference.
• Msg 8120 Column 'xxx' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause. (SQL-
42000)
Oracle
• ORA-00918: column ambiguously defined
DB2
• SQL0203N A reference to column "NAME" is ambiguous.
SQLSTATE=42702
Postgres
• Error 7 ERROR: Column reference "name" is ambiguous