We will use the following site to test SQL commands: https://sqliteonline.com/
Connect to the 'PostgreSQL' database. Note that this site has a 15 minute limit until the database is refreshed.
Create the following two tables for this demo:
For more information on creating tables: https://www.postgresql.org/docs/9.2/sql-createtable.html
Insert data into the 'person' table:
Insert into person (id, person_name) values (1, 'Sally');
Insert into person (id, person_name) values (2, 'Bob');
Insert into person (id, person_name) values (3, 'Lucy');
Insert data into the 'country' table:
Insert into country (id, country_name) values (1, 'Canada'), (2, 'USA'), (3, 'Mexico');
Update the person table to have a column for the person's country:
ALTER TABLE person ADD column country_id integer;
Update the data:
update person
set country_id = 2
where person_name = 'Lucy';
update person
set country_id = 1
where person_name in ('Sally', 'Bob');
-- select all data from person table
select * from person;
-- filter data from person table by name
select * from person where person_name = 'Lucy';
-- join data between person and country tables
select person_name, country_name from person
inner join country on person.country_id = country.id;
select country_name, count(*) from person
inner join country on person.country_id = country.id
group by country_name;
select country_name, count(*) from person
inner join country on person.country_id = country.id
group by country_name
having count(*) > 1;
-- delete row from country table where the ID of the country = 3
delete from country where id = 3;
| Name | Reference |
|---|---|
| Postgres cheatsheet with common commands | https://www.postgresqltutorial.com/postgresql-cheat-sheet/ |
| Postgres documentation | https://www.postgresql.org/docs/9.4/ |