Functions for Manipulating Data in PostgreSQL
Here you can access the tables used in the course. To access the table, you will need to specify the dvdrentals schema in your queries (e.g., dvdrentals.film for the film table and dvdrentals.country for the country table).
Note: When using sample integrations such as those that contain course data, you have read-only access. You can run queries, but cannot make any changes such as adding, deleting, or modifying the data (e.g., creating tables, views, etc.).
-- Add your own queries here
SELECT *
FROM dvdrentals.film
LIMIT 10Explore Datasets 1
Use the different tables to explore the data and practice your skills!
- Select the
title,release_year, andratingof films in thefilmtable.- Add a
description_shortenedcolumn which contains the first 50 characters of thedescriptioncolumn, ending with "...". - Filter the
filmtable for rows where thespecial_featurescolumn contains "Commentaries".
- Add a
SELECT title, release_year, rating, LEFT(description, 50)||'...' AS description_shortened
FROM dvdrentals.film
WHERE special_features IS NOT NULL
;Explore Datasets 2
- Select the
customer_id,amount, andpayment_datefrom thepaymenttable.
SELECT customer_id, amount, payment_date
FROM dvdrentals.payment
;Explore Datasets 3
Extract date information from the payment_date column, creating new columns for the day, month, quarter, and year of transaction.
SELECT
CAST(SUBSTRING(payment_date :: text, 9, 2) AS integer) AS day,
CAST(SUBSTRING(payment_date :: text, 6, 2) AS integer) AS month,
EXTRACT(quarter FROM TO_DATE(payment_date :: text, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')) AS quarter,
CAST(SUBSTRING(payment_date :: text, 1, 4) AS integer) AS year
FROM
dvdrentals.payment
;Explore Datasets 4
Use the rental table to include a column containing the number of days rented (i.e., time between the rental_date and the return_date).
SELECT
rental_date,
return_date,
return_date - rental_date AS days_rented
FROM dvdrentals.rental
;Explore Datasets 5
- Update the title column so that titles with multiple words are reduced to the first word and the first letter of the second word followed by a period.
- For example:
- "BEACH HEARTBREAKERS" becomes "BEACH H."
- "BEAST HUNCHBACK" becomes "BEAST H."
- Reformat your shortened title to title case (e.g., "BEACH H." becomes "Beach H.").
- For example:
SELECT INITCAP(CONCAT_WS(' ', SUBSTRING(title FROM 1 FOR POSITION(' ' IN title) - 1), SUBSTRING(title FROM POSITION(' ' IN title) + 1 FOR 1) || '.')) AS shortened_title
FROM dvdrentals.film;
;SELECT DISTINCT time.year, time.month, COUNT(*) AS payed_rents
FROM
(
SELECT
CAST(SUBSTRING(payment_date :: text, 9, 2) AS integer) AS day,
CAST(SUBSTRING(payment_date :: text, 6, 2) AS integer) AS month,
CAST(SUBSTRING(payment_date :: text, 1, 4) AS integer) AS year
FROM
dvdrentals.payment) AS time
GROUP BY year, month
ORDER BY year, month
;df5