DEV Community

JAYA SRI J
JAYA SRI J

Posted on

select queries

let's see about different queries and how it works
1. Retrieve Film Titles and Rental Rates

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

Explanation
here I ma doing title is renamed as "Movie Name" and rental_rate is renamed as rate
here why I use string----> when you give space without using string it does not exists.
here without AS or with As no problem both is acceptable

2. Retrieve Customer Names and Email
Query

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

Explanation
first_name is renamed to "First Name". and last_name is renamed to "Last Name".
email is left unchanged because it is already clear.

3.Films sorted by rental rate (descending), then title

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

Explanation
DESC is for highest first and ASC is for alphabetical.
Used for ranking + tie-breaking.

4. Actor names sorted by last name, then first name

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

Explanation:
Standard way to sort names .

5. Unique replacement costs

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

Explanation:
Distinct removes duplicates is useful for analysis.

6. Film title and duration

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

Explanation:
Alias makes unit clear.
**

  1. Customer active status** SELECT first_name, last_name, active AS "Is Active" FROM customer;

Explanation:
Boolean values become user-friendly labels.

8. Film categories alphabetically

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

Explanation:
Sorting improves readability.

9. Films sorted by length (descending)

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

Explanation:
Find longest movies quickly.

10. Actor names sorted by first name (descending)

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

explanation:
Reverse sorting for different views.
**

  1. Unique ratings**
SELECT DISTINCT rating FROM film;
Enter fullscreen mode Exit fullscreen mode

explanation:
Know all available categories (PG, R, etc.).

12. Unique rental durations

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

explanation:
Understand available rental plans.

13. First unique customer by active status

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

explanation:
Combining DISTINCT + ORDER BY helps identify unique records in order.
**

  1. Earliest rental date for each customer**
SELECT customer_id, MIN(rental_date) AS first_rental FROM rental GROUP BY customer_id ORDER BY customer_id;
Enter fullscreen mode Exit fullscreen mode

explanation:
GROUP BY groups data MIN() used to finds earliest value

15. 10 shortest films

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

explanation:
LIMIT restricts output of useful for top/bottom lists.

16. Top 5 customers

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

explanation:
It is used to find latest or highest records.
**

  1. Unique store ID**s
SELECT DISTINCT store_id FROM inventory;
Enter fullscreen mode Exit fullscreen mode

explanation:
Identify all store locations.
**

  1. Unique replacement cost (sorted)**
SELECT DISTINCT replacement_cost FROM film ORDER BY replacement_cost ASC;
Enter fullscreen mode Exit fullscreen mode

explanation:
Sorting helps in analysis.

19. First rental date for each store

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

explanation:
Track store activity start.

20. Unique ratings sorted

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

explanation:
Cleaner categorized output.

21. Films by rating and length

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

Enter fullscreen mode Exit fullscreen mode

explanation:
Multi-level sorting.

*22. Actor names sorted *

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

explanation:
Advanced sorting combinations.

23. Films 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

explanation:
Useful for pricing analysis.

24. Customers sorted by name

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

explanation:
Readable listing format.

25. Rentals sorted by customer and date

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

explanation:
See latest activity per customer.

26. Films by rental duration and title

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

explanation:
Helps compare short vs long rental plans.

Top comments (0)