SQL练习

本文提供了一系列逐步深入的SQL练习,涵盖了从基础查询到复杂的联接、约束条件、聚合函数和数据操作。通过这些例子,读者可以掌握如何查询电影数据库中的信息,如导演、年份、销售额等,并学习如何过滤、排序和聚合数据。此外,还介绍了如何插入、更新和删除数据,以及创建和修改数据库表格。
摘要由CSDN通过智能技术生成

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
    
SQL Lesson 8: A short note on NULLs

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
EngineerYancy I.0
ArtistOliver P.0
  1. Find the name and role of all employees who have not been assigned to a building

    SELECT name, role 
    FROM employees 
    where building is null;
    
  2. Find the names of the buildings that hold no employees

    SELECT b.building_name 
    FROM buildings b 
    left join employees e
    on b.building_name = e.building
    where role is null;
    
SQL Lesson 9: Queries with expressions

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. List all movies and their combined sales in millions of dollars

    SELECT title, (Domestic_sales + International_sales)/1000000 as sales 
    FROM movies m 
    left join boxoffice b
    on m.id = b.movie_id;
    
  2. List all movies and their ratings in percent

    SELECT title, rating * 10 as rating
    FROM movies m 
    left join boxoffice b
    on m.id = b.movie_id;
    
  3. List all movies that were released on even number years

    SELECT * FROM movies
    where year % 2= 0;
    
SQL Lesson 10: Queries with aggregates (Pt. 1)

Table: Employees

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 longest time that an employee has been at the studio

    SELECT MAX(years_employed) as max_years_employed
    FROM employees;
    
  2. For each role, find the average number of years employed by employees in that role

    SELECT role, avg(years_employed) as avg_years_employed
    FROM employees
    group by role;
    
  3. Find the total number of employee years worked in each building

    SELECT building, sum(years_employed) as sum_years_employed
    FROM employees
    group by building;
    
SQL Lesson 11: Queries with aggregates (Pt. 2)

Table: Employees

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 number of Artists in the studio (without a HAVING clause)

    SELECT role, count(*) as Number_of_artists
    FROM employees
    where role = "Artist";	
    
  2. Find the number of Employees of each role in the studio

    SELECT building, role, count(*) as count 
    FROM employees
    group by role;
    
  3. Find the total number of years employed by all Engineers

    SELECT role, sum(years_employed) as sum_years_employed 
    FROM employees 
    where role = "Engineer";
    
SQL Lesson 12: Order of execution of a Query

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 number of movies each director has directed

    SELECT director, count(*) as Count
    FROM movies 
    group by director;
    
  2. Find the total domestic and international sales that can be attributed to each director

    SELECT director, sum(Domestic_sales+International_sales) as sum_sales
    FROM movies m 
    left join Boxoffice b
    on m.id = b.movie_id
    group by director;
    
SQL Lesson 13: Inserting rows

Table: Movies (Read-Only)

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993

Table: Boxoffice (Read-Only)

Movie_idRatingDomestic_salesInternational_sales
37.9245852179239163000
18.3191796233170162503
27.2162798565200600000
  1. Add the studio’s new production, Toy Story 4 to the list of movies (you can use any director)

    INSERT INTO movies VALUES (4, "Toy Story 4", "El Directore", 2015, 90);
    
  2. Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million domestically and 270 million internationally. Add the record to the BoxOffice table.

    INSERT INTO movies VALUES (4, "Toy Story 4", "El Directore", 2015, 90);
    
SQL Lesson 14: Updating rows

Table: Movies

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeEl Directore199895
3Toy Story 2John Lasseter189993
4Monsters, Inc.Pete Docter200192
5Finding NemoAndrew Stanton2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 8El Directore2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110
  1. The director for A Bug’s Life is incorrect, it was actually directed by John Lasseter

    update movies
    set director = "John Lasseter"
    where id = 2;
    
  2. The year that Toy Story 2 was released is incorrect, it was actually released in 1999

    update movies
    set year = 1999
    where id = 3;
    
  3. Both the title and director for Toy Story 8 is incorrect! The title should be “Toy Story 3” and it was directed by Lee Unkrich

    update movies
    set title = "Toy Story 3",director = "Lee Unkrich"
    where id = 11;
    
SQL Lesson 15: Deleting rows

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. This database is getting too big, lets remove all movies that were released before 2005.

    delete from movies where year < 2005;
    
  2. Andrew Stanton has also left the studio, so please remove all movies directed by him.

    delete from movies where director = "Andrew Stanton";
    
SQL Lesson 16: Creating tables
  1. Create a new table named Database with the following columns:

    Name A string (text) describing the name of the database
    Version A number (floating point) of the latest version of this database
    Download_count An integer count of the number of times this database was downloaded

    This table has no constraints.

    create table Database (
        Name text,
        Version float,
        Download_count integer
    );
    
SQL Lesson 17: Altering tables

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. Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was released in.

    alter table movies
    add Aspect_ratio float;
    
  2. Add another column named Language with a TEXT data type to store the language that the movie was released in. Ensure that the default for this language is English.

    alter table movies 
    add Language text default English;
    
SQL Lesson 18: Dropping tables

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. We’ve sadly reached the end of our lessons, lets clean up by removing the Movies table

    drop table movies;
    
  2. And drop the BoxOffice table as well

    drop table Boxoffice;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值