見出し画像

【SQLZOO答え】9.Self join

SQL入門を勉強するため、友人にSQLの練習問題ないかと聞いたら、
SQLZOO】というサイトを教えてもらいました。
ただし、問題を解いてるうちに、わからない問題に関して、クエリの答えがおらず、結果しか教えてくれないので、答えをアウトプットしようと思い、noteを始めました。 

0.SQLZOO練習問題

Self join

1.Summary

How many stops are in the database.

select
 count(id)
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
 id, name
from
 stops
join
 route 
 on id = stop
where
 num = '4' and company = 'LRT';

4.Routes and stops

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=53
group 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.stop
from
 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. 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' 
 and stopb.name = 'London Road';

7.Using a self join

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.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='Haymarket' 
 and stopb.name = 'Leith';

8.

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

select distinct
 a.company, a.num
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' 
 and 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
 stopb.name, a.company, a.num
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' 
 and a.company = 'LRT';

10.

Find the routes involving two buses that can go from Craiglockhart to Lochend.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.
Hint
Self-join twice to find buses that visit Craiglockhart and Lochend, then join those on matching stops.

select distinct
 a.num,a.company,stopb.name,c.num,c.company
from
 route a 
join
 route b
 on (a.company=b.company AND a.num=b.num)
join (route c join route d on (c.company=d.company and c.num=d.num))
join stops stopa on (a.stop=stopa.id)
join stops stopb on (b.stop=stopb.id)
join stops stopc on (c.stop=stopc.id)
join stops stopd on (d.stop=stopd.id)
where
 stopa.name='Craiglockhart' 
 and stopd.name='Lochend' 
 and stopb.name = stopc.name

その他の答へ        

0.SELECT basics
1.SELECT name
2.SELECT from World
3.SELECT from Nobel
4.SELECT within SELECT
5.SUM and COUNT
6.JOIN
7.More JOIN operations
8.Using Null
8+ Numeric Examples
9.Self join
10.Tutorial Quizzes
11.Tutorial Student Records
12.Tutorial DDL

※問題を攻略でき次第、随時更新いたします。

この記事が気に入ったらサポートをしてみませんか?