Professional Documents
Culture Documents
AGGREGATE, GROUP BY, HAVING, JOINS Tutorial
AGGREGATE, GROUP BY, HAVING, JOINS Tutorial
--what is average rental duration for the films in the DVD rental store?
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
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
GROUP BY customer_id
FROM payment;
--INNER JOIN only return the values that matches in two tables
--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
--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
--it returns the full values of two tables even if there is no matches
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
--it returns all values on the left table with the matching values on the right table
ON inventory.film_id = film.film_id;
--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)
ON address.address_id = customer.address_id
--Provide a list of payment IDs and customers first name and last name
ON customer.customer_id = payment.customer_id
WHERE payment_id IN(17504, 17646,17792, 18119);