当前位置:网站首页>Sqlzoo question brushing record-3
Sqlzoo question brushing record-3
2022-06-11 07:24:00 【Amelia who loves learning】
- List 1962 The first film in , [ Display id, title].
SELECT id,title
FROM movie
WHERE yr=1962;
- The movie nation ‘Citizen Kane’ The year of its debut .
select yr from movie
where title='Citizen Kane';
- List all Star Trek The Star Trek series , Include id, title and yr( This system is based on Star Trek For the beginning of the movie title ). Sort by year .
select id,title,yr from movie
where title like 'Star Trek%';
- id yes 11768, 11955, 21191 What is the name of the movie ?
select title from movie
where id in ('11768','11955','21191');
- actress ’Glenn Close’ The number of id What is it ?
select id from actor
where name = 'Glenn Close';
- North African spy film ’Casablanca’ The number of id What is it ?
select id from movie
where title='Casablanca';
- List the movie North Africa spy movie 'Casablanca’ The cast of . Use movieid=11768, This is the result of your last question .
select name from movie,actor,casting
where actor.id=casting.actorid and movie.id=casting.movieid and title='Casablanca';
- Show movie aliens ’Alien’ List of actors .
select name from actor,casting
where actor.id=casting.actorid and movieid = (select id from movie where title = 'Alien');
- List actor xialisunfu ‘Harrison Ford’ Movies that have been performed .
select title from movie,actor,casting
where movie.id=casting.movieid and actor.id=casting.actorid and actor.name='Harrison Ford';
- List actor xialisunfu ‘Harrison Ford’ Movies that have been performed , But he is not the first 1 Lead .
select movie.title from casting,actor,movie
where casting.actorid=actor.id and movie.id=casting.movieid and actor.name='Harrison Ford' and ord!=1;
- List 1962 The first film made in and its first film 1 Lead .
select movie.title,actor.name from movie,actor,casting
where movie.yr=1962 and movie.id=casting.movieid and casting.ord=1 and casting.actorid=actor.id;
- List the actors Julie · Anders ’Julie Andrews’ The names of the films in which they have participated and their titles 1 Lead . She was 1980 Participate in this movie again Little Miss Marker. Originally written in 1934 year , She was also involved . The movie title is not unique . Use movie number in subquery .
select title,name from (casting join actor on casting.actorid=actor.id)
join movie on movie.id=casting.movieid
where movieid in(select movieid from casting join actor on casting.actorid=actor.id
where name='Julie Andrews') and ord=1;
- List in alphabetical order , List which actors have done 30 Second 1 Lead .
select name,count(ord) from actor join casting on actor.id = casting.actorid
where ord=1
group by name
having count(ord)=30;
- List 1978 The number of movie titles and roles in the first film of the year , According to this number, arrange at least from the most .
select title,count(ord) from movie join casting on casting.movieid=movie.id
where yr=1978
group by title
order by count(ord) desc;
- List those who have worked with actor art · Gefenko ’Art Garfunkel’ The names of the actors who have worked together .
select distinct name from (casting join actor on actor.id=casting.actorid)
join movie on movie.id=casting.movieid
where movieid in (select movieid from casting join actor on actor.id=casting.actorid
where name='Art Garfunkel') and name!='Art Garfunkel';
- List the departments department yes NULL A worthy teacher .
select name from teacher
where dept is NULL;
- Use different JOIN( External connection ), To list all the teachers .
select teacher.name,dept.name from teacher left join dept on teacher.dept=dept.id;
- Use different JOIN( External connection ), To list all the teachers .
select teacher.name,dept.name from teacher left join dept on teacher.dept=dept.id;
- Use different JOIN( External connection ), To list all the departments .
select teacher.name,dept.name from teacher right join dept on teacher.dept=dept.id;
- 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') from teacher;
- 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;
- Use COUNT Count the number of teachers and mobile phones .
select count(name),count(mobile) from teacher;
- Use COUNT and GROUP BY dept.name To show the number of teachers in each department . Use RIGHT JOIN To ensure that the Engineering Department Engineering Is in the middle .
select dept.name,count(teacher.name) from teacher right join dept on dept.id=teacher.dept
group by dept.name;
- 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;
- 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 teacher’s dept is 3 and ‘None’ otherwise.
select name,case when dept in (1,2) then 'Sci'
else 'None' end
from teacher;
- How many stops are in the database.
select count(id) from stops;
- Find the id value for the stop ‘Craiglockhart’.
SELECT id FROM stops
WHERE name = 'Craiglockhart';
- 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';
- 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;
- 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
- 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';
- 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';
- 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';
- 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 name,company,num from stops join route on id = stop
where company='LRT' and (name='Craiglockhart' or num in (select num from stops join route on id = stop
where company='LRT' and name='Craiglockhart'));
- 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.
SELECT DISTINCT x.num,x.company,name,y.num,y.company
FROM(SELECT a.num,a.company,b.stop
FROM route a JOIN route b
ON (a.num = b.num AND a.company = b.company)
AND a.stop != b.stop
WHERE a.stop = (SELECT id FROM stops WHERE name ='Craiglockhart')) AS x
JOIN(SELECT c.num,c.company,c.stop
FROM route c JOIN route d
ON (c.num = d.num and c.company = d.company)
AND c.stop != d.stop
WHERE d.stop =(SELECT id FROM stops WHERE name = 'Lochend'))AS y
ON x.stop = y.stop
JOIN stops ON x.stop = stops.id
ORDER BY x.num,stops.name,y.num
边栏推荐
- [STL source code analysis] summary notes (12): functors and adapters
- [deploy private warehouse based on harbor] 4 push image to harbor
- 【CF#223 (Div. 2)】A. Sereja and Dima
- Notes on learning Es5 and ES6
- @Jsonproperty annotation
- Decimal to binary
- 多线程复习总结之解析Volatile关键字
- P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
- P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
- 教育专家王中泽老师一招解决学生问题
猜你喜欢

Nodejs database (Part 2)

Biological sequence intelligent analysis platform blog (1)

【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算

Leetcode-141. Linked List Cycle

JVM learning record (VII) -- class loading process and parental delegation model
![P5431 [template] multiplicative inverse 2](/img/63/1cb95a55c9ce9b92d6d55381d0215b.jpg)
P5431 [template] multiplicative inverse 2

学 SQL 必须了解的10个高级概念

Detailed explanation of mutationobserver

Records how cookies are carried in cross domain requests

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
随机推荐
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Decimal to binary
[deploy private warehouse based on harbor] 3 deploy harbor
Library management system 1- project approval
Set center alignment
2022年熔化焊接与热切割考试练习题及答案
QT 基于QScrollArea的界面嵌套移动
[STL source code analysis] summary note (2): overview of containers
First day of database
Detailed explanation of mutationobserver
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
[deploy private warehouse based on harbor] 2 machine preparation
【CF#693 (Div. 3)】B. Fair Division
AtomicInteger原子操作类
Leetcode-141. Linked List Cycle
【CF#223 (Div. 2)】A. Sereja and Dima
P5431 [template] multiplicative inverse 2
商汤科技积极复工,将大力投入数字哨兵的产能和部署