DEV Community

Mohammed Azim J
Mohammed Azim J

Posted on

DB-TASK-001

I was practicing some SELECT queries in the dvdrental database to understand sorting, alias, unique values and ordering. I tried different queries and below are the ones I used and why I used them.

First I tried to retrieve film titles and rental rate but I renamed the column names so it looks better while reading.

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

I used this because alias makes output more readable instead of database column names.

Then I listed customer names and emails with alias.

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

I used this to see customer contact details in a proper format.

Then I sorted films by rental rate descending and if same rate then by title.

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

I used this to see costly rental movies first and sort same price movies alphabetically.

Then I sorted actor names.

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

I used this to arrange actors properly by last name.

Then I tried unique replacement cost values.

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

I used DISTINCT to remove duplicate values.

Then I listed film title and length.

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

I used alias again to make column readable.

Then I checked customer active status.

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

I used this to see which customers are active.

Then I listed film categories alphabetically.

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

I used ORDER BY to sort alphabetically.

Then I listed films by length descending.

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

I used this to see longest movies first.

Then actors sorted by first name descending.

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

Then I checked unique ratings.

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

Then unique rental duration.

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

Then I retrieved unique customer id by active status.

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

Then earliest rental date per customer.

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

Then 10 shortest films.

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

Then top 5 customers with highest id.

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

Then unique store ids.

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

Then replacement cost sorted.

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

Then first rental date per store.

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

Then ratings sorted alphabetically.

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

Then films sorted by rating and length.

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

Then actors sorted last name ascending and first name descending.

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

Then films sorted 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

Then customers sorted by last and first name.

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

Then rentals sorted by customer and rental date.

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

Then 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

From all these queries I understood mainly SELECT, DISTINCT, ORDER BY, LIMIT and alias. These are very basic but very important queries for fetching and sorting data from database.

Top comments (0)