All of my answers from the SQLBolt
Click Here, If you want to improve your SQL skills and acquire more experience with difficult queries
Exercise 1 — Tasks
- Find the title of each film
SELECT Title FROM movies;
- Find the director of each film
SELECT Director FROM movies;
- Find the title and director of each film
SELECT Title, Director FROM movies;
- Find the title and year of each film
SELECT Title, Year FROM movies;
- Find all the information about each film
SELECT * FROM movies;
Exercise 2 — Tasks
- Find the movie with a row id of 6
SELECT * FROM movies WHERE Id = 6;
- Find the movies released in the years between 2000 and 2010
SELECT * FROM movies WHERE Year BETWEEN 2000 AND 2010;
- Find the movies not released in the years between 2000 and 2010
SELECT * FROM movies WHERE Year NOT BETWEEN 2000 AND 2010;
- Find the first 5 Pixar movies and their release year
SELECT Title, Year FROM movies LIMIT 5;
Exercise 3 — Tasks
- Find all the Toy Story movies
SELECT * FROM movies WHERE Title LIKE '%Toy Story%';
- Find all the movies directed by John Lasseter
SELECT * FROM movies WHERE Director = 'John Lasseter';
- Find all the movies (and director) not directed by John Lasseter
SELECT Title, Director FROM movies WHERE Director != 'John Lasseter';
- Find all the WALL-* movies
SELECT Title FROM movies WHERE Title LIKE '%WALL%';
Exercise 4 — Tasks
- List all directors of Pixar movies (alphabetically), without duplicates
SELECT DISTINCT Director FROM movies ORDER BY Director;
- List the last four Pixar movies released (ordered from most recent to least)
SELECT Title FROM movies ORDER BY Year DESC LIMIT 4;
- List the first five Pixar movies sorted alphabetically
SELECT Title FROM movies ORDER BY Title LIMIT 5;
- List the next five Pixar movies sorted alphabetically
SELECT Title FROM movies ORDER BY Title LIMIT 5 OFFSET 5;
Review 1 — Tasks
- List all the Canadian cities and their populations
SELECT City, Population FROM north_american_cities
WHERE Country = 'Canada';
- Order all the cities in the United States by their latitude from north to south
SELECT City FROM north_american_cities
WHERE Country = 'United States'
ORDER BY Latitude DESC;
- List all the cities west of Chicago, ordered from west to east
SELECT City, Longitude FROM north_american_cities
WHERE Longitude < -87.629798
ORDER BY Longitude ASC;
4.List the two largest cities in Mexico (by population)
SELECT City, Population FROM north_american_cities
WHERE Country = 'Mexico'
ORDER BY Population DESC
LIMIT 2;
- List the third and fourth largest cities (by population) in the United States and their population
SELECT City, Population FROM north_american_cities
WHERE Country LIKE 'United States'
ORDER BY Population
LIMIT 2 OFFSET 2;
Exercise 6 — Tasks
- Find the domestic and international sales for each movie
SELECT Title , Domestic_sales, International_sales FROM Movies
INNER JOIN Boxoffice ON
Boxoffice.Movie_id = Movies.Id;
- Show the sales numbers for each movie that did better internationally rather than domestically
SELECT Title, Domestic_sales,International_sales FROM Movies
INNER JOIN Boxoffice ON
Movies.Id = Boxoffice.Movie_id
WHERE International_sales > Domestic_sales;
- List all the movies by their ratings in descending order
SELECT Title, Rating FROM Movies
INNER JOIN Boxoffice ON
Movies.Id = Boxoffice.Movie_id
ORDER BY Rating DESC;
Exercise 7 — Tasks
- Find the list of all buildings that have employees
SELECT DISTINCT Building FROM Employees;
- Find the list of all buildings and their capacity
SELECT * FROM Buildings;
- List all buildings and the distinct employee roles in each building (including empty buildings)
SELECT DISTINCT Building_name, Role FROM Buildings
LEFT JOIN Employees ON Building_name = Building;
Exercise 8 — Tasks
- 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;
- Find the names of the buildings that hold no employees
SELECT DISTINCT Building_name FROM Buildings
LEFT JOIN Employees ON Building_name = Building
WHERE Role IS NULL;
Exercise 9 — Tasks
- List all movies and their combined sales in millions of dollars
SELECT DISTINCT Title, (Domestic_sales + International_sales) / 1000000 AS Sales FROM Movies
INNER JOIN Boxoffice ON
Movies.Id = Boxoffice.Movie_id;
- List all movies and their ratings in percent
SELECT DISTINCT Title, (Rating * 10) AS Rating_Percent FROM Movies
INNER JOIN Boxoffice ON
Movies.Id = Boxoffice.Movie_id;
- List all movies that were released on even number years
SELECT Title FROM Movies WHERE Year % 2 = 0;
Exercise 10 — Tasks
- Find the longest time that an employee has been at the studio
SELECT MAX(Years_employed) FROM employees;
- For each role, find the average number of years employed by employees in that role
SELECT Role, AVG(Years_employed) AS Average_Years FROM Employees GROUP BY Role;
- Find the total number of employee years worked in each building
SELECT DISTINCT Building, SUM(Years_employed) FROM Employees
GROUP BY Building;
Exercise 11 — Tasks
- Find the number of Artists in the studio (without a HAVING clause)
SELECT COUNT(*) FROM Employees
WHERE Role LIKE 'Artist';
- Find the number of Employees of each role in the studio
SELECT DISTINCT Role, COUNT(*) FROM Employees
GROUP BY Role;
- Find the total number of years employed by all Engineers.
SELECT Role, SUM(Years_employed) FROM Employees
GROUP BY Role
HAVING Role = 'Engineer';
Exercise 12 — Tasks
- Find the number of movies each director has directed
SELECT Director , COUNT(Title) AS Amount_of_Movies FROM Movies
GROUP BY Director;
- Find the total domestic and international sales that can be attributed to each director
SELECT Director, SUM(Domestic_sales + International_sales) AS Sales FROM Movies
INNER JOIN Boxoffice ON
Movies.Id = Boxoffice.Movie_id
GROUP BY Director;
Exercise 13 — Tasks
- 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', 'John Lasseter', 2013,100);
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 Boxoffice VALUES(4,8.7,340000000,270000000);
Exercise 14 — Tasks
- 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;
OR
UPDATE Movies
SET Director = 'John Lasseter'
WHERE Title = "A Bug's Life";
- The year that Toy Story 2 was released is incorrect, it was actually released in 1999
UPDATE Movies
SET Year = '1999'
WHERE Title = 'Toy Story 2'
- 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 Movies.Id = 11;
Exercise 15 — Tasks
- 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';
Exercise 16 — Tasks
- 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 REAL,
Download_count INTEGER);
Exercise 17 — Tasks
- 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;
- 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 COLUMN Language TEXT DEFAULT 'English';
Exercise 18 — Tasks
- We've sadly reached the end of our lessons, lets clean up by removing the Movies table
DROP TABLE IF EXISTS Movies ;
- And drop the BoxOffice table as well
DROP TABLE IF EXISTS BoxOffice ;