0% found this document useful (0 votes)
7 views

SQL Commands

This document contains SQL commands and examples used in an SQL primer course. It covers basic queries using SELECT, WHERE, ORDER BY, LIMIT and DISTINCT clauses. It also covers aggregate functions, joins, subqueries, DML for INSERT, UPDATE, DELETE, and DDL for CREATE, ALTER, DROP TABLE and TRUNCATE TABLE statements. The examples select, filter, sort and limit data from movie databases to demonstrate how to work with different SQL clauses and statements.

Uploaded by

as.one.1021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Commands

This document contains SQL commands and examples used in an SQL primer course. It covers basic queries using SELECT, WHERE, ORDER BY, LIMIT and DISTINCT clauses. It also covers aggregate functions, joins, subqueries, DML for INSERT, UPDATE, DELETE, and DDL for CREATE, ALTER, DROP TABLE and TRUNCATE TABLE statements. The examples select, filter, sort and limit data from movie databases to demonstrate how to work with different SQL clauses and statements.

Uploaded by

as.one.1021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL PRIME COURSE 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;

Questions: Select column name & year from movies:


SELECT name, year
FROM movies;

Questions: Select column rankscore and name from movies:


SELECT rankscore, name
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;

SELECT DISTINCT first_name, last_name


FROM directors;
WHERE:

Question: list all movies with rankscore>9


SELECT name, year,rankscore
FROM movies
WHERE rankscore>9;

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;

# Condition’s outputs: TRUE, FALSE, NULL


# Comparison Operators: =, <> or !=, <, <=, >, >=

Question: List all movies where movie genre is Comedy.


SELECT * FROM movies_genres
WHERE genre = ‘Comedy’;

Question: List all movies where the movie genre is not Horror.
SELECT * FROM movies_genres
WHERE genre <> ‘Horror’;

NULL => does not-exist/unknown/missing


# "=" does not work with NULL, will give you an empty result-set.

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.

SELECT name, year,rankscore


FROM movies
WHERE rankscore = NULL;

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.

SELECT name, year,rankscore


FROM movies
WHERE rankscore IS NULL
LIMIT 20;

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.

This is the same as genre=‘Comedy’ OR genre=‘Horror’


SELECT director_id, genre
FROM directors_genres
WHERE genre IN (‘Comedy’,’Horror’);

% wildcard character to imply zero or more characters


SELECT name, year,rankscore
FROM movies
WHERE name LIKE ’Tis%’;

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%';

'_' implies exactly one character.


SELECT first_name, last_name
FROM actors
WHERE first_name LIKE 'Agn_s';

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:

Aggregate window functions (Count, MIN, MAX, AVG, SUM)

Question: Select the minimum value of a year movie year released.

SELECT MIN(year) FROM movies;

Question: Select the maximum value of a year movie year released.

SELECT MAX(year) FROM movies;

Question: Return the total number of movies in the dataset.


SELECT COUNT(*) FROM movies;

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.

SELECT COUNT(year) FROM movies;

Analytical window functions (ROW_NUMBER, RANK & DENSE RANK)

Question: Select movie with highest rankscore

SELECT id, name, rankscore


FROM movies ORDER BY rankscore DESC;

# 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.

SELECT id, name, year, rankscore,


ROW_NUMBER() OVER
(PARTITION BY year ORDER BY rankscore DESC) as `row number`
FROM movies;

# RANK & DENSE RANK

SELECT id, name, rankscore,


RANK() OVER (ORDER BY rankscore DESC) as `rank_number`,
DENSE_RANK() OVER (ORDER BY rankscore DESC) as `dense_rank_number` FROM movies;

Group By & Having Clause

Question: Find the number of movies released per year.

SELECT year, COUNT(year)


FROM movies
GROUP BY year;

# Order by the results with respect to the year in ascending order

SELECT year, COUNT(year)


FROM movies
GROUP BY year ORDER BY year;

# year_count is an alias, often used with COUNT, MIN, MAX or SUM.

SELECT year, COUNT(year) year_count


FROM movies
GROUP BY year ORDER BY year_count;

# 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.

SELECT year, COUNT(year) year_count


FROM movies
GROUP BY year HAVING year_count>1000;

# having clause without using the group by

SELECT name, year


FROM movies
HAVING year>2000;

Question: Print year of movies where in each year there are 20 movies which have rankscore greater than 9.

SELECT year, COUNT(year) year_count


FROM movies
WHERE rankscore>9 GROUP BY year HAVING year_count>20;

JOIN’s

Question: For each movie print name of the movie and the genre of the movie.

SELECT m.name, g.genre


FROM movies m JOIN movies_genres g ON m.id=g.movie_id
LIMIT 20;

# Left Join

SELECT m.name, g.genre


FROM movies m LEFT JOIN movies_genres g ON m.id=g.movie_id
LIMIT 20;

# 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’.

SELECT a.first_name, a.last_name


FROM actors a
JOIN roles r ON a.id=r.actor_id
JOIN movies m on m.id=r.movie_id AND m.name=‘Officer 444’;

SubQueries

Question: List all the actors in the movie Schindler’s List.

SELECT first_name, last_name


FROM actors
WHERE id IN
( SELECT actor_id from roles WHERE movie_id IN
( SELECT id FROM movies where name=“Schindler’s List”));

Question: Select all the movies whose rankscore is same as the maximum rankscore of the movie in the database.

SELECT * FROM movies


WHERE rankscore >= ALL
(SELECT MAX(rankscore) from movies);

DML: Insert

# insert single value


INSERT INTO movies(id, name, year, rankscore) VALUES (412321, ‘Tho’’, 2011, 7);

#insert multiple values


INSERT INTO movies(id, name, year, rankscore) VALUES (412321, ‘Thor’, 2011, 7), (412322, ‘Iron Man’, 2008, 7.9), (412323, ‘Iron Man 2’, 2010,

DML: Update

Question: Update the rankscore of a movie to value 9.

UPDATE movies SET rankscore=9 where id=412321;

8
SQL PRIME COURSE COMMANDS
DML: Delete

Question: Delete a movie from the database.

DELETE FROM movies WHERE id=412321;

DDL: Create Table

CREATE TABLE language (id INT PRIMARY, lang VARCHAR(50) NOT NULL);

DDL: Alter, Modify, DROP

ALTER TABLE language ADD country VARCHAR(50);

ALTER TABLE language MODIFY country VARCHAR(60);

ALTER TABLE language DROP country;

DDL: DROP TABLE, TRUNCATE, DELETE

DROP TABLE <tablename>;

DROP TABLE TableName IF EXISTS;

TRUNCATE TABLE TableName;

You might also like