Lab 2 Getting Familiar with SQL Queries - JadenGil/Jaden-Tech-Journal GitHub Wiki

To start this lab I made a database called "pets"

image

I then made the tables owner and cats:

Owner:

image

Cats:

image

After the tables were created I added the following to the owner table:

image


SQL Queries:

I found this guide that helped me figure out the SQL queries I needed to come up with:

https://www.w3schools.com/sql/sql_quickref.asp

Deliverable-1: Write an SQL query that displays name and birth of cats whose names are Siggy

image

Deliverable-2: Write an SQL query that displays name and birth of cats whose owners names are starting with the letter 'F'

image

Explanation:

JOIN owners ON cats.ownerid = owners.id joins the cats table with the owner table based on the ownerid in cats and the id in owner.

WHERE owner.name LIKE 'F%' filters the results to only include owners whose names start with "F".

SELECT cats.name, cats.birth retrieves only the name and birth columns from the cats table.

Deliverable-3: Write an SQL query (using a UNION) that displays the cat names, their owners names, and the birth of cats in single table for cats born in year 2020

image

This one was a little more complicated than the others because there was a syntax error with WHERE YEAR(cats.birth) There should've been a space on both of those arguments and that was unclear at first.

Explaination:

I'm using two SELECT statements that pull the same information (cat_name, owner_name, and birth) from the cats and owners tables.

The JOIN operation links cats and owners on ownerid (in cats) and id (in owners).

WHERE YEAR (cats.birth) = 2020 filters the results to only show cats born in the year 2020.

Deliverable-4: Write an SQL query that displays names of owners who has no cats

image

Explaination:

LEFT JOIN: The LEFT JOIN returns all rows from the owner table, along with matching rows from the cats table. If an owner has no cats, the cats.ownerid field will be NULL.

WHERE cats.ownerid IS NULL: This condition filters for rows where there is no matching ownerid in the cats table, meaning the owner has no cats.