DEV Community

Cover image for Select Queries from DVD Rental database
Jonah Blessy
Jonah Blessy

Posted on

Select Queries from DVD Rental database

Qn 1: Retrieve film titles and their rental rates. Use column aliases to rename title as "Movie Title" and rental_rate as "Rate".

SELECT title AS "Movie Title", rental_rate AS "Rate" FROM film;
Enter fullscreen mode Exit fullscreen mode

Thought: Just selecting needed columns and renaming them using aliases for clarity. No filtering or sorting needed.


Qn 2: List customer names and their email addresses. Alias first_name and last_name.

SELECT first_name AS "First Name", last_name AS "Last Name", email FROM customer;
Enter fullscreen mode Exit fullscreen mode

Thought: Pick required columns and use aliases for readability. Email doesn’t need alias since name is clear.


Qn 3: Get films sorted by rental rate descending, then by title ascending.

SELECT rental_rate, title FROM film ORDER BY rental_rate DESC, title ASC;
Enter fullscreen mode Exit fullscreen mode

Thought: First priority sorting is rental rate, then resolve ties using title alphabetically.


Qn 4: Retrieve actor names sorted by last name, then first name.

SELECT first_name, last_name FROM actor ORDER BY last_name, first_name;
Enter fullscreen mode Exit fullscreen mode

Thought: Sort hierarchy matters—last name first, then first name for tie-breaking.


Qn 5: List all unique replacement costs.

SELECT DISTINCT replacement_cost FROM film;
Enter fullscreen mode Exit fullscreen mode

Thought: Use DISTINCT to remove duplicates and show only unique values.


Qn 6: List film title and length with alias.

SELECT title, length AS "Duration (min)" FROM film;
Enter fullscreen mode Exit fullscreen mode

Thought: Just renaming a column for better readability, no filtering involved.


Qn 7: Retrieve customer names with active status.

SELECT first_name, last_name, active AS "Is Active" FROM customer;
Enter fullscreen mode Exit fullscreen mode

Thought: Straight selection with alias to make boolean column more understandable.


Qn 8: Retrieve film categories sorted alphabetically.

SELECT name FROM category ORDER BY name;
Enter fullscreen mode Exit fullscreen mode

Thought: Only one column needed, sorted alphabetically using ORDER BY.


Qn 9: List films by length descending.

SELECT title, length FROM film ORDER BY length DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Sort based on numeric column length in descending order.


Qn 10: Retrieve actor names sorted by first name descending.

SELECT first_name FROM actor ORDER BY first_name DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Only one column, so directly apply descending sort.


Qn 11: List all unique ratings.

SELECT DISTINCT rating FROM film;
Enter fullscreen mode Exit fullscreen mode

Thought: Ratings may repeat, so DISTINCT ensures uniqueness.


Qn 12: Find all unique rental durations.

SELECT DISTINCT rental_duration FROM film;
Enter fullscreen mode Exit fullscreen mode

Thought: Same idea as above—remove duplicates using DISTINCT.


Qn 13: Retrieve first unique customer_id based on active status.

SELECT customer_id, active FROM customer ORDER BY customer_id;
Enter fullscreen mode Exit fullscreen mode

Thought: Order by customer_id ensures first record appears at top; uniqueness assumed by PK.


Qn 14: List earliest rental date for each customer.

SELECT customer_id, MIN(rental_date) FROM rental GROUP BY customer_id ORDER BY customer_id;
Enter fullscreen mode Exit fullscreen mode

Thought: Use aggregation MIN to get earliest date, grouped per customer.


Qn 15: List 10 shortest films by length.

SELECT title, length FROM film ORDER BY length ASC LIMIT 10;
Enter fullscreen mode Exit fullscreen mode

Thought: Smallest values come first in ascending order, then limit results.


Qn 16: Get top 5 customers with highest customer_id.

SELECT first_name, last_name FROM customer ORDER BY customer_id DESC LIMIT 5;
Enter fullscreen mode Exit fullscreen mode

Thought: Highest IDs come first using DESC, then limit to top 5.


Qn 17: Retrieve unique store_ids from inventory.

SELECT DISTINCT store_id FROM inventory;
Enter fullscreen mode Exit fullscreen mode

Thought: Use DISTINCT since multiple entries may belong to same store.


Qn 18: Unique replacement_cost sorted ascending.

SELECT DISTINCT replacement_cost FROM film ORDER BY replacement_cost ASC;
Enter fullscreen mode Exit fullscreen mode

Thought: Combine DISTINCT and ORDER BY to get sorted unique values.


Qn 19: First rental date for each store.

SELECT store_id, MIN(rental_date) FROM rental GROUP BY store_id ORDER BY store_id;
Enter fullscreen mode Exit fullscreen mode

Thought: Group by store and use MIN to get earliest rental.


Qn 20: Unique film ratings sorted alphabetically.

SELECT DISTINCT rating FROM film ORDER BY rating;
Enter fullscreen mode Exit fullscreen mode

Thought: DISTINCT removes duplicates, ORDER BY arranges alphabetically.


Qn 21: List films by rating ascending and length descending.

SELECT rating, length FROM film ORDER BY rating ASC, length DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Apply multiple sorting—first by rating, then length for tie-breaking.


Qn 22: Retrieve actor names sorted by last_name ASC and first_name DESC.

SELECT first_name, last_name FROM actor ORDER BY last_name ASC, first_name DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Mixed sorting—primary ascending, secondary descending.


Qn 23: List films ordered by replacement_cost ASC and rental_rate DESC.

SELECT replacement_cost, rental_rate FROM film ORDER BY replacement_cost ASC, rental_rate DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: First sort cheaper films, then within same cost sort by higher rate.


Qn 24: Retrieve customer names sorted by last_name ASC and first_name DESC.

SELECT first_name, last_name FROM customer ORDER BY last_name ASC, first_name DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Same multi-level sorting logic applied on names.


Qn 25: List rentals sorted by customer_id ASC and rental_date DESC.

SELECT * FROM rental ORDER BY customer_id ASC, rental_date DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Group by customer order, then show latest rentals first.


Qn 26: Retrieve films ordered by rental_duration ASC and title DESC.

SELECT title, rental_duration FROM film ORDER BY rental_duration ASC, title DESC;
Enter fullscreen mode Exit fullscreen mode

Thought: Shorter durations first, and for same duration sort titles in reverse order.

Top comments (0)