DEV Community

Suruthika
Suruthika

Posted on

CA 31 - Select Queries from DVD Rental database

I retrieved film titles and their rental rates. I used column aliases to make the output more readable. Instead of showing column names like title and rental_rate, I renamed them to "Movie Title" and "Rate".

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

Next, I listed customer names along with their email addresses. I renamed first_name and last_name to "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

I retrieved films sorted by rental rate in descending order. If two films had the same rental rate, I sorted them alphabetically by title.

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

I also sorted actor names by last name first and then by first name.

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

To understand unique values, I used DISTINCT. I listed all unique replacement costs and ratings from the film table.

SELECT DISTINCT replacement_cost
FROM film;

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

I also retrieved film titles along with their length and renamed the length column.

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

Then I checked customer details with their active status.

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

I explored categories and sorted them alphabetically.

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

I listed films by length in descending order.

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

I also sorted actor names in reverse order based on first name.

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

To find unique rental durations, I used DISTINCT again.

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

I retrieved the first customer based on active status.

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

I also retrieved the earliest rental date for each customer.

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

Next, I found the shortest films by length.

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

I retrieved the top 5 customers with the highest IDs.

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

I checked unique store IDs from the inventory table.

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

I also sorted replacement costs in ascending order.

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

Then I found the first rental date for each store.

SELECT i.store_id, MIN(r.rental_date) AS rental_date
FROM rental r
JOIN inventory i ON r.inventory_id = i.inventory_id
GROUP BY i.store_id
ORDER BY i.store_id;
Enter fullscreen mode Exit fullscreen mode

I sorted films by rating and then by length.

SELECT title, rating, length
FROM film
ORDER BY rating ASC, length DESC;
Enter fullscreen mode Exit fullscreen mode
SELECT first_name, last_name
FROM actor
ORDER BY last_name ASC, first_name DESC;
Enter fullscreen mode Exit fullscreen mode

I sorted films by replacement cost and rental rate.

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

I sorted customer names.

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

I sorted rentals.

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

Finally, I listed films sorted by rental duration and title.

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

Top comments (0)