DEV Community

ARUL SELVI ML
ARUL SELVI ML

Posted on

BASIC SQL QUERIES

Retrieving Film Titles and Rental Rates

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

This query displays film titles and their rental rates with readable column names.


Listing Customer Names and Emails

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

This shows customer names along with their email addresses.


Sorting Films by Rental Rate

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

Films are sorted by rental rate from highest to lowest, and then by title.


Sorting Actor Names

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

Actors are listed in order of last name and then first name.


Getting Unique Replacement Costs

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

This query returns only different replacement cost values.


Displaying Film Duration

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

Shows film titles along with their duration in minutes.


Customer Active Status

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

Displays whether each customer is active or not.


Film Categories in Order

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

Lists all film categories alphabetically.


Films by Length

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

Shows films from longest to shortest.


Actors Sorted by First Name Descending

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

Actors are sorted in reverse alphabetical order by first name.


Unique Film Ratings

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

Displays all different ratings available.


Unique Rental Durations

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

Shows all unique rental durations.


Customer ID and Active Status

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

Lists customer IDs along 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;
Enter fullscreen mode Exit fullscreen mode

Finds the first rental date for each customer.


Ten Shortest Films

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

Displays the ten shortest films.


Top Five Customers by ID

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

Shows the customers with the highest IDs.


Unique Store IDs

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

Lists all store IDs without duplicates.


Sorted Replacement Costs

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

Displays replacement costs in increasing order.


First Rental Date per Store

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

Shows the first rental date for each store.


Unique Ratings in Order

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

Lists all ratings alphabetically.


Films by Rating and Length

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

Sorts films by rating and then by length.


Actor Names with Mixed Sorting

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

Actors are sorted by last name and then reverse order of first name.


Films by Cost and Rate

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

Sorts films by replacement cost and rental rate.


Customer Names Sorted

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

Customers are sorted by last name and reverse first name.


Rentals Sorted by Customer and Date

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

Shows rentals sorted by customer and latest rental first.


Films by Rental Duration

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

Sorts films by rental duration and title.

Top comments (0)