题目来源:SQLBolt - Learn SQL - Introduction to SQL
目录
SQL Lesson 1: SELECT queries 101
SQL Lesson 2: Queries with constraints (Pt. 1)
SQL Lesson 3: Queries with constraints (Pt. 2)
SQL Lesson 4: Filtering and sorting Query results
SQL Review: Simple SELECT Queries
SQL Lesson 6: Multi-table queries with JOINs
SQL Lesson 8: A short note on NULLs
SQL Lesson 9: Queries with expressions
SQL Lesson 10: Queries with aggregates (Pt. 1)
SQL Lesson 11: Queries with aggregates (Pt. 2)
SQL Lesson 12: Order of execution of a Query
SQL Lesson 16: Creating tables
SQL Lesson 17: Altering tables
SQL Lesson 18: Dropping tables
SQL Lesson 1: SELECT queries 101
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 | Andrew Stanton | 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 |
- Find the
title
of each filmSELECT Title FROM movies
- Find the
director
of each filmSELECT director FROM movies
- Find the
title
anddirector
of each filmSELECT title,director FROM movies
- Find the
title
andyear
of each filmSELECT title , year FROM movies
- Find
all
the information about each filmSELECT * FROM movies
SQL Lesson 2: Queries with constraints (Pt. 1)
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 | Andrew Stanton | 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 |
- Find the movie with a row
id
of 6 - Find the movies released in the
year
s between 2000 and 2010 - Find the movies not released in the
year
s between 2000 and 2010 - Find the first 5 Pixar movies and their release
year
1.SELECT * FROM movies
where id = 6;
2.SELECT * FROM movies
where year between 2000 and 2010;
3.SELECT * FROM movies
where year not between 2000 and 2010;
4.SELECT title, year FROM movies
where year <= 2003;
SQL Lesson 3: Queries with constraints (Pt. 2)
Operator | Condition | Example |
= | Case sensitive exact string comparison (notice the single equals) | col_name = "abc" |
!= or <> | Case sensitive exact string inequality comparison | col_name != "abcd" |
LIKE | Case insensitive exact string comparison | col_name LIKE "ABC" |
NOT LIKE | Case insensitive exact string inequality comparison | col_name NOT LIKE "ABCD" |
% | Used anywhere in a string to match a sequence of zero or more characters (only with LIKE or NOT LIKE) | col_name LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS") |
_ | Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) | col_name LIKE "AN_" (matches "AND", but not "AN") |
IN (…) | String exists in a list | col_name IN ("A", "B", "C") |
NOT IN (…) | String does not exist in a list | col_name NOT IN ("D", "E", "F") |
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 | Andrew Stanton | 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 |
87 | WALL-G | Brenda Chapman | 2042 | 97 |
- Find all the Toy Story movies
- Find all the movies directed by John Lasseter
- Find all the movies (and director) not directed by John Lasseter
- Find all the WALL-* movies
1.SELECT title, director FROM movies
where title LIKE "Toy Story%";
2.SELECT * FROM movies
WHERE director = 'John Lasseter';
3.SELECT * FROM movies
WHERE director != 'John Lasseter';
4.SELECT * FROM movies
WHERE title LIKE "WALL-_";
SQL Lesson 4: Filtering and sorting Query results
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
Table: Movies
Id | Title | Director | Year | Length_minutes |
1 | WALL-E | Andrew Stanton | 2008 | 104 |
2 | Cars | John Lasseter | 2006 | 117 |
3 | Brave | Brenda Chapman | 2012 | 102 |
4 | The Incredibles | Brad Bird | 2004 | 116 |
5 | Cars 2 | John Lasseter | 2011 | 120 |
6 | Toy Story 3 | Lee Unkrich | 2010 | 103 |
7 | A Bug's Life | John Lasseter | 1998 | 95 |
8 | Ratatouille | Brad Bird | 2007 | 115 |
9 | Monsters University | Dan Scanlon | 2013 | 110 |
10 | Toy Story | John Lasseter | 1995 | 81 |
11 | Finding Nemo | Andrew Stanton | 2003 | 107 |
12 | Toy Story 2 | John Lasseter | 1999 | 93 |
13 | Up | Pete Docter | 2009 | 101 |
14 | Monsters, Inc. | Pete Docter | 2001 | 92 |
- List all directors of Pixar movies (alphabetically), without duplicates
- List the last four Pixar movies released (ordered from most recent to least)
- List the first five Pixar movies sorted alphabetically
- List the next five Pixar movies sorted alphabetically
1.SELECT distinct director FROM movies
order by director;
2.SELECT title
FROM movies
ORDER BY year desc
limit 4
3.select * from movies
order by title
limit 5
4.select * from movies
order by title
limit 5 offset 5
SQL Review: Simple SELECT Queries
Table: North_american_cities
City | Country | Population | Latitude | Longitude |
Guadalajara | Mexico | 1500800 | 20.659699 | -103.349609 |
Toronto | Canada | 2795060 | 43.653226 | -79.383184 |
Houston | United States | 2195914 | 29.760427 | -95.369803 |
New York | United States | 8405837 | 40.712784 | -74.005941 |
Philadelphia | United States | 1553165 | 39.952584 | -75.165222 |
Havana | Cuba | 2106146 | 23.05407 | -82.345189 |
Mexico City | Mexico | 8555500 | 19.432608 | -99.133208 |
Phoenix | United States | 1513367 | 33.448377 | -112.074037 |
Los Angeles | United States | 3884307 | 34.052234 | -118.243685 |
Ecatepec de Morelos | Mexico | 1742000 | 19.601841 | -99.050674 |
Montreal | Canada | 1717767 | 45.501689 | -73.567256 |
Chicago | United States | 2718782 | 41.878114 | -87.629798 |
- List all the Canadian cities and their populations
- Order all the cities in the United States by their latitude from north to south
- List all the cities west of Chicago, ordered from west to east
- List the two largest cities in Mexico (by population)
- List the third and fourth largest cities (by population) in the United States and their population
1.select city , population from North_american_cities
where country = "Canada"
2.select city from North_american_cities
where country = 'United States'
order by Latitude desc
3.SELECT city, longitude FROM north_american_cities
WHERE longitude < -87.629798
ORDER BY longitude
4.select * from North_american_cities
where country = "Mexico"
order by population desc
limit 2
5.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
Select query with INNER JOIN on multiple tables
SELECT column, another_table_column, …
FROM mytable
INNER JOIN another_table
ON mytable.id = another_table.id
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;
Table: Movies (Read-Only)
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 | Andrew Stanton | 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 |
Table: Boxoffice (Read-Only)
Movie_id | Rating | Domestic_sales | International_sales |
5 | 8.2 | 380843261 | 555900000 |
14 | 7.4 | 268492764 | 475066843 |
8 | 8 | 206445654 | 417277164 |
12 | 6.4 | 191452396 | 368400000 |
3 | 7.9 | 245852179 | 239163000 |
6 | 8 | 261441092 | 370001000 |
9 | 8.5 | 223808164 | 297503696 |
11 | 8.4 | 415004880 | 648167031 |
1 | 8.3 | 191796233 | 170162503 |
7 | 7.2 | 244082982 | 217900167 |
10 | 8.3 | 293004164 | 438338580 |
4 | 8.1 | 289916256 | 272900000 |
2 | 7.2 | 162798565 | 200600000 |
13 | 7.2 | 237283207 | 301700000 |
- Find the domestic and international sales for each movie
- Show the sales numbers for each movie that did better internationally rather than domestically
- List all the movies by their ratings in descending order
1.select Title , Domestic_sales , International_sales
from Movies
inner join Boxoffice
on Movies.Id = Boxoffice.Movie_id
2.select Title , Domestic_sales , International_sales
from Movies
inner join Boxoffice
on Movies.Id = Boxoffice.Movie_id
where International_sales > Domestic_sales
3.select Title , rating
from Movies
inner join Boxoffice
on Movies.Id = Boxoffice.Movie_id
order by rating desc
SQL Lesson 7: OUTER JOINs
Table: Buildings (Read-Only)
Building_name | Capacity |
1e | 24 |
1w | 32 |
2e | 16 |
2w | 20 |
Table: Employees (Read-Only)
Role | Name | Building | Years_employed |
Engineer | Becky A. | 1e | 4 |
Engineer | Dan B. | 1e | 2 |
Engineer | Sharon F. | 1e | 6 |
Engineer | Dan M. | 1e | 4 |
Engineer | Malcom S. | 1e | 1 |
Artist | Tylar S. | 2w | 2 |
Artist | Sherman D. | 2w | 8 |
Artist | Jakob J. | 2w | 6 |
Artist | Lillia A. | 2w | 7 |
Artist | Brandon J. | 2w | 7 |
Manager | Scott K. | 1e | 9 |
Manager | Shirlee M. | 1e | 3 |
Manager | Daria O. | 2w | 6 |
- Find the list of all buildings that have employees
- Find the list of all buildings and their capacity
- List all buildings and the distinct employee roles in each building (including empty buildings)
1.SELECT DISTINCT building
FROM employees
2.SELECT * FROM buildings
3.SELECT DISTINCT building_name, role
FROM buildings
LEFT JOIN employees
ON building_name = building;
SQL Lesson 8: A short note on NULLs
Table: Buildings (Read-Only)
Building_name | Capacity |
1e | 24 |
1w | 32 |
2e | 16 |
2w | 20 |
Table: Employees (Read-Only)
Role | Name | Building | Years_employed |
Engineer | Becky A. | 1e | 4 |
Engineer | Dan B. | 1e | 2 |
Engineer | Sharon F. | 1e | 6 |
Engineer | Dan M. | 1e | 4 |
Engineer | Malcom S. | 1e | 1 |
Artist | Tylar S. | 2w | 2 |
Artist | Sherman D. | 2w | 8 |
Artist | Jakob J. | 2w | 6 |
Artist | Lillia A. | 2w | 7 |
Artist | Brandon J. | 2w | 7 |
Manager | Scott K. | 1e | 9 |
Manager | Shirlee M. | 1e | 3 |
Manager | Daria O. | 2w | 6 |
Engineer | Yancy I. | 0 | |
Artist | Oliver P. | 0 |
- Find the name and role of all employees who have not been assigned to a building
- Find the names of the buildings that hold no employees
1.SELECT name , role
FROM employees
where building is null
2.SELECT DISTINCT building_name
FROM buildings
LEFT JOIN employees
ON building_name = building
WHERE role IS NULL;
SQL Lesson 9: Queries with expressions
Table: Movies (Read-Only)
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 | Andrew Stanton | 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 |
Table: Boxoffice (Read-Only)
Movie_id | Rating | Domestic_sales | International_sales |
5 | 8.2 | 380843261 | 555900000 |
14 | 7.4 | 268492764 | 475066843 |
8 | 8 | 206445654 | 417277164 |
12 | 6.4 | 191452396 | 368400000 |
3 | 7.9 | 245852179 | 239163000 |
6 | 8 | 261441092 | 370001000 |
9 | 8.5 | 223808164 | 297503696 |
11 | 8.4 | 415004880 | 648167031 |
1 | 8.3 | 191796233 | 170162503 |
7 | 7.2 | 244082982 | 217900167 |
10 | 8.3 | 293004164 | 438338580 |
4 | 8.1 | 289916256 | 272900000 |
2 | 7.2 | 162798565 | 200600000 |
13 | 7.2 | 237283207 | 301700000 |
- List all movies and their combined sales in millions of dollars
- List all movies and their ratings in percent
- List all movies that were released on even number years
1.SELECT Title ,(Domestic_sales+International_sales)/1000000
as total_sales
FROM movies
join Boxoffice
on Movies.Id =Boxoffice.Movie_id
2.SELECT Title ,rating*10 as rating_persent
FROM movies
join Boxoffice
on Movies.Id =Boxoffice.Movie_id
3.SELECT Title ,year
FROM movies
join Boxoffice
on Movies.Id =Boxoffice.Movie_id
where year%2 = 0
SQL Lesson 10: Queries with aggregates (Pt. 1)
Function | Description |
COUNT(*), COUNT(column) | A common function used to counts the number of rows in the group if no column name is specified. Otherwise, count the number of rows in the group with non-NULL values in the specified column. |
MIN(column) | Finds the smallest numerical value in the specified column for all rows in the group. |
MAX(column) | Finds the largest numerical value in the specified column for all rows in the group. |
AVG(column) | Finds the average numerical value in the specified column for all rows in the group. |
SUM(column) | Finds the sum of all numerical values in the specified column for the rows in the group. |
Table: Employees
Role | Name | Building | Years_employed |
Engineer | Becky A. | 1e | 4 |
Engineer | Dan B. | 1e | 2 |
Engineer | Sharon F. | 1e | 6 |
Engineer | Dan M. | 1e | 4 |
Engineer | Malcom S. | 1e | 1 |
Artist | Tylar S. | 2w | 2 |
Artist | Sherman D. | 2w | 8 |
Artist | Jakob J. | 2w | 6 |
Artist | Lillia A. | 2w | 7 |
Artist | Brandon J. | 2w | 7 |
Manager | Scott K. | 1e | 9 |
Manager | Shirlee M. | 1e | 3 |
Manager | Daria O. | 2w | 6 |
- Find the longest time that an employee has been at the studio
- For each role, find the average number of years employed by employees in that role
- Find the total number of employee years worked in each building
1.SELECT name , max(Years_employed)
FROM employees
2.select role , avg(Years_employed) as avg_years
from employees
group by role
3.select building , sum(Years_employed)
from employees
group by building
SQL Lesson 11: Queries with aggregates (Pt. 2)
Table: Employees
Role | Name | Building | Years_employed |
Engineer | Becky A. | 1e | 4 |
Engineer | Dan B. | 1e | 2 |
Engineer | Sharon F. | 1e | 6 |
Engineer | Dan M. | 1e | 4 |
Engineer | Malcom S. | 1e | 1 |
Artist | Tylar S. | 2w | 2 |
Artist | Sherman D. | 2w | 8 |
Artist | Jakob J. | 2w | 6 |
Artist | Lillia A. | 2w | 7 |
Artist | Brandon J. | 2w | 7 |
Manager | Scott K. | 1e | 9 |
Manager | Shirlee M. | 1e | 3 |
Manager | Daria O. | 2w | 6 |
- Find the number of Artists in the studio (without a HAVING clause)
- Find the number of Employees of each role in the studio
- Find the total number of years employed by all Engineers
1.select role , count(*) as num
from employees
where role = "Artist"
2.select role , count(*)
from employees
group by role
3.SELECT role, SUM(years_employed)
FROM employees
GROUP BY role
HAVING role = "Engineer";
SQL Lesson 12: Order of execution of a Query
Table: Movies (Read-Only)
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 | Andrew Stanton | 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 |
Table: Boxoffice (Read-Only)
Movie_id | Rating | Domestic_sales | International_sales |
5 | 8.2 | 380843261 | 555900000 |
14 | 7.4 | 268492764 | 475066843 |
8 | 8 | 206445654 | 417277164 |
12 | 6.4 | 191452396 | 368400000 |
3 | 7.9 | 245852179 | 239163000 |
6 | 8 | 261441092 | 370001000 |
9 | 8.5 | 223808164 | 297503696 |
11 | 8.4 | 415004880 | 648167031 |
1 | 8.3 | 191796233 | 170162503 |
7 | 7.2 | 244082982 | 217900167 |
10 | 8.3 | 293004164 | 438338580 |
4 | 8.1 | 289916256 | 272900000 |
2 | 7.2 | 162798565 | 200600000 |
13 | 7.2 | 237283207 | 301700000 |
- Find the number of movies each director has directed
- Find the total domestic and international sales that can be attributed to each director
1.SELECT director , count(*)
FROM movies
group by director
2.select director , sum(Domestic_sales+International_sales) as total_sales
from movies
join Boxoffice
on Movies.Id = Boxoffice.Movie_id
group by director
SQL Lesson 13: Inserting rows
INSERT INTO mytable
(column, another_column, …)
VALUES (value_or_expr, another_value_or_expr, …),
(value_or_expr_2, another_value_or_expr_2, …),
…;
Table: Movies (Read-Only)
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 |
Table: Boxoffice (Read-Only)
Movie_id | Rating | Domestic_sales | International_sales |
3 | 7.9 | 245852179 | 239163000 |
1 | 8.3 | 191796233 | 170162503 |
2 | 7.2 | 162798565 | 200600000 |
- Add the studio's new production, Toy Story 4 to the list of movies (you can use any director)
- 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.
1.insert into movies
(Title,Director)
values('Toy Story 4',"John Lasseter")
2.INSERT INTO boxoffice
VALUES (4, 8.7, 340000000, 270000000)
SQL Lesson 14: Updating rows
Update statement with values
UPDATE mytable
SET column = value_or_expr,
other_column = another_value_or_expr,
…
WHERE condition;
Table: Movies
Id | Title | Director | Year | Length_minutes |
1 | Toy Story | John Lasseter | 1995 | 81 |
2 | A Bug's Life | El Directore | 1998 | 95 |
3 | Toy Story 2 | John Lasseter | 1899 | 93 |
4 | Monsters, Inc. | Pete Docter | 2001 | 92 |
5 | Finding Nemo | Andrew Stanton | 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 8 | El Directore | 2010 | 103 |
12 | Cars 2 | John Lasseter | 2011 | 120 |
13 | Brave | Brenda Chapman | 2012 | 102 |
14 | Monsters University | Dan Scanlon | 2013 | 110 |
- The director for A Bug's Life is incorrect, it was actually directed by John Lasseter
- The year that Toy Story 2 was released is incorrect, it was actually released in 1999
- 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
1.UPDATE movies
SET director = "John Lasseter"
WHERE id = 2
2.update movies
set year=1999
where Title="Toy Story 2"
3.update movies
set title = "Toy Story 3", director = "Lee Unkrich"
where id = 11
SQL Lesson 15: Deleting rows
DELETE FROM mytable
WHERE condition;
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 | Andrew Stanton | 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 |
- This database is getting too big, lets remove all movies that were released before 2005.
- Andrew Stanton has also left the studio, so please remove all movies directed by him.
1.delete from movies
where year < 2005
2.delete from movies
where director = "Andrew Stanton";
SQL Lesson 16: Creating tables
Data type | Description |
INTEGER , BOOLEAN | The integer datatypes can store whole integer values like the count of a number or an age. In some implementations, the boolean value is just represented as an integer value of just 0 or 1. |
FLOAT , DOUBLE , REAL | The floating point datatypes can store more precise numerical data like measurements or fractional values. Different types can be used depending on the floating point precision required for that value. |
CHARACTER(num_chars) , VARCHAR(num_chars) , TEXT | The text based datatypes can store strings and text in all sorts of locales. The distinction between the various types generally amount to underlaying efficiency of the database when working with these columns. Both the CHARACTER and VARCHAR (variable character) types are specified with the max number of characters that they can store (longer values may be truncated), so can be more efficient to store and query with big tables. |
DATE , DATETIME | SQL can also store date and time stamps to keep track of time series and event data. They can be tricky to work with especially when manipulating data across timezones. |
BLOB | Finally, SQL can store binary data in blobs right in the database. These values are often opaque to the database, so you usually have to store them with the right metadata to requery them. |
Constraint | Description |
PRIMARY KEY | This means that the values in this column are unique, and each value can be used to identify a single row in this table. |
AUTOINCREMENT | For integer values, this means that the value is automatically filled in and incremented with each row insertion. Not supported in all databases. |
UNIQUE | This means that the values in this column have to be unique, so you can't insert another row with the same value in this column as another row in the table. Differs from the `PRIMARY KEY` in that it doesn't have to be a key for a row in the table. |
NOT NULL | This means that the inserted value can not be `NULL`. |
CHECK (expression) | This allows you to run a more complex expression to test whether the values inserted are valid. For example, you can check that values are positive, or greater than a specific size, or start with a certain prefix, etc. |
FOREIGN KEY | This is a consistency check which ensures that each value in this column corresponds to another value in a column in another table. For example, if there are two tables, one listing all Employees by ID, and another listing their payroll information, the `FOREIGN KEY` can ensure that every row in the payroll table corresponds to a valid employee in the master Employee list. |
CREATE TABLE movies (
id INTEGER PRIMARY KEY,
title TEXT,
director TEXT,
year INTEGER,
length_minutes INTEGER
);
- Create a new table named
Database
with the following columns:–
This table has no constraints.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
CREATE TABLE Database (
Name TEXT,
Version FLOAT,
Download_count INTEGER
);
SQL Lesson 17: Altering tables
Adding columns
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
DEFAULT default_value;
---------------------------------------------------
Removing columns
ALTER TABLE mytable
DROP column_to_be_deleted;
----------------------------------------------------
Renaming the table
ALTER TABLE mytable
RENAME TO new_table_name;
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 | Andrew Stanton | 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 |
- Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was released in.
- 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.
1.alter table Movies
add Aspect_ratio
default float
2.alter table Movies
add COLUMN Language TEXT DEFAULT "English"
SQL Lesson 18: Dropping tables
DROP TABLE IF EXISTS mytable;
Table: Movies (Read-Only)
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 | Andrew Stanton | 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 |
Table: Boxoffice (Read-Only)
Movie_id | Rating | Domestic_sales | International_sales |
5 | 8.2 | 380843261 | 555900000 |
14 | 7.4 | 268492764 | 475066843 |
8 | 8 | 206445654 | 417277164 |
12 | 6.4 | 191452396 | 368400000 |
3 | 7.9 | 245852179 | 239163000 |
6 | 8 | 261441092 | 370001000 |
9 | 8.5 | 223808164 | 297503696 |
11 | 8.4 | 415004880 | 648167031 |
1 | 8.3 | 191796233 | 170162503 |
7 | 7.2 | 244082982 | 217900167 |
10 | 8.3 | 293004164 | 438338580 |
4 | 8.1 | 289916256 | 272900000 |
2 | 7.2 | 162798565 | 200600000 |
13 | 7.2 | 237283207 | 301700000 |
- We've sadly reached the end of our lessons, lets clean up by removing the Movies table
- And drop the BoxOffice table as well
1.drop table IF EXISTS Movies
2.drop table IF EXISTS BoxOffice