为了方便复习。同时感谢这个网站的制作者以及各位大佬。
一、SELECT 查询 101
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 title,id,Length_minutes FROM movies;
7.请列出所有电影的ID,名称和出版国(即美国)
SELECT id,title,'美国' 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
ORDER BY id ASC LIMIT 5;
5.找到2010(含)年之后的电影里片长小于两个小时的片子
SELECT * FROM movies
WHERE length_minutes<120 AND year>=2010;
6.找到99年和09年的电影,只要列出年份和片长看下
SELECT year,length_minutes FROM movies
WHERE year = 1999 OR year = 2009;
三、条件查询2
1.找到所有Toy Story系列电影
SELECT * FROM movies
WHERE title LIKE '%Toy Story%';
2.找到所有John Lasseter导演的电影
SELECT * FROM movies
WHERE director LIKE 'John Lasseter';
3.找到所有不是John Lasseter导演的电影
SELECT * FROM movies
WHERE director NOT LIKE 'John Lasseter';
4.找到所有电影名为 “WALL-” 开头的电影
SELECT * FROM movies
WHERE title LIKE 'WALL-%';
5.有一部98年电影中文名《虫虫危机》请给我找出来
SELECT * FROM movies
WHERE year=1998 AND title LIKE 'A Bug's Life';
6.找出所有Pete导演的电影,只要列出电影名,导演名和年份就可以
SELECT title,director,year FROM movies
WHERE director LIKE '%Pete%';
John Lasseter导演了两个系列,一个Car系列一个Toy Story系列,请帮我列出这John Lasseter导演两个系列千禧年之后的电影
SELECT * FROM movies
WHERE director='John Lasseter' AND
(title LIKE '%Car%' OR title LIKE '%Toy Story%')
AND year>2000;
四、查询过滤
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;
SELECT * FROM movies
ORDER BY director ASC,year DESC
LIMIT 10;
五、复杂查询
1.列出所有加拿大人的Canadian信息(包括所有字段)
SELECT * FROM north_american_cities
WHERE country LIKE 'Canada';
2.列出所有美国United States的城市按纬度从北到南排序(包括所有字段)
SELECT * FROM north_american_cities
WHERE country LIKE 'United States'
ORDER BY latitude DESC;
3.列出所有在Chicago西部的城市,从西到东排序(包括所有字段)
SELECT * FROM north_american_cities
WHERE longitude<(SELECT longitude
FROM north_american_cities WHERE city='Chicago')
ORDER BY longitude DESC;
4.用人口数population排序,列出墨西哥Mexico最大的2个城市(包括所有字段)
SELECT * FROM north_american_cities
WHERE country = 'Mexico'
ORDER BY population DESC LIMIT 2;
SELECT * FROM north_american_cities
WHERE country = 'United States'
ORDER BY population DESC
LIMIT 2 OFFSET 2;
6.北美所有城市,请按国家名字母序从A-Z再按人口从多到少排列看下前10位的城市(包括所有字段)
SELECT * FROM north_american_cities
ORDER BY country ASC,population DESC
LIMIT 10;
接下来的写在另一篇里面。