当前位置:网站首页>Sqlzoo question brushing record-3

Sqlzoo question brushing record-3

2022-06-11 07:24:00 Amelia who loves learning

  1. List 1962 The first film in , [ Display id, title].
SELECT  id,title
 FROM movie
 WHERE yr=1962;
  1. The movie nation ‘Citizen Kane’ The year of its debut .
select yr from movie
  where title='Citizen Kane';
  1. 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%';
  1. id yes 11768, 11955, 21191 What is the name of the movie ?
select title from movie
  where id in ('11768','11955','21191');
  1. actress ’Glenn Close’ The number of id What is it ?
select id from actor 
  where name = 'Glenn Close';
  1. North African spy film ’Casablanca’ The number of id What is it ?
select id from movie
  where title='Casablanca';
  1. 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';
  1. 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');
  1. 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';
  1. 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;
  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;
  1. 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;
  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;
  1. 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;
  1. 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';
  1. List the departments department yes NULL A worthy teacher .
select name from teacher
  where dept is NULL;
  1. 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;
  1. 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;
  1. 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;
  1. 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;
  1. 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;
  1. Use COUNT Count the number of teachers and mobile phones .
select count(name),count(mobile) from teacher;
  1. 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;
  1. 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;
  1. 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;
  1. How many stops are in the database.
select count(id) from stops;
  1. Find the id value for the stop ‘Craiglockhart’.
SELECT id FROM stops 
  WHERE name = 'Craiglockhart';
  1. 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';
  1. 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;
  1. 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
  1. 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';
  1. 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';
  1. 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';
  1. 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'));
  1. 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
原网站

版权声明
本文为[Amelia who loves learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110719596652.html