Jun 22, 2024
USE IMDb;
selects the IMDb database.SHOW TABLES;
lists all tables in the current database.DESCRIBE movies;
displays the schema of the movies table.SELECT * FROM movies;
selects all columns from the movies table.SELECT name, year FROM movies;
selects only the name and year columns.SELECT * FROM movies WHERE rank_score > 9;
selects movies with a rank_score greater than 9.SELECT * FROM movies WHERE rank_score > 9 AND year > 2000;
selects movies with a rank_score greater than 9 and released after the year 2000.SELECT * FROM movies LIMIT 20;
limits the result to the first 20 rows.SELECT * FROM movies OFFSET 40 LIMIT 20;
starts results at the 41st row and limits to 20 rows.SELECT * FROM movies ORDER BY year DESC LIMIT 10;
gets the top 10 most recent movies.SELECT * FROM movies ORDER BY year ASC LIMIT 10;
gets the top 10 oldest movies.SELECT DISTINCT genre FROM movie_genres;
lists all unique genres from the movie_genres table.SELECT DISTINCT first_name, last_name FROM directors;
lists unique combinations of first and last names from directors.SELECT * FROM movies WHERE name LIKE 'T%';
selects movies whose names start with 'T'.SELECT * FROM actors WHERE first_name LIKE '%es';
selects actors whose first name ends with 'es'.SELECT * FROM actors WHERE first_name LIKE '%es%';
selects actors whose first name contains 'es'.SELECT MIN(year), MAX(year) FROM movies;
finds the earliest and most recent years of movie releases.SELECT COUNT(*) FROM movies;
counts the total number of rows in the movies table.SELECT year, COUNT(*) FROM movies GROUP BY year;
counts movies released each year.SELECT year, COUNT(*) FROM movies GROUP BY year HAVING COUNT(*) > 1000;
finds years with more than 1000 movies.SELECT year, COUNT(*) FROM movies WHERE rank_score > 9 GROUP BY year HAVING COUNT(*) > 20;
finds years with more than 20 movies that have a rank score greater than 9.SELECT m.name, g.genre FROM movies m INNER JOIN movie_genres g ON m.id = g.movie_id;
joins movies and movie_genres tables on movie ID.SELECT m.name, g.genre FROM movies m LEFT JOIN movie_genres g ON m.id = g.movie_id;
includes all movies, whether they have genres or not.SELECT * FROM t1 INNER JOIN t2 ON t1.common_col = t2.common_col;
joins t1 and t2 tables using common columns.SELECT * FROM actors WHERE id IN (SELECT actor_id FROM roles WHERE movie_id IN (SELECT id FROM movies WHERE name = 'Schindler's List'));
nested query to find actors in a specific movie.SELECT * FROM movies WHERE EXISTS (SELECT 1 FROM roles WHERE movies.id = roles.movie_id AND rank_score = (SELECT MAX(rank_score) FROM movies));
checks if the movie exists with the highest rank score.INSERT INTO movies (id, name, year) VALUES (1, 'Thor', 2011);
INSERT INTO movies (id, name, year) VALUES (1, 'Thor', 2011), (2, 'Avengers', 2012);
INSERT INTO target_table (col1, col2) SELECT col1, col2 FROM source_table WHERE condition;
UPDATE movies SET rank_score = 9 WHERE id = 412321;
DELETE FROM movies WHERE id = 412321;
TRUNCATE TABLE movies;
CREATE TABLE movies (id INT PRIMARY KEY, name VARCHAR(100), year INT);
ALTER TABLE movies ADD COLUMN genre VARCHAR(50);
, ALTER TABLE movies MODIFY COLUMN genre VARCHAR(100);
, ALTER TABLE movies DROP COLUMN genre;
DROP TABLE IF EXISTS movies;
ALTER TABLE users ADD CONSTRAINT UNIQUE (email);
ALTER TABLE users MODIFY COLUMN name VARCHAR(100) NOT NULL;
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users (id);