Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

--how many recorded customers does the dvd rental store have?

SELECT COUNT (customer_id) FROM customer;

--what is average rental duration for the films in the DVD rental store?

SELECT ROUND (AVG(rental_duration)) FROM film;

--what is the maximum length of the DVD rental stores film?

SELECT MAX("length") FROM film;

--what is the DVD rental store minimum rental rate?

SELECT MIN(rental_rate) FROM film;

--what is the sum of all payments made in the payment table?

SELECT SUM(amount) FROM payment;

SELECT rating, AVG("length") FROM film

GROUP BY rating; --GROUP BY combined with AVG means it average the length and it group it in
common value of rating

--group them according to rating , and each rating show how many titles are there

SELECT rating, COUNT(*) FROM film

GROUP BY rating;

--*you cannot use WHERE clause if you are using aggregate function, that why you will use is HAVING

--*you can use HAVING after the GROUP BY statement or if you have aggregate function

SELECT customer_id, COUNT(*)FROM payment

GROUP BY customer_id

HAVING COUNT(*) >= 35

ORDER BY customer_id ASC;


--AS function is used to give an alias to your column

--it will only use for the query only

--the AVG(amount) column name become AVG_Amount

--AVG_Amount column name can be use to other query

SELECT AVG(amount) AS AVG_Amount

FROM payment;

--THE INNER JOIN FUNCTION

--INNER JOIN only return the values that matches in two tables

SELECT customer.customer_id AS cc, first_name AS fn, last_name AS lsn FROM customer

--the customer.customer_id this means the customer_id column is from customer table because
customer_id also exist in payment table

--and you want to show the customer_id of customer together with first name and last name which
came from customer table

INNER JOIN payment

--the INNER JOIN means you want to join the table of customer table with payment table

ON customer.customer_id = payment.customer_id;

--ON means you are setting a statement which the customer_id of customer table is equal to
customer_id of payment table

--THE UNION FUNCTION

--it only combines the two tables

--but the tables should have the same number of columns

--columns also should have same data types

--columns in the SELECT statement must also be in order

--FULL OUTER JOIN/FULL JOIN FUNCTION

--it returns the full values of two tables even if there is no matches

SELECT * FROM customer


FULL OUTER JOIN payment

ON payment.customer_id = customer.customer_id

WHERE customer.customer_id = 342; --setting a condition to show customer id of customer which have
id number of 342

--LEFT OUTER JOIN/LEFT JOIN FUNCTION

--it returns all values on the left table with the matching values on the right table

SELECT inventory.film_id, title, inventory_id FROM film

--the left table here is the film table

--the right table here is the inventory table

LEFT OUTER JOIN inventory

ON inventory.film_id = film.film_id;

--RIGHT OUTER JOIN/RIGHT JOIN FUNCTION

--it returns everything from the right table with matching values on the left table

--the rows that doesnt have match values will be null values

--make a syntax where you join the two table (customer and address)

--which shows the district and email of the customer

SELECT district,email FROM customer

INNER JOIN address

ON address.address_id = customer.address_id

WHERE district = 'California';

--Provide a list of payment IDs and customers first name and last name

--that associated with payment IDs 17504, 17646,17792, and 18119

SELECT payment_id,first_name, last_name FROM customer

INNER JOIN payment

ON customer.customer_id = payment.customer_id
WHERE payment_id IN(17504, 17646,17792, 18119);

You might also like