Introduction
The SELECT statement in SQL is used to retrieve data from a database. In this exercise, we explore different ways of querying the DVD Rental database, such as sorting, filtering, renaming columns, and finding unique values.
These queries demonstrate essential SQL concepts like:
•Column selection
•Aliasing (AS)
•Sorting (ORDER BY)
•Removing duplicates (DISTINCT)
•Aggregation (MIN)
•Limiting results (LIMIT)
Queries with Code & Explanation
- Film titles and rental rates
SELECT title AS "Movie Title",rental_rate AS "Rate"
FROM film;
Retrieves movie titles and rental rates with user-friendly column names.
- Customer names and email
SELECT first_name AS "First Name",last_name AS "Last Name",email
FROM customer;
Displays customer details with renamed columns.
- Films sorted by rental rate
SELECT title, rental_rate
FROM film
ORDER BY rental_rate DESC, title ASC;
Sorts films by highest rental rate, then alphabetically.
- Actor names sorted
SELECT first_name, last_name
FROM actor
ORDER BY last_name ASC, first_name ASC;
Lists actors in proper alphabetical order.
- Unique replacement cost
SELECT DISTINCT replacement_cost
FROM film;
Removes duplicate replacement cost values.
- Film title and duration
SELECT title,length AS "Duration (min)" FROM film;
Shows movie duration with a clear column name.
- Customer active status
SELECT first_name,last_name,
active AS "Is Active"
FROM customer;
Displays whether customers are active.
- Film categories
SELECT name
FROM category
ORDER BY name ASC;
Lists categories alphabetically.
- Films by length
SELECT title, length
FROM film ORDER BY length DESC;
Shows longest films first.
- Actors sorted by first name
SELECT first_name, last_name FROM actor
ORDER BY first_name DESC;
Sorts actors in reverse alphabetical order by first name.
- Unique ratings
SELECT DISTINCT rating
FROM film;
Displays all available movie ratings.
- Unique rental durations
SELECT DISTINCT rental_duration
FROM film;
Shows different rental duration values.
- Customer ID with active status
SELECT DISTINCT customer_id,active
FROM customer
ORDER BY customer_id;
Displays unique customer IDs with their active status.
- Earliest rental date per customer
SELECT customer_id,
MIN(rental_date) AS rental_date
FROM rental
GROUP BY customer_id
ORDER BY customer_id;
Finds each customer’s first rental.
- 10 shortest films
SELECT title, length
FROM film
ORDER BY length ASC
LIMIT 10;
Lists the shortest movies.
- Top 5 customers
SELECT title, length
FROM film
ORDER BY length ASC
LIMIT 10;
Retrieves customers with highest IDs.
- Unique store IDs
SELECT DISTINCT store_id
FROM inventory;
Shows all store IDs.
- Replacement cost sorted
SELECT DISTINCT replacement_cost
FROM film
ORDER BY replacement_cost ASC;
Displays sorted unique replacement costs.
- First rental per 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;
Finds earliest rental date for each store.
- Unique film ratings sorted
SELECT DISTINCT rating
FROM film
ORDER BY rating ASC;
Displays ratings alphabetically.
- Films by rating and length
SELECT title, rating, length
FROM film
ORDER BY rating ASC, length DESC;
Sorts by rating, then longest films first.
- Actor sorting variation
SELECT first_name, last_name
FROM actor
ORDER BY last_name ASC, first_name DESC;
Sorts actors with mixed order conditions.
- Films by cost and rate
SELECT title, replacement_cost, rental_rate
FROM film
ORDER BY replacement_cost ASC, rental_rate DESC;
Sorts by cost and rental rate.
- Customer sorting variation
SELECT first_name, last_name
FROM customer
ORDER BY last_name ASC, first_name DESC;
Orders customers by last name and reverse first name.
- Rentals sorted
SELECT *
FROM rental
ORDER BY customer_id ASC, rental_date DESC;
Shows all rentals sorted by customer and latest date.
- Films by rental duration
SELECT title, rental_duration
FROM film
ORDER BY rental_duration ASC, title DESC;
Sorts films by duration and reverse title order.
Conclusion
These queries demonstrate how SQL can efficiently:
•Retrieve specific data
•Sort and organize results
•Remove duplicates
•Perform basic analysis
Top comments (0)