SQL練習(二)

SQL練習(二)

MORE Join

1.List the films where the yr is 1962 [Show id, title]

SELECT id, title FROM movie WHERE yr=1962

2.Give year of Citizen Kane.

SELECT yr FROM movie WHERE title=Citizen Kane

3.List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

SELECT id,title,yr FROM movie WHERE title LIKE %Star Trek% ORDER BY yr

4.What id number does the actor Glenn Close have?

SELECT id FROM actor WHERE name=Glenn Close

5.What is the id of the film Casablanca

SELECT id FROM movie WHERE title=Casablanca

6.Obtain the cast list for Casablanca.

what is a cast list?

The cast list is the names of the actors who were in the movie.

Use movieid=11768, (or whatever value you got from the previous question)

SELECT actor.name FROM actor INNER JOIN casting ON actor.id=casting.actorid WHERE casting.movieid=11768

7.Obtain the cast list for the film Alien

SELECT actor.name FROM actor INNER JOIN casting ON casting.actorid=actor.id INNER JOIN movie ON movie.id=casting.movieidWHERE title=Alien

8.List the films in which Harrison Ford has appeared

SELECT title FROM movie INNER JOIN casting ON movie.id =casting.movieid INNER JOIN actor ON casting.actorid=actor.id WHERE name LIKE %Harrison Ford%

9.List the films where Harrison Ford has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

SELECT title FROM movie INNER JOIN casting ON movie.id =casting.movieid INNER JOIN actor ON casting.actorid=actor.id WHERE name LIKE %Harrison Ford% AND casting.ord<>1

10.List the films together with the leading star for all 1962 films.

SELECT title,actor.name FROM movie INNER JOIN casting ON movie.id=casting.movieidINNER JOIN actor ON actor.id=casting.actoridWHERE yr=1962 AND ord=1

11.Which were the busiest years for John Travolta, show the year and the number of movies he made each year for any year in which he made more than 2 movies.

SELECT yr,COUNT(title) FROM movie JOIN casting ON movie.id=movieid JOIN actor ON actorid=actor.idwhere name=John TravoltaGROUP BY yrHAVING COUNT(title)=(SELECT MAX(c) FROM(SELECT yr,COUNT(title) AS c FROM movie JOIN casting ON movie.id=movieid JOIN actor ON actorid=actor.id where name=John Travolta GROUP BY yr) AS t)

12.List the film title and the leading actor for all of the films Julie Andrews played in.

SELECT movie.title,actor.name FROM movie INNER JOIN casting ON movie.id=casting.movieidINNER JOIN actor ON casting.actorid=actor.idWHERE movie.id IN (SELECT movie.id FROM movie INNER JOIN casting ON movie.id=casting.movieidINNER JOIN actor ON actor.id=casting.actoridWHERE actor.id IN (SELECT id FROM actor WHERE name=Julie Andrews ))AND ord=1

13.Obtain a list, in alphabetical order, of actors whove had at least 30 starring roles.

SELECT name FROM actorJOIN casting ON actor.id=casting.actorid WHERE ord=1 GROUP BY nameHAVING COUNT(movieid)>=30

14.List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

SELECT movie.title,COUNT(casting.actorid) FROM movie INNER JOIN casting ON movie.id=casting.movieid INNER JOIN actor ON casting.actorid=actor.idWHERE yr=1978group by titleORDER BY COUNT(actorid) DESC, title

15.List all the people who have worked with Art Garfunkel.

SELECT actor.name FROM actor INNER JOIN casting ON actor.id=casting.actoridINNER JOIN movie ON casting.movieid=movie.idWHERE casting.movieid IN (SELECT movie.id FROM movieINNER JOIN casting ON casting.movieid=movie.idINNER JOIN actor ON actor.id=casting.actoridWHERE actor.name=Art Garfunkel)AND actor.name<>Art Garfunkel

Using Null

1.List the teachers who have NULL for their department.

SELECT name FROM teacher WHERE dept IS NULL

2.Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

SELECT teacher.name, dept.nameFROM teacher INNER JOIN deptON (teacher.dept=dept.id)

3.Use a different JOIN so that all teachers are listed.

SELECT teacher.name,dept.name FROM teacher LEFT JOIN dept ON teacher.dept=dept.id

4.Use a different JOIN so that all departments are listed.

SELECT teacher.name,dept.name FROM teacher RIGHT JOIN dept ON teacher.dept=dept.id

5.Use COALESCE to print the mobile number. Use the number 07986 444 2266 if there is no number given. Show teacher name and mobile number or 07986 444 2266

SELECT name,COALESCE(mobile,07986 444 2266) AS mobile FROM teacher

6.Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string None where there is no department.

SELECT teacher.name,COALESCE(dept.name,None) FROM teacher LEFT JOIN dept ON dept.id=teacher.dept

7.Use COUNT to show the number of teachers and the number of mobile phones.

SELECT COUNT(name),COUNT(mobile) FROM teacher

8.Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed.

SELECT dept.name,COUNT(teacher.name) FROM teacher RIGHT JOIN dept ON teacher.dept=dept.idGROUP BY dept.name

9.Use CASE to show the name of each teacher followed by Sci if the teacher is in dept 1 or 2 and Art otherwise.

SELECT name, CASE when dept in(1, 2) then Sci else Art end FROM teacher

10.Use CASE to show the name of each teacher followed by Sci if the teacher is in dept 1 or 2, show Art if the teachers dept is 3 and None otherwise.

SELECT name, CASE WHEN dept in (1,2) then Sci WHEN dept=3 THEN Art else None ENDFROM teacher;

Self join

1.How many stops are in the database.

SELECT COUNT(stops.name) FROM stops

2.Find the id value for the stop Craiglockhart

SELECT id FROM stops WHERE name=Craiglockhart

3.Give the id and the name for the stops on the 4 LRT service

SELECT stops.id,stops.name FROM stops JOIN route ON stops.id=route.stopWHERE route.num=4 AND company=LRT

4.The query shown gives the number of routes that visit either London 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=53GROUP BY company, num HAVING count(*)=2

5.Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.

SELECT a.company, a.num, a.stop, b.stopFROM route a JOIN route b ON(a.company=b.company AND a.num=b.num)WHERE a.stop=53 AND b.stop=149

6.The query shown is similar to the previous one, however by joining two 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.

SELECT a.company, a.num, stopa.name, stopb.nameFROM 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

7.Give a list of all the services which connect stops 115 and 137 (Haymarket and Leith)

SELECT DISTINCT a.company,a.num FROM route a JOIN route b ON a.num=b.num AND a.company=b.company AND a.stop=115 and b.stop=137

8.Give a list of the services which connect the stops Craiglockhart and Tollcross

SELECT a.company, a.numFROM route aJOIN route b ON (a.num=b.num AND a.company=b.company)JOIN stops stopa ON (a.stop=stopa.id)JOIN stops stopb ON (b.stop=stopb.id)WHERE stopa.name=CraiglockhartAND stopb.name=Tollcross

9.Give a distinct list of the stops which may be reached from Craiglockhart by taking one bus, including Craiglockhart itself, offered by the LRT company. Include the company and bus no. of the relevant services.

SELECT DISTINCT b1.name, a1.company, a1.num FROM route a1 , route a2, stops b1, stops b2WHERE b2.name=CraiglockhartAND b2.id=a2.stop AND a1.company=a2.company AND a1.num=a2.num AND a1.stop=b1.id

推薦閱讀:

SQL練習及解答
關於mysql安裝的坑
SQL(七、集合運算)
配置MySQL開啟遠程連接的方法

TAG:SQL | 計算機視覺 | SQL語句 |