For these challenges, you'll be working with two tables, owners
and properties
. Keep this relationship in mind when designing your schema: one owner can have many properties.
- Create a database called
apartmentlab
. - Using the
apartmentlab
database, create two tables, one forowners
and one forproperties
. See the next section for schema specs.
-
The
owners
table should consist of:- id (this should be the primary key as well as a unique number that increments automatically)
- name
- age
-
The
properties
table should consist of:- id (this should be the primary key as well as a unique number that increments automatically)
- name
- num_units
- owner_id (this should be a foreign key that references the owners table)
In a markdown file, write the SQL statements required to solve the following tasks. Make sure to test your queries in the psql
terminal! Remember you can type \?
in the psql
terminal for a list of commands.
- Show all the psql users. (Hint: Look for a command to show
roles
) - Show all the tables in your
apartmentlab
database. - Show all the data in the
owners
table. - Add three owners: Donald (age 56), Elaine (age 24), and Emma (age 36).
- Show the names of all owners.
- Show the ages of all of the owners in ascending order.
- Show the name of an owner whose name is Donald.
- Show the age of all owners who are older than 30.
- Show the name of all owners whose name starts with an "E".
- Add an owner named John who is 33 years old.
- Add an owner named Jane who is 43 years old.
- Change Jane's age to 30.
- Change Jane's name to Janet.
- Delete the owner named Janet.
- Add a property named Archstone that has 20 units.
- Add two more properties with names and number of units of your choice.
- Show all of the properties in alphabetical order that are not named Archstone.
- Count the total number of rows in the properties table.
- Show the highest age of all the owners.
- Show the names of the first three owners in your owners table.
- Use a
FULL OUTER JOIN
to show all of the information from the owners table and the properties table. - Update at least one of your properties to belong to the owner with id 1.
- Use an
INNER JOIN
to show all of the owners with associated properties. - Use a
CROSS JOIN
to show all possible combinations of owners and properties.
Note: You may need to research documentation for these challenges.
- In the
properties
table, change the name of the columnname
toproperty_name
. - Count the total number of properties where the
owner_id
is between 1 and 3.