- 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;
- 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;
- Sorting Data (ORDER BY) Sorting helps us organize data.
SELECT title, rental_rate
FROM film
ORDER BY rental_rate DESC, title ASC;
Sorting Actor Names
SELECT first_name, last_name
FROM actor
ORDER BY last_name, first_name;
This sorts actors by last name first.Using DISTINCT (Unique Values)
To avoid duplicates, we use DISTINCT.
SELECT DISTINCT replacement_cost
FROM film;
This shows only unique values.
Showing Film Duration
SELECT title, length AS "Duration (min)"
FROM film;
Here, length is renamed to make it clearer.Customer Active Status
SELECT first_name, last_name, active AS "Is Active"
FROM customer;
This shows whether a customer is active or not.Film Categories Sorted
SELECT name
FROM category
ORDER BY name;
This displays categories alphabetically.Sorting Films by Length
SELECT title, length
FROM film
ORDER BY length DESC;
This shows longest movies first.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;Unique Customer Based on Status
SELECT DISTINCT ON (active) customer_id, active
FROM customer
ORDER BY active, customer_id;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;Shortest Films
SELECT title, length
FROM film
ORDER BY length ASC
LIMIT 10;Top 5 Customers
SELECT first_name, last_name
FROM customer
ORDER BY customer_id DESC
LIMIT 5;Unique Store IDs
SELECT DISTINCT store_id
FROM inventory;Sorted Replacement Cost
SELECT DISTINCT replacement_cost
FROM film
ORDER BY replacement_cost;First Rental Date Per Store
SELECT store_id, MIN(rental_date)
FROM rental
GROUP BY store_id
ORDER BY store_id;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)