SQLZOO习题详解(10)——Self join

Posted 谷哥的小弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLZOO习题详解(10)——Self join相关的知识,希望对你有一定的参考价值。


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

数据表

stops表如下:
在这里插入图片描述
在这里插入图片描述

route表如下:
在这里插入图片描述
在这里插入图片描述

链接地址

https://napier.sqlzoo.net/wiki/Self_join

SQL练习

01、请查询站点stop的总数量

SELECT COUNT(*) FROM stops

02、查询站点Craiglockhart对应的id编号

SELECT id FROM stops WHERE name='Craiglockhart'

03、查询LRT公司运营的4号线路的所有站点的id和name

SELECT id, name
FROM stops INNER JOIN route ON stops.id = route.stop
WHERE company = 'LRT' AND num = '4'

04、请查询两次经过149号站点或者53号站点的巴士相关信息

SELECT company, num, COUNT(*)
FROM route WHERE stop=149 OR stop=53
GROUP BY company,num
HAVING count(*) = 2

在这里插入图片描述

05、请查询可从53号站点到149号站点的路线及其所属公司

SELECT a.company, a.num, a.stop, b.stop
FROM route AS a INNER JOIN route AS b ON a.company = b.company 
WHERE a.stop = 53 AND b.stop = 149 AND a.num = b.num

在这里插入图片描述
06、请查询可从Craiglockhart到London Road的路线及其所属公司

SELECT a.company, a.num, stopa.name, stopb.name
FROM route AS a JOIN route AS b ON a.company = b.company
INNER JOIN stops stopa ON a.stop = stopa.id INNER JOIN stops stopb ON b.stop = stopb.id
WHERE stopa.name = 'Craiglockhart' AND stopb.name = 'London Road' AND a.num = b.num

在这里插入图片描述
07、请查询可从115号站点Haymarket到137号站点Leith的路线及其所属公司

SELECT DISTINCT a.company, a.num
FROM route AS a INNER JOIN route AS b ON a.num = b.num
AND a.company = b.company AND a.stop = 115 AND b.stop = 137
SELECT DISTINCT a.company, a.num
FROM route AS a INNER JOIN route AS b ON a.company = b.company AND a.num = b.num
INNER JOIN stops stopa ON a.stop = stopa.id
INNER JOIN stops stopb ON b.stop = stopb.id
WHERE stopa.name = 'Haymarket' AND stopb.name = 'Leith'

在这里插入图片描述
08、请查询可从Craiglockhart到Tollcross的路线及其所属公司

SELECT DISTINCT a.company, a.num
FROM route AS a INNER JOIN route AS b ON (a.company=b.company AND a.num=b.num)
INNER JOIN stops stopa ON (a.stop=stopa.id)
INNER JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart' AND stopb.name = 'Tollcross'

在这里插入图片描述
09、请查询在Craiglockhart站点乘坐LRT公司的公交车能到达的其他站点

SELECT DISTINCT stopb.name, a.company, a.num
FROM route AS a INNER JOIN route AS b ON (a.company=b.company AND a.num=b.num)
INNER JOIN stops stopa ON (a.stop=stopa.id)
INNER JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart' AND a.company = 'LRT'

在这里插入图片描述
10、查询从Craiglockhart 到Lochend,并且包含2路公交的线路(即只中转一次)

SELECT m.num, m.company, name, n.num, n.company
FROM (
	SELECT a.num, a.company, b.stop
	FROM route a
		JOIN route b
		ON a.company = b.company
			AND a.num = b.num
			AND a.stop != b.stop
	WHERE a.stop = (
		SELECT id
		FROM stops
		WHERE name = 'Craiglockhart'
	)
) m
	JOIN (
		SELECT d.num, d.company, c.stop
		FROM route c
			JOIN route d
			ON c.company = d.company
				AND c.num = d.num
				AND c.stop != d.stop
		WHERE d.stop = (
			SELECT id
			FROM stops
			WHERE name = 'Lochend'
		)
	) n
	ON m.stop = n.stop
	JOIN stops ON m.stop = stops.id
WHERE m.num != n.num
ORDER BY m.num, stops.name, n.num;

在这里插入图片描述

以上是关于SQLZOO习题详解(10)——Self join的主要内容,如果未能解决你的问题,请参考以下文章

SQLZOO习题详解(09)——The JOIN operation

SQLZOO习题详解(07)——The JOIN operation

SQLZOO习题详解(05)——SELECT within SELECT

SQLZOO习题详解(06)——SUM and COUNT

SQL练习-SQLZOO SQLZOO:The JOIN operation

SQL练习-SQLZOO SQLZOO:The JOIN operation