Skip to content

Instantly share code, notes, and snippets.

@SergeGray
Created August 25, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeGray/67d7806328fc1771ff65c89e7f24cfde to your computer and use it in GitHub Desktop.
Save SergeGray/67d7806328fc1771ff65c89e7f24cfde to your computer and use it in GitHub Desktop.
postgres=# CREATE DATABASE test_guru;
CREATE DATABASE
postgres=# CREATE TABLE categories (
postgres(# id serial PRIMARY KEY,
postgres(# title varchar(50)
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE tests (
postgres(# id serial PRIMARY KEY,
postgres(# title varchar(50),
postgres(# level int,
postgres(# category_id int REFERENCES categories(id)
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE questions (
postgres(# id serial PRIMARY KEY,
postgres(# body text,
postgres(# test_id int REFERENCES tests(id)
postgres(# );
CREATE TABLE
postgres=# INSERT INTO categories(title) VALUES
postgres-# ('Web Development'),
postgres-# ('General'),
postgres-# ('Machine Learning');
INSERT 0 3
postgres=# INSERT INTO tests(title, level, category_id) VALUES
postgres-# ('Ruby', 0, 2),
postgres-# ('Rails', 1, 1),
postgres-# ('JavaScript', 2, 1),
postgres-# ('Artificial Intelligence', 3, 3),
postgres-# ('Python', 0, 2);
INSERT 0 5
postgres=# INSERT INTO questions(body, test_id) VALUES
postgres-# ('Input method in ruby', 1),
postgres-# ('Default file extension for views', 2),
postgres-# ('Value to represent absence of value', 3),
postgres-# ('Acronym for Artificial Intelligence', 4),
postgres-# ('Name of a data type that stores key-value pairs', 5);
INSERT 0 5
postgres=# SELECT * FROM tests WHERE (level = 2) OR (level = 3);
id | title | level | category_id
----+-------------------------+-------+-------------
3 | JavaScript | 2 | 1
4 | Artificial Intelligence | 3 | 3
(2 rows)
postgres=# SELECT * FROM questions WHERE (test_id = 2);
id | body | test_id
----+----------------------------------+---------
2 | Default file extension for views | 2
(1 row)
postgres=# UPDATE tests SET title = 'Ruby on Rails', level = 2 WHERE (title = 'Rails');
UPDATE 1
postgres=# DELETE FROM questions WHERE (test_id = 4);
DELETE 1
postgres=# SELECT tests.title AS test_title, categories.title AS category_title
postgres-# FROM tests JOIN categories ON tests.category_id = categories.id;
test_title | category_title
-------------------------+------------------
Ruby | General
JavaScript | Web Development
Artificial Intelligence | Machine Learning
Python | General
Ruby on Rails | Web Development
(5 rows)
postgres=# SELECT questions.body AS question_body, tests.title AS test_title
postgres-# FROM questions JOIN tests ON questions.test_id = tests.id;
question_body | test_title
-------------------------------------------------+---------------
Input method in ruby | Ruby
Default file extension for views | Ruby on Rails
Value to represent absence of value | JavaScript
Name of a data type that stores key-value pairs | Python
(4 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment