Forem

Sandhya Steffy M
Sandhya Steffy M

Posted on

DB-TASK-001

  1. Retrieving Data with Column Aliases Sometimes, column names in tables are not user-friendly. So, we can rename them using aliases.

SELECT title AS "Movie Title", rental_rate AS "Rate"
FROM film;

  1. Displaying Customer Details We can also rename customer details to make them clear.

SELECT first_name AS "First Name", last_name AS "Last Name", email
FROM customer;

  1. Sorting Data (ORDER BY) Sorting helps us organize data.

SELECT title, rental_rate
FROM film
ORDER BY rental_rate DESC, title ASC;

  1. Sorting Actor Names
    SELECT first_name, last_name
    FROM actor
    ORDER BY last_name, first_name;
    This sorts actors by last name first.

  2. Using DISTINCT (Unique Values)
    To avoid duplicates, we use DISTINCT.

SELECT DISTINCT replacement_cost
FROM film;
This shows only unique values.

  1. Showing Film Duration
    SELECT title, length AS "Duration (min)"
    FROM film;
    Here, length is renamed to make it clearer.

  2. Customer Active Status
    SELECT first_name, last_name, active AS "Is Active"
    FROM customer;
    This shows whether a customer is active or not.

  3. Film Categories Sorted
    SELECT name
    FROM category
    ORDER BY name;
    This displays categories alphabetically.

  4. Sorting Films by Length
    SELECT title, length
    FROM film
    ORDER BY length DESC;
    This shows longest movies first.

  5. Sorting Actors in Reverse Order
    SELECT first_name, last_name
    FROM actor
    ORDER BY first_name DESC;
    11 & 12. Unique Ratings and Rental Durations
    SELECT DISTINCT rating FROM film;
    SELECT DISTINCT rental_duration FROM film;

  6. Unique Customer Based on Status
    SELECT DISTINCT ON (active) customer_id, active
    FROM customer
    ORDER BY active, customer_id;

  7. 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;

  8. Shortest Films
    SELECT title, length
    FROM film
    ORDER BY length ASC
    LIMIT 10;

  9. Top 5 Customers
    SELECT first_name, last_name
    FROM customer
    ORDER BY customer_id DESC
    LIMIT 5;

  10. Unique Store IDs
    SELECT DISTINCT store_id
    FROM inventory;

  11. Sorted Replacement Cost
    SELECT DISTINCT replacement_cost
    FROM film
    ORDER BY replacement_cost;

  12. First Rental Date Per Store
    SELECT store_id, MIN(rental_date)
    FROM rental
    GROUP BY store_id
    ORDER BY store_id;

  13. Unique Ratings Sorted
    SELECT DISTINCT rating
    FROM film
    ORDER BY rating;
    21.

SELECT title, rating, length
FROM film
ORDER BY rating ASC, length DESC;
22.

SELECT first_name, last_name
FROM actor
ORDER BY last_name ASC, first_name DESC;
23.

SELECT title, replacement_cost, rental_rate
FROM film
ORDER BY replacement_cost ASC, rental_rate DESC;
24.

SELECT first_name, last_name
FROM customer
ORDER BY last_name ASC, first_name DESC;
25.

SELECT *
FROM rental
ORDER BY customer_id ASC, rental_date DESC;
26.

SELECT title, rental_duration
FROM film
ORDER BY rental_duration ASC, title DESC;

Top comments (0)