I love SQL and have noticed that a number of junior devs aren't well versed in it and over-rely on the ORM. This tutorial is my attempt to share the love. It takes about 2 hours.
up a quotation mark, you must close with the same type of quotation mark (ex: "hi", 'hi', but "hi' won't work) If you get stuck and it keeps showing you ...> instead of sqlite> in the prompt, then you have most likely either A. messed up the quotes B. forgotten your semi-colon SQL doesn't care if you return after each line or if you indent. The indentations in these slides are just to show how you would do it if you are writing up SQL in a professional setting.
email ---------- ---------- ---------- ----------------------- 1 Rylee Wynn [email protected] sqlite> SELECT name, species FROM pet LIMIT 1; name species ---------- ---------- Amani rabbit
'2016-05-10 10:00:00'); sqlite> SELECT * FROM pet WHERE name = 'Fluffypants'; id name owner_id species birth_date ---------- ----------- ---------- ---------- ------------------- 195 Fluffypants rabbit 2016-05-10 10:00:00
= 'Fluffypants'; sqlite> SELECT * FROM pet WHERE name = 'Fluffypants'; id name owner_id species birth_date ---------- ----------- ---------- ---------- ------------------- 195 Fluffypants rabbit 2016-05-11 10:00:00
INNER JOIN address ON address.person_id = person.id LEFT JOIN pet ON pet.owner_id = person.id GROUP BY person.id; first_name last_name city NumPets ---------- ---------- ---------- ---------- Rylee Wynn Stamford 2 Korbin Stuart Hialeah 1 Rebecca Reilly Sioux Fall 1 Mariano Dawson Wichita 3 Andres Horne Naperville 0 How many pets does each person have?
ON pet.owner_id = person.id GROUP BY person.id HAVING COUNT(pet.id) = 2 ORDER BY person.last_name LIMIT 5; first_name last_name TotalPets ---------- ---------- ---------- Kendyl Adkins 2 Katherine Aguirre 2 Ismael Alvarez 2 Camden Bailey 2 Clay Barton 2 Select all people with 2 pets (limit 5)