JOINS Flashcards
SELECT customer_id, SUM(amount) AS total_spent_by_customer
FROM payment
GROUP BY customer_id
HAVING SUM(amount) > 100;
creating alias of total_spent_by_customer, cannot use alias name in query-will throw an error
SELECT * FROM payment FULL OUTER JOIN customer ON customer.customer_id = payment.customer_id WHERE payment.payment_id IS NULL OR customer.customer_id IS NULL
produces opposite of an inner join
SELECT COUNT(inventory.film_id) AS number_of_copies, store_id, title FROM inventory
FULL JOIN film
ON film.film_id = inventory.film_id
GROUP BY inventory.film_id, store_id, title
ORDER BY title
provides a list of number of copies of movies by store
SELECT COUNT(inventory.film_id) AS number_of_copies, store_id, title FROM inventory
FULL JOIN film
ON film.film_id = inventory.film_id
WHERE inventory_id IS null
GROUP BY inventory.film_id, store_id, title
ORDER BY title
provides list of movies that no stores have in possession
SELECT film.film_id, title, inventory_id FROM film LEFT JOIN inventory ON inventory.film_id = film.film_id WHERE inventory.film_id IS null
left outer join
SELECT COUNT(amount) AS num_transactions FROM payment;
provides alias for COUNT(amount) value
SELECT title, first_name, last_name FROM film
INNER JOIN film_actor
ON film_actor.film_id = film.film_id
INNER JOIN actor
ON film_actor.actor_id = actor.actor_id
WHERE first_name = ‘Nick’ AND last_name = ‘Wahlberg’;
used to connect three tables together , you can do multiple inner joins.