SQL Commands
SQL Commands
SQL Commands
This file contains all the commands discussed in the SQL course.
USE imdb;
SHOW TABLES;
DESCRIBE movies;
SELECT Statement:
Questions: Select all columns from movies:
SELECT * FROM movies;
LIMIT
Questions: Show name and rankscore of first 20 rows from movies table.
SELECT name, rankscore
FROM movies LIMIT 20;
Questions: Show name and rankscore of 20 rows from movies table skip first 20.
SELECT name,rankscore
FROM movies LIMIT 20 OFFSET 40;
ORDER BY
Question: list recent movies first
SELECT name,rankscore, year
FROM movies
ORDER BY year
DESC LIMIT 10;
# default: ASC
SELECT name,rankscore, year FROM movies ORDER BY year LIMIT 10;
1
SQL PRIME COURSE COMMANDS
DISTINCT
Question: Return a list of all genres.
SELECT DISTINCT genre
FROM movies_genres;
Question: list all movies with rankscore>9 display first 20 rows by top rankscore first.
SELECT name, year,rankscore
FROM movies
WHERE rankscore>9
ORDER BY rankscore DESC
LIMIT 20;
Question: List all movies where the movie genre is not Horror.
SELECT * FROM movies_genres
WHERE genre <> ‘Horror’;
2
SQL PRIME COURSE COMMANDS
Question: List all movies where the rankscore of the movie is NULL and print the name, year, and rankscore of the movie.
Question: List all movies where rankscore of the movie is NULL and print the name, year, and rankscore of the movie display first 2o
movies.
Question: List all movies where rankscore of the movie is not NULL and print the name, year, and rankscore of the movie display first
movies.
SELECT name, year,rankscore
FROM movies
WHERE rankscore IS NOT NULL
LIMIT 20;
LOGICAL OPERATORS:
AND, OR, NOT, ALL, ANY, BETWEEN, EXISTS, IN, LIKE, SOME
Question: Print the name, year, and rankscore of the movie which was released after the year 2000 and rankscore of the movie is
greater than 9
SELECT name, year,rankscore
FROM movies
WHERE rankscore>9 AND year>2000;
Question: Display first 20 movies which released after 2000. Print the name of the movie, the year it was released, and the rankscore
the movie.
SELECT name, year,rankscore
FROM movies
WHERE NOT year<=2000 LIMIT 20;
3
SQL PRIME COURSE COMMANDS
Questions: Display movies having rankscore more than 9, and the movie should be released after 2007.
SELECT name, year, rankscore
FROM movies
WHERE rankscore>9 OR year>2007;
Question: Display the movies which were released between the years 1999 and 2000. Both years should be inclusive.
SELECT name, year,rankscore
FROM movies
WHERE year BETWEEN 1999 AND 2000;
# This will throw an error
SELECT name, year,rankscore
FROM movies
WHERE year BETWEEN 2000 AND 1999;
Because low value cannot be higher than the high value.
Question: Display the first name and last name of the actors where the first name of the actor ends with ‘es’.
SELECT first_name, last_name
FROM actors
WHERE first_name LIKE ‘%es’;
Question: Display the first name and last name of the actors where the first name of the actor contains with ‘es’.
SELECT first_name, last_name
FROM actors
WHERE first_name LIKE '%es%';
4
SQL PRIME COURSE COMMANDS
If we want to match % or _, we should use the backslash as the escape character: \% and \ _
SELECT first_name, last_name
FROM actors
WHERE first_name LIKE ‘L%’ AND first_name NOT LIKE ‘Li%’;
Window Functions:
Question: How many movies were released after the year 2000?
SELECT COUNT(*)
FROM movies
WHERE year>2000;
Question: Count the year present in the database, excluding the null values.
# Use of ROW_NUMBER
5
SQL PRIME COURSE COMMANDS
SELECT id, name, rankscore,
ROW_NUMBER() OVER (ORDER BY rankscore DESC) as row_number
FROM movies;
# USE of PARTITION on year and ROW number: As soon as the year changes it will reset the row number.
# if grouping columns contain NULL values, all null values are grouped together.
6
SQL PRIME COURSE COMMANDS
Having
Question: Print all the years where the number of movies released in the year is greater than 1000.
Question: Print year of movies where in each year there are 20 movies which have rankscore greater than 9.
JOIN’s
Question: For each movie print name of the movie and the genre of the movie.
# Left Join
# 3 - way join
#Practical note about joins: Joins can be expensive computationally when we have large tables.
7
SQL PRIME COURSE COMMANDS
Question: Return the Name and Last name of all the actors who have worked in the movie ‘officer 444’.
SubQueries
Question: Select all the movies whose rankscore is same as the maximum rankscore of the movie in the database.
DML: Insert
DML: Update
8
SQL PRIME COURSE COMMANDS
DML: Delete
CREATE TABLE language (id INT PRIMARY, lang VARCHAR(50) NOT NULL);