Back to notes
What is the query to limit the result to the first 20 rows in the 'movies' table?
Press to flip
SELECT * FROM movies LIMIT 20;
Formulate a query to get the earliest and most recent years of movie releases.
SELECT MIN(year), MAX(year) FROM movies;
How do you count the number of rows in the 'movies' table?
SELECT COUNT(*) FROM movies;
How would you add a foreign key constraint in the 'orders' table to reference the 'user_id' from the 'users' table?
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users (id);
Write a query to select only the 'name' and 'year' columns from the 'movies' table.
SELECT name, year FROM movies;
Describe the command to delete a particular row with id 412321 from the movies table.
DELETE FROM movies WHERE id = 412321;
How can you list all tables in the current database?
SHOW TABLES;
How do you fetch all unique genres from the 'movie_genres' table?
SELECT DISTINCT genre FROM movie_genres;
How do you include all movies in a LEFT JOIN between the 'movies' and 'movie_genres' tables?
SELECT m.name, g.genre FROM movies m LEFT JOIN movie_genres g ON m.id = g.movie_id;
How would you fetch all columns from a table named 'movies'?
SELECT * FROM movies;
What is the syntax for an INNER JOIN between 'movies' and 'movie_genres' tables?
SELECT m.name, g.genre FROM movies m INNER JOIN movie_genres g ON m.id = g.movie_id;
Write a SQL statement to ensure that the 'name' column in the 'users' table cannot have null values.
ALTER TABLE users MODIFY COLUMN name VARCHAR(100) NOT NULL;
What is the syntax to update the rank_score to 9 for a movie with id 412321?
UPDATE movies SET rank_score = 9 WHERE id = 412321;
Write a query to find the movies whose name starts with 'T'.
SELECT * FROM movies WHERE name LIKE 'T%';
Write a query to get the top 10 most recent movies.
SELECT * FROM movies ORDER BY year DESC LIMIT 10;
Write a query to get the top 10 oldest movies.
SELECT * FROM movies ORDER BY year ASC LIMIT 10;
How do you combine conditions in a WHERE clause to fetch movies with a rank_score greater than 9 and released after the year 2000?
SELECT * FROM movies WHERE rank_score > 9 AND year > 2000;
Write a SQL query to group movies by year and display the count of movies released each year.
SELECT year, COUNT(*) FROM movies GROUP BY year;
What command is used to switch to a different database?
USE [database_name];
What SQL command is used to describe the schema of a particular table?
DESCRIBE [table_name];
How do you start the results at the 41st row and limit to 20 rows in a SELECT query?
SELECT * FROM movies OFFSET 40 LIMIT 20;
How do you insert a single row with the id of 1, name 'Thor', and year 2011 into the 'movies' table?
INSERT INTO movies (id, name, year) VALUES (1, 'Thor', 2011);
What SQL query would you use to fetch movies with a rank_score greater than 9?
SELECT * FROM movies WHERE rank_score > 9;
Formulate a query to add a unique constraint to the 'email' column in the 'users' table.
ALTER TABLE users ADD CONSTRAINT UNIQUE (email);
What would be a subquery to fetch actors in a specific movie named 'Schindler's List'?
SELECT * FROM actors WHERE id IN (SELECT actor_id FROM roles WHERE movie_id IN (SELECT id FROM movies WHERE name = 'Schindler's List'));
How do you create a table named 'movies' with columns id (primary key), name, and year?
CREATE TABLE movies (id INT PRIMARY KEY, name VARCHAR(100), year INT);
Previous
Next