Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Last active December 9, 2024 03:49
Show Gist options
  • Save CSaratakij/dfef81cd71c0f616d4d36a189247b19e to your computer and use it in GitHub Desktop.
Save CSaratakij/dfef81cd71c0f616d4d36a189247b19e to your computer and use it in GitHub Desktop.
SQL query practice : 6002526

SQL query practice : (6002526)

Lesson 1 : SELECT queries 101

  1. Find the title of each film
SELECT title FROM movies;
  1. Find the director of each film
SELECT director FROM movies;
  1. Find the title and director of each film
SELECT title, director FROM movies;
  1. Find the title and year of each film
SELECT title, year FROM movies;
  1. Find all the information about each film
SELECT * FROM movies;

Lesson 2 : Queries with constraints (Pt. 1)

  1. Find the movie with a row id of 6
SELECT * FROM movies WHERE id = 6;
  1. Find the movies released in the years between 2000 and 2010
SELECT * FROM movies WHERE year BETWEEN 2000 AND 2010;
  1. Find the movies not released in the years between 2000 and 2010
SELECT * FROM movies WHERE year NOT BETWEEN 2000 AND 2010;
  1. Find the first 5 Pixar movies and their release year
SELECT * FROM movies WHERE year LIMIT 5;

Lesson 3 : Queries with constraints (Pt. 2)

  1. Find all the Toy Story movies
SELECT title FROM movies WHERE title LIKE "Toy Story%";
  1. Find all the movies directed by John Lasseter
SELECT title FROM movies WHERE director = "John Lasseter";
  1. Find all the movies (and director) not directed by John Lasseter
SELECT title FROM movies WHERE director != "John Lasseter";
  1. Find all the WALL-* movies
SELECT title FROM movies WHERE title LIKE "WALL-%"

Lesson 4 : Filtering and sorting Query results

  1. List all directors of Pixar movies (alphabetically), without duplicates
SELECT DISTINCT director FROM movies ORDER BY director;
  1. List the last four Pixar movies released (ordered from most recent to least)
SELECT DISTINCT title FROM movies ORDER BY year DESC LIMIT 4;
  1. List the first five Pixar movies sorted alphabetically
SELECT title FROM movies ORDER BY title LIMIT 5;
  1. List the next five Pixar movies sorted alphabetically
SELECT title FROM movies ORDER BY title LIMIT 5 OFFSET 5;

Lesson 5 : Review Simple SELECT Queries

  1. List all the Canadian cities and their populations
SELECT city, population
FROM north_american_cities
WHERE country = "Canada";
  1. 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;
  1. List all the cities west of Chicago, ordered from west to east
SELECT city
FROM north_american_cities
WHERE longitude < -87.629798
ORDER BY longitude;
  1. List the two largest cities in Mexico (by population)
SELECT city
FROM north_american_cities
WHERE country = "Mexico"
ORDER BY population DESC
LIMIT 2;
  1. List the third and fourth largest cities (by population) in the United States and their population
SELECT city
FROM north_american_cities
WHERE country = "United States"
ORDER BY population DESC
LIMIT 2 OFFSET 2;

Lesson 6 : Multi-table queries with JOINs

  1. Find the domestic and international sales for each movie
SELECT title, domestic_sales, international_sales
FROM movies
INNER JOIN boxoffice
    ON movies.id = boxoffice.movie_id;
  1. 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;
  1. 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;

Lesson 7 : OUTER JOINs

  1. Find the list of all buildings that have employees
SELECT DISTINCT building FROM employees;
  1. Find the list of all buildings and their capacity
SELECT * FROM buildings;
  1. 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 = employees.building;

Lesson 8 : A short note on NULLs

  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;
  1. Find the names of the buildings that hold no employees
SELECT DISTINCT building_name
FROM buildings
LEFT JOIN employees
    ON building_name = employees.building
WHERE employees.building IS NULL;

Lesson 9 : Queries with expressions

  1. 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;
  1. List all movies and their ratings in percent
SELECT DISTINCT
    title,
    (rating * 10) AS rate_percent
FROM movies
INNER JOIN boxoffice
    ON movies.id = boxoffice.movie_id;
  1. List all movies that were released on even number years
SELECT title FROM movies WHERE year % 2 = 0;
@Fatih-Ceylan
Copy link

been very helpful, thanks! 👍

@zhenyajzz
Copy link

ye, thank you !!!

@jyoti-raj
Copy link

Thank you!.....very helpful :)

@Ray-56
Copy link

Ray-56 commented Sep 18, 2021

thank you

@sakshi-git08
Copy link

Thank you so much for sharing!!

@thewalimuhammad
Copy link

Thank you

@AndyGoncalves
Copy link

Thanks!

@Zahin-Azmaeen
Copy link

Review 1 — Tasks
3. SELECT * FROM north_american_cities where longitude < -87.629798
ORDER BY longitude asc;

@sayidbek-bakhrom
Copy link

Great Job!

@Furukawamin
Copy link

Thank you it's useful to practice.

@Hemanth1217
Copy link

great work bro

@piperoldan
Copy link

Great, thank you!!

@marlymarmolejos
Copy link

Thanks!!

@NandhaHyuga
Copy link

nandri nanba..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment