con <- dbConnect(
RMariaDB::MariaDB(),
host = "relational.fit.cvut.cz",
port = 3306,
username = "guest",
password = "relational",
dbname = "sakila"
)
dbListTables(con)
## [1] "actor" "address" "category" "city"
## [5] "country" "customer" "film" "film_actor"
## [9] "film_category" "film_text" "inventory" "language"
## [13] "payment" "rental" "staff" "store"
SQL
1
SELECT customer.first_name, customer.last_name, customer.email, COUNT(*) AS total_rentals
FROM customer
JOIN rental ON customer.customer_id = rental.customer_id
GROUP BY customer.first_name, customer.last_name, customer.email
ORDER BY total_rentals DESC
LIMIT 10;
2
SELECT film.title, film.description
FROM film
LEFT JOIN inventory ON film.film_id = inventory.film_id
LEFT JOIN rental ON inventory.inventory_id = rental.inventory_id
WHERE rental.rental_id IS NULL
GROUP BY film.title, film.description
ORDER BY film.title;
Displaying records 1 - 10
| ACADEMY DINOSAUR |
A Epic Drama of a Feminist And a Mad Scientist who must
Battle a Teacher in The Canadian Rockies |
| ALICE FANTASIA |
A Emotional Drama of a A Shark And a Database
Administrator who must Vanquish a Pioneer in Soviet Georgia |
| APOLLO TEEN |
A Action-Packed Reflection of a Crocodile And a
Explorer who must Find a Sumo Wrestler in An Abandoned Mine Shaft |
| ARGONAUTS TOWN |
A Emotional Epistle of a Forensic Psychologist And a
Butler who must Challenge a Waitress in An Abandoned Mine Shaft |
| ARK RIDGEMONT |
A Beautiful Yarn of a Pioneer And a Monkey who must
Pursue a Explorer in The Sahara Desert |
| ARSENIC INDEPENDENCE |
A Fanciful Documentary of a Mad Cow And a Womanizer who
must Find a Dentist in Berlin |
| BOONDOCK BALLROOM |
A Fateful Panorama of a Crocodile And a Boy who must
Defeat a Monkey in The Gulf of Mexico |
| BUTCH PANTHER |
A Lacklusture Yarn of a Feminist And a Database
Administrator who must Face a Hunter in New Orleans |
| CATCH AMISTAD |
A Boring Reflection of a Lumberjack And a Feminist who
must Discover a Woman in Nigeria |
| CHINATOWN GLADIATOR |
A Brilliant Panorama of a Technical Writer And a
Lumberjack who must Escape a Butler in Ancient India |
3
SELECT category.name AS category_name, AVG(film.length) AS average_length
FROM film
JOIN film_category ON film.film_id = film_category.film_id
JOIN category ON film_category.category_id = category.category_id
GROUP BY category.name
ORDER BY average_length DESC;
Displaying records 1 - 10
| Sports |
128.2027 |
| Games |
127.8361 |
| Foreign |
121.6986 |
| Drama |
120.8387 |
| Comedy |
115.8276 |
| Family |
114.7826 |
| Music |
113.6471 |
| Travel |
113.3158 |
| Horror |
112.4821 |
| Classics |
111.6667 |
4
SELECT actor.first_name, actor.last_name, COUNT(*) AS film_count
FROM actor
JOIN film_actor ON actor.actor_id = film_actor.actor_id
GROUP BY actor.first_name, actor.last_name
ORDER BY film_count DESC
LIMIT 5;
5 records
| SUSAN |
DAVIS |
54 |
| GINA |
DEGENERES |
42 |
| WALTER |
TORN |
41 |
| MARY |
KEITEL |
40 |
| MATTHEW |
CARREY |
39 |
5
SELECT DISTINCT customer.first_name, customer.last_name
FROM customer
JOIN rental ON customer.customer_id = rental.customer_id
JOIN inventory ON rental.inventory_id = inventory.inventory_id
JOIN film ON inventory.film_id = film.film_id
JOIN film_actor ON film.film_id = film_actor.film_id
JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE actor.first_name = 'Johnny' AND actor.last_name = 'Depp';
6
SELECT film.title, SUM(payment.amount) AS total_revenue
FROM film
JOIN inventory ON film.film_id = inventory.film_id
JOIN rental ON inventory.inventory_id = rental.inventory_id
JOIN payment ON rental.rental_id = payment.rental_id
GROUP BY film.title
ORDER BY total_revenue DESC
LIMIT 10;
Displaying records 1 - 10
| TELEGRAPH VOYAGE |
231.73 |
| WIFE TURN |
223.69 |
| ZORRO ARK |
214.69 |
| GOODFELLAS SALUTE |
209.69 |
| SATURDAY LAMBS |
204.72 |
| TITANS JERK |
201.71 |
| TORQUE BOUND |
198.72 |
| HARRY IDAHO |
195.70 |
| INNOCENT USUAL |
191.74 |
| HUSTLER PARTY |
190.78 |
Plotly
1
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2)
p<-ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(text = paste("Clarity:", clarity)), size = 1) +
ggtitle("Diamond Price vs Carat") +
xlab("Carat") +
ylab("Price")
## Warning in geom_point(aes(text = paste("Clarity:", clarity)), size = 1):
## Ignoring unknown aesthetics: text
ggplotly(p, tooltip = "text")
2
p
ggplot2 is highly customizable and it is particularly favored in
academic and research settings where detailed and precise static
visualizations are required for reports or publications. Its extensive
customization options make it ideal for exploratory data analysis, where
the focus is on the accuracy and clarity of the representation rather
than on interactive elements.
Plotly, in contrast, is better in creating interactive and dynamic
visualizations. It allows users to interact with the data through
features like zooming, panning, and dynamic updating. This interactivity
makes Plotly a preferred choice for presentations or web-based reports
where engaging the audience. So when interactions are needed, plotly is
better.
3
library(plotly)
library(gapminder)
data_2007<-subset(gapminder, year == 2007)
p2<-ggplot(data_2007, aes(x = gdpPercap, y = lifeExp, text = country)) +
geom_point(aes(size = pop, color = continent), alpha = 0.7) +
scale_size(range = c(2, 12)) +
ggtitle("Life Expectancy vs GDP Per Capita in 2003") +
xlab("GDP Per Capita") +
ylab("Life Expectancy")
ggplotly(p2, tooltip = "text")
This plot compares GDP per capita with life expectancy for different
countries in 2007. The size of the points represents the population, and
the color represents the continent. The narrative advanced by this
comparison might focus on the relationship between economic development
and health standards across different regions. Higher GDP per capita
often leads to longer life expectancy. This comparison also underscores
global disparities, revealing significant differences in economic and
health indicators across regions.