You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT*FROM store
JOIN address ONstore.address_id=address.address_id;
SELECT*FROM store
RIGHT JOIN address ONstore.address_id=address.address_id;
SELECT*FROM store
LEFT JOIN address ONstore.address_id=address.address_id;
JOIN and GROUP BY Example
SELECTcustomer.customer_id,
customer.first_name,
customer.last_name,
COUNT(rental_id)
FROM customer
LEFT JOIN rental ONrental.customer_id=customer.customer_idGROUP BYcustomer.customer_id;
Multiple JOINS in One Query
SELECTc.customer_id,
c.first_name,
c.last_name,
store.store_id,
COUNT(rental_id) rentals_checked_out,
address.addressas store_adress
FROM customer c
LEFT JOIN rental ONrental.customer_id=c.customer_idLEFT JOIN store ONstore.store_id=c.store_idLEFT JOIN address ONaddress.address_id=store.address_idGROUP BYc.customer_id, address.address;
Filtering Aggregated Data
SELECT title, SUM(amount) sales, COUNT(*) rentals
FROM rental
JOIN payment ONpayment.rental_id=rental.rental_idJOIN inventory ONinventory.inventory_id=rental.inventory_idJOIN film ONfilm.film_id=inventory.film_idGROUP BY title
HAVING sales >200ORDER BY sales DESC;
One-to-One Relationship
user table has only one associated record into profile table
SELECT*FROM user
JOIN profile ONuser.id=profile.user_id;
SELECTusers.id, users.name, COUNT(*) post_read
FROM users
LEFT JOIN post_reads
ONpost_reads.post_idIN (SELECT id
FROM posts
WHERE user_id =users.id)
GROUP BYusers.idORDER BY posts_read DESCLIMIT10;
Determine the Average Rentals Per Day
SELECT ROUND(AVG(total_rentals)) average_rent
FROM (SELECTdate(rental_date) day, count(*) total_rentals
FROM rental
GROUP BY dAY
ORDER BY DAY DESC) rentals;