Table(表): Movies
Id | Title | Director | Year | Length_minutes |
1 | Toy Story | John Lasseter | 1995 | 81 |
2 | A Bug's Life | John Lasseter | 1998 | 95 |
3 | Toy Story 2 | John Lasseter | 1999 | 93 |
4 | Monsters, Inc. | Pete Docter | 2001 | 92 |
5 | Finding Nemo | Finding Nemo | 2003 | 107 |
6 | The Incredibles | Brad Bird | 2004 | 116 |
7 | Cars | John Lasseter | 2006 | 117 |
8 | Ratatouille | Brad Bird | 2007 | 115 |
9 | WALL-E | Andrew Stanton | 2008 | 104 |
10 | Up | Pete Docter | 2009 | 101 |
11 | Toy Story 3 | Lee Unkrich | 2010 | 103 |
12 | Cars 2 | John Lasseter | 2011 | 120 |
13 | Brave | Brenda Chapman | 2012 | 102 |
14 | Monsters University | Dan Scanlon | 2013 | 110 |
1.找到所有电影的名称title
SELECT title FROM movies;
2.找到所有电影的导演
SELECT director FROM movies;
3.找到所有电影的名称和导演
SELECT title,director FROM movies;
4.找到所有电影的名称和上映年份
SELECT title,year FROM movies;
5.找到所有电影的所有信息
SELECT * FROM movies;
6.找到所有电影的名称,Id和播放时长
SELECT id,title,length_minutes FROM movies;
1.找到id
为6的电影
SELECT * FROM movies where id='6';
2.找到在2000-2010年间year
上映的电影
SELECT * FROM movies where year between '2000' and '2010';
3.找到不是在2000-2010年间year
上映的电影
SELECT * FROM movies where year not between '2000' and '2010';
4.找到头5部电影
SELECT * FROM movies where id<='5';
5.找到2010(含)年之后的电影里片长小于两个小时的片子
SELECT * FROM movies where year>='2010' and length_minutes<'120';
1.找到所有Toy Story
系列电影
SELECT * FROM movies where title like 'Toy Story%';
2.找到所有John Lasseter
导演的电影
SELECT * FROM movies where director='John Lasseter';
3.找到所有不是John Lasseter
导演的电影
SELECT * FROM movies where director!='John Lasseter';
4.找到所有电影名为 "WALL-"
开头的电影
SELECT * FROM movies where title like 'WALL-%';
5.有一部98年电影中文名《虫虫危机》请给我找出来
SELECT * FROM movies where year like '%98';
1.按导演名排重
列出所有电影(只显示导演),并按导演名正序排列
SELECT distinct director FROM movies order by director asc;
2.列出按上映年份最新
上线的4部电影
SELECT * FROM movies order by year desc limit 4;
3.按电影名字母序升序
排列,列出前5部电影
SELECT * FROM movies order by title asc limit 5;
4.按电影名字母序升序排列,列出上一题之后
的5部电影
SELECT * FROM movies order by title asc limit 5 offset 5;
5.如果按片长排列,John Lasseter导演导过片长第3长的电影是哪部,列出名字即可
SELECT title FROM movies where director='John Lasseter'
order by length_minutes desc limit 1 offset 2;
1.列出所有加拿大人的Canadian
信息(包括所有字段)
SELECT * FROM north_american_cities where country='Canada';
2.列出所有在Chicago
西部的城市,从西到东排序(包括所有字段)
SELECT * FROM north_american_cities where longitude<(select longitude
FROM north_american_cities where city='Chicago')
order by longitude asc;
3.用人口数population
排序,列出墨西哥Mexico
最大的2个城市(包括所有字段)
select * from North_american_cities where country='Mexico'
order by population desc
limit 2;
4.列出美国United States
人口3-4位的两个城市和他们的人口(包括所有字段)
select * from North_american_cities where country='United States'
order by population desc
limit 2 offset 2;
1.找到所有电影的国内Domestic_sales
和国际销售额
SELECT * FROM movies a
inner join Boxoffice b on b.movie_id=a.id;
2.找到所有国际销售额比国内销售大的电影
SELECT * FROM movies a
inner join Boxoffice b on b.movie_id=a.id and b.international_sales>b.domestic_sales;
3.找出所有电影按市场占有率rating
倒序排列
SELECT * FROM movies a
inner join Boxoffice b on b.movie_id=a.id
order by rating desc;
4.每部电影按国际销售额比较,排名最靠前的导演是谁,国际销量多少
SELECT director,international_sales FROM movies a
inner join Boxoffice b on b.movie_id=a.id
order by international_sales desc
limit 1;
延伸:查找国际销售额最多的导演和销量
SELECT director,sum(international_sales) as international_sales FROM movies a
inner join Boxoffice b on b.movie_id=a.id
group by director
order by international_sales desc
limit 1;
1.找到所有有雇员的办公室(buildings
)名字
SELECT distinct a.building_name FROM Buildings a
left join employees b on b.building=a.building_name
where building is not null;
2.找到所有办公室里的所有角色(包含没有雇员的),并做唯一输出(DISTINCT
)
SELECT distinct a.building_name,b.role FROM Buildings a
left join employees b on b.building=a.building_name;
3.找到所有有雇员的办公室(buildings
)和对应的容量
SELECT distinct a.building_name,a.capacity FROM Buildings a
left join employees b on b.building=a.building_name
where role is not null;