1.
SELECT title, special_features
FROM film
WHERE special_features IS NULL;
This question is asking us to find movies where no special feature is given at all.
So instead of checking for empty text, we use IS NULL because NULL means “no value stored”.
2.
SELECT title, rental_duration
FROM film
WHERE rental_duration > 7;
Here we only want movies whose rental duration is greater than 7 days.
So we simply filter the rows using the > operator.
3.
SELECT title, rental_rate, replacement_cost
FROM film
WHERE rental_rate = 4.99
AND replacement_cost > 20;
This question has two conditions, and both must be true at the same time.
That is why we use AND to combine rental rate and replacement cost.
4.
SELECT title, rental_rate, rating
FROM film
WHERE rental_rate = 0.99
OR rating = 'PG-13';
Here the movie can satisfy either one condition or the other.
So we use OR because even if one part matches, the row should come.
5.
SELECT title
FROM film
ORDER BY title ASC
LIMIT 5;
First, the movies must be arranged in alphabetical order by title.
After sorting, LIMIT 5 gives only the first 5 rows.
6.
SELECT title, replacement_cost
FROM film
ORDER BY replacement_cost DESC
OFFSET 10
LIMIT 3;
This question says to skip the first 10 rows, then take the next 3.
So we sort first, then use OFFSET 10 and LIMIT 3.
7.
SELECT title, rating
FROM film
WHERE rating IN ('G', 'PG', 'PG-13');
Instead of writing many OR conditions, we can use IN.
It makes the query shorter and easier to read when checking multiple values.
8.
SELECT title, rental_rate
FROM film
WHERE rental_rate BETWEEN 2 AND 4;
The word “between” clearly tells us to use the BETWEEN operator.
This checks whether the rental rate is inside the range from 2 to 4.
9.
SELECT title
FROM film
WHERE title LIKE 'The%';
If the title starts with “The”, then “The” must come at the beginning.
So we use LIKE 'The%', where % means anything can come after it.
10.
SELECT title, rental_rate, rating
FROM film
WHERE rental_rate IN (2.99, 4.99)
AND rating = 'R'
AND title ILIKE '%Love%'
LIMIT 10;
This question combines many filters together, so all of them must match.
ILIKE is useful here because it checks for the word “Love” without worrying about uppercase or lowercase.
11.
SELECT title
FROM film
WHERE title LIKE '%\%%' ESCAPE '\';
Normally % is treated as a wildcard in LIKE, not as a real symbol.
So we use ESCAPE '\' to tell SQL that here % should be treated as an actual character.
12.
SELECT title
FROM film
WHERE title LIKE '%_%' ESCAPE '\';
The underscore _ is also a wildcard in LIKE, for one character.
So we escape it to search for a real underscore symbol inside the title.
13.
SELECT title
FROM film
WHERE (title LIKE 'A%s' OR title LIKE 'B%s');
The title must begin with either A or B, and it must end with s.
So we write two patterns and join them using OR.
14.
SELECT title
FROM film
WHERE title ILIKE '%Man%'
OR title ILIKE '%Men%'
OR title ILIKE '%Woman%';
This question wants titles containing any one of these words.
So we check each word separately and connect them using OR.
15.
SELECT title
FROM film
WHERE title ~ '[0-9]';
Here we need titles that contain numbers anywhere inside them.
The regex [0-9] means “any digit from 0 to 9”.
16.
SELECT title
FROM film
WHERE title LIKE '%\%' ESCAPE '\';
Since backslash is a special escape character, searching for it is a little tricky.
So we escape it properly to make SQL look for a real \ inside the title.
17.
SELECT title
FROM film
WHERE title ILIKE '%Love%'
OR title ILIKE '%Hate%';
This question asks for movies whose titles contain either “Love” or “Hate”.
So we search for both words and use OR because either one is enough.
18.
SELECT title
FROM film
WHERE title ILIKE '%er'
OR title ILIKE '%or'
OR title ILIKE '%ar'
LIMIT 5;
The question is asking for titles that end with specific letter combinations.
So we use patterns ending in er, or, or ar, and then take only the first 5 rows.
Top comments (0)