SQL练习(更新中...)

SQL练习

地址:https://sqlbolt.com/
SQL Lesson 1: SELECT queries 101

Table: Movies

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoAndrew Stanton2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110
  1. Find the title of each film

    select title from movies;
    
  2. Find the director of each film

    select director from movies;
    
  3. Find the title and director of each film

    select title, director from movies;
    
  4. Find the title and year of each film

    select title, year from movies;
    
  5. Find all the information about each film

    select * from movies;
    
SQL Lesson 2: Queries with constraints (Pt. 1)

Table: Movies

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoAndrew Stanton2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110
  1. Find the movie with a row id of 6

    SELECT * FROM movies 
    where id = 6;
    
  2. Find the movies released in the years between 2000 and 2010

    SELECT * FROM movies 
    where year between 2000 and 2010;
    
  3. Find the movies not released in the years between 2000 and 2010

    SELECT * FROM movies 
    where year not between 2000 and 2010;
    
  4. Find the first 5 Pixar movies and their release year

    SELECT * FROM movies 
    limit 5;
    
SQL Lesson 3: Queries with constraints (Pt. 2)

Table: Movies

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoAndrew Stanton2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110
87WALL-GBrenda Chapman204297
  1. Find all the Toy Story movies

    SELECT * FROM movies 
    where title like "%Toy Story%";
    
  2. Find all the movies directed by John Lasseter

    SELECT * FROM movies 
    where director = "John Lasseter";
    
  3. Find all the movies (and director) not directed by John Lasseter

    SELECT * FROM movies 
    where director != "John Lasseter";
    
  4. Find all the WALL-* movies

    SELECT * FROM movies 
    where title like "WALL%";
    
SQL Lesson 4: Filtering and sorting Query results

Table: Movies

IdTitleDirectorYearLength_minutes
1Cars 2John Lasseter2011120
2RatatouilleBrad Bird2007115
3A Bug’s LifeJohn Lasseter199895
4Finding NemoAndrew Stanton2003107
5WALL-EAndrew Stanton2008104
6CarsJohn Lasseter2006117
7The IncrediblesBrad Bird2004116
8Toy Story 3Lee Unkrich2010103
9Monsters UniversityDan Scanlon2013110
10Toy StoryJohn Lasseter199581
11Monsters, Inc.Pete Docter200192
12Toy Story 2John Lasseter199993
13UpPete Docter2009101
14BraveBrenda Chapman2012102
  1. List all directors of Pixar movies (alphabetically), without duplicates

    select distinct director 
    from Movies 
    order by director asc;
    
  2. List the last four Pixar movies released (ordered from most recent to least)

    SELECT * FROM movies 
    order by year desc 
    limit 4;
    
  3. List the first five Pixar movies sorted alphabetically

    SELECT * FROM movies 
    order by title asc 
    limit 5;
    
  4. List the next five Pixar movies sorted alphabetically

    SELECT * FROM movies 
    order by title asc 
    limit 5 offset 5;
    
SQL Review: Simple SELECT Queries

Table: North_american_cities

CityCountryPopulationLatitudeLongitude
GuadalajaraMexico150080020.659699-103.349609
TorontoCanada279506043.653226-79.383184
HoustonUnited States219591429.760427-95.369803
New YorkUnited States840583740.712784-74.005941
PhiladelphiaUnited States155316539.952584-75.165222
HavanaCuba210614623.05407-82.345189
Mexico CityMexico855550019.432608-99.133208
PhoenixUnited States151336733.448377-112.074037
Los AngelesUnited States388430734.052234-118.243685
Ecatepec de MorelosMexico174200019.601841-99.050674
MontrealCanada171776745.501689-73.567256
ChicagoUnited States271878241.878114-87.629798
  1. List all the Canadian cities and their populations

    SELECT city, population FROM north_american_cities 
    where country = "Canada";
    
  2. Order all the cities in the United States by their latitude from north to south

    SELECT * FROM north_american_cities 
    where country = "United States" 
    order by latitude desc;
    
  3. List all the cities west of Chicago, ordered from west to east

    SELECT city FROM north_american_cities 
    where Longitude < (SELECT Longitude FROM north_american_cities where city = "Chicago") 
    order by longitude desc;
    
  4. List the two largest cities in Mexico (by population)

    SELECT * FROM north_american_cities 
    where country = "Mexico" 
    order by population desc
    limit 2;
    
  5. List the third and fourth largest cities (by population) in the United States and their population

    SELECT * FROM north_american_cities 
    where country = "United States" 
    order by population desc
    limit 2 offset 2;
    
SQL Lesson 6: Multi-table queries with JOINs

Table: Movies (Read-Only)

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoAndrew Stanton2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110

Table: Boxoffice (Read-Only)

Movie_idRatingDomestic_salesInternational_sales
58.2380843261555900000
147.4268492764475066843
88206445654417277164
126.4191452396368400000
37.9245852179239163000
68261441092370001000
98.5223808164297503696
118.4415004880648167031
18.3191796233170162503
77.2244082982217900167
108.3293004164438338580
48.1289916256272900000
27.2162798565200600000
137.2237283207301700000
  1. Find the domestic and international sales for each movie

    SELECT * FROM movies m left join Boxoffice b 
    where m.id = b.movie_id;
    
  2. Show the sales numbers for each movie that did better internationally rather than domestically

    SELECT title, domestic_sales, international_sales
    FROM movies
    JOIN boxoffice
    ON movies.id = boxoffice.movie_id
    WHERE international_sales > domestic_sales;
    
  3. List all the movies by their ratings in descending order

    SELECT title, domestic_sales, international_sales
    FROM movies
    JOIN boxoffice
    ON movies.id = boxoffice.movie_id
    order by rating desc;
    
SQL Lesson 7: OUTER JOINs

Table: Buildings (Read-Only)

Building_nameCapacity
1e24
1w32
2e16
2w20

Table: Employees (Read-Only)

RoleNameBuildingYears_employed
EngineerBecky A.1e4
EngineerDan B.1e2
EngineerSharon F.1e6
EngineerDan M.1e4
EngineerMalcom S.1e1
ArtistTylar S.2w2
ArtistSherman D.2w8
ArtistJakob J.2w6
ArtistLillia A.2w7
ArtistBrandon J.2w7
ManagerScott K.1e9
ManagerShirlee M.1e3
ManagerDaria O.2w6
  1. Find the list of all buildings that have employees

    SELECT distinct building FROM employees 
    where building is not null;
    
  2. Find the list of all buildings and their capacity

    SELECT * FROM buildings;
    
  3. List all buildings and the distinct employee roles in each building (including empty buildings)

    select distinct building_name, role 
    from building 
    left join employees
    on building = building_name
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值