DB-TASK-002

Let me explain it with some "DVD Rental DataBase" qns

QN: 1

Get all movies with rental rate greater than 3.

ANS: SELECT * FROM film WHERE rental_rate > 3;

since, it is to select all columns from film table.. and condition is rental_rate greater than 3.

QN: 2

Movies with rental rate > 3 and replacement cost < 20.

ANS: SELECT * FROM film WHERE rental_rate > 3 AND replacement_cost < 20;

since, two conditions are given.. so we use AND to combine both.

QN: 3

Movies rated PG or rental rate 0.99.

ANS: SELECT * FROM film WHERE rating = 'PG' OR rental_rate = 0.99;

since, either one condition can be true.. so we use OR.

QN: 4

First 10 movies sorted by rental rate (highest first).

ANS: SELECT * FROM film ORDER BY rental_rate DESC LIMIT 10;

since, we sort rental_rate in descending.. and take first 10.

QN: 5

Skip first 5 and get next 3 (ascending).

ANS: SELECT * FROM film ORDER BY rental_rate ASC OFFSET 5 LIMIT 3;

since, OFFSET skips first 5 rows.. then LIMIT gives next 3.

QN: 6

Skip first 5 and get next 3 (ascending).

ANS: SELECT * FROM film ORDER BY rental_rate ASC OFFSET 5 LIMIT 3;

since, same question so same query.

QN: 7

Movies with rental duration between 3 and 7.

ANS: SELECT * FROM film WHERE rental_duration BETWEEN 3 AND 7;

since, BETWEEN is used for range values.

QN: 8

Title starts with A and ends with e.

ANS: SELECT * FROM film WHERE title LIKE 'A%e';

since, A% means starts with A and %e means ends with e.

QN: 9

Customers without email.

ANS: SELECT * FROM customer WHERE email IS NULL;

since, NULL is checked using IS NULL.

QN: 10

Movies in 2006, rental rate 2.99 or 3.99, title starts with S.

ANS:
SELECT title, rental_rate, release_year
FROM film
WHERE release_year = 2006
AND (rental_rate = 2.99 OR rental_rate = 3.99)
AND title LIKE 'S%'
LIMIT 5;

since, multiple conditions are combined.. and we limit to top 5.

QN: 11

Skip 20 customers and show next 10 sorted by last name.

ANS: SELECT * FROM customer ORDER BY last_name ASC OFFSET 20 LIMIT 10;

since, sorted alphabetically.. then skip 20 and take next 10.

QN: 12

Top 5 movies with highest replacement cost, skip most expensive one.

ANS: SELECT * FROM film ORDER BY replacement_cost DESC OFFSET 1 LIMIT 5;

since, highest is skipped using OFFSET 1.. then next 5 are taken.

QN: 13

Rentals between two dates.

ANS: SELECT * FROM rental WHERE rental_date BETWEEN '2005-05-01' AND '2005-06-01';

since, BETWEEN is used for date range.

QN: 14

Actors with "man" in last name.

ANS: SELECT * FROM actor WHERE last_name LIKE '%man%';

since, %man% means anywhere in the name.

QN: 15

Movies with no special features.

ANS: SELECT * FROM film WHERE special_features IS NULL;

since, NULL values are checked using IS NULL.

QN: 16

Movies with rental duration more than 7.

ANS: SELECT * FROM film WHERE rental_duration > 7;

since, condition is greater than 7.

QN: 17

First 10 movies with rental rate 2.99 or 4.99, rating R, title contains L.

ANS:
SELECT * FROM film
WHERE (rental_rate = 2.99 OR rental_rate = 4.99)
AND rating = 'R'
AND title LIKE '%L%'
LIMIT 10;

since, multiple conditions and title contains L.

QN: 18

Title starts with A or B and ends with s.

ANS: SELECT * FROM film WHERE (title LIKE 'A%s' OR title LIKE 'B%s');

since, starts with A or B.. ends with s.

QN: 19

Title contains Man, Men or Woman.

ANS:
SELECT * FROM film
WHERE title LIKE '%Man%'
OR title LIKE '%Men%'
OR title LIKE '%Woman%';

since, checking multiple words using LIKE.

Comments

Popular posts from this blog

THE DELIVERY MAN

EC2 LAUNCHING

SORT A LINKED LIST USING MERGE SORT