-
-
Save Sandstorm750/d3b5c9bab5c1b41b2c4de21ef32e50da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postgres=# CREATE DATABASE test_guru; | |
CREATE DATABASE | |
postgres=# CREATE TABLE categories ( | |
postgres(# id serial PRIMARY KEY, | |
postgres(# title varchar(40) | |
postgres(# ); | |
CREATE TABLE | |
postgres=# CREATE TABLE tests ( | |
postgres(# id serial PRIMARY KEY, | |
postgres(# title varchar(40), | |
postgres(# level int, | |
postgres(# category_id int | |
postgres(# ); | |
CREATE TABLE | |
postgres=# CREATE TABLE questions ( | |
postgres(# id serial PRIMARY KEY, | |
postgres(# body varchar(50), | |
postgres(# category_id int | |
postgres(# ); | |
CREATE TABLE | |
postgres=# INSERT INTO categories(title) VALUES | |
postgres-# ('Frontend'), | |
postgres-# ('Backend'), | |
postgres-# ('Fullstack'); | |
INSERT 0 3 | |
postgres=# SELECT* | |
postgres-# FROM categories; | |
id | title | |
----+----------- | |
1 | Frontend | |
2 | Backend | |
3 | Fullstack | |
(3 строки) | |
postgres=# INSERT INTO tests(title, level, category_id) VALUES | |
postgres-# ('Ruby', 2, 2), | |
postgres-# ('HTML', 1, 1), | |
postgres-# ('CSS', 2, 1), | |
postgres-# ('SQL', 3, 2), | |
postgres-# ('RoR', 3, 3); | |
INSERT 0 5 | |
postgres=# SELECT* | |
postgres-# FROM tests; | |
id | title | level | category_id | |
----+-------+-------+------------- | |
1 | Ruby | 2 | 2 | |
2 | HTML | 1 | 1 | |
3 | CSS | 2 | 1 | |
4 | SQL | 3 | 2 | |
5 | RoR | 3 | 3 | |
(5 строк) | |
postgres=# INSERT INTO questions(body, category_id) VALUES | |
postgres-# ('Why?', 2), | |
postgres-# ('Why not?', 1), | |
postgres-# ('Where?', 4), | |
postgres-# ('Who?', 2), | |
postgres-# ('How?', 3); | |
INSERT 0 5 | |
postgres=# SELECT* | |
postgres-# FROM questions; | |
id | body | category_id | |
----+----------+------------- | |
1 | Why? | 2 | |
2 | Why not? | 1 | |
3 | Where? | 4 | |
4 | Who? | 2 | |
5 | How? | 3 | |
(5 строк) | |
postgres=# SELECT* FROM tests WHERE level>1; | |
id | title | level | category_id | |
----+-------+-------+------------- | |
1 | Ruby | 2 | 2 | |
3 | CSS | 2 | 1 | |
4 | SQL | 3 | 2 | |
5 | RoR | 3 | 3 | |
(4 строки) | |
postgres=# SELECT* | |
postgres-# FROM questions | |
postgres-# WHERE category_id = 2; | |
id | body | category_id | |
----+------+------------- | |
1 | Why? | 2 | |
4 | Who? | 2 | |
(2 строки) | |
postgres=# UPDATE tests | |
postgres-# SET title = 'HAML', level = 3 | |
postgres-# WHERE title = 'HTML'; | |
UPDATE 1 | |
postgres=# SELECT* | |
postgres-# FROM tests; | |
id | title | level | category_id | |
----+-------+-------+------------- | |
1 | Ruby | 2 | 2 | |
3 | CSS | 2 | 1 | |
4 | SQL | 3 | 2 | |
5 | RoR | 3 | 3 | |
2 | HAML | 3 | 1 | |
(5 строк) | |
postgres=# DELETE | |
postgres-# FROM questions | |
postgres-# WHERE category_id = 4; | |
DELETE 1 | |
postgres=# SELECT* | |
postgres-# FROM questions; | |
id | body | category_id | |
----+----------+------------- | |
1 | Why? | 2 | |
2 | Why not? | 1 | |
4 | Who? | 2 | |
5 | How? | 3 | |
(4 строки) | |
postgres=# SELECT* | |
postgres-# FROM tests | |
postgres-# JOIN categories | |
postgres-# ON tests.category_id = categories.id; | |
id | title | level | category_id | id | title | |
----+-------+-------+-------------+----+----------- | |
1 | Ruby | 2 | 2 | 2 | Backend | |
3 | CSS | 2 | 1 | 1 | Frontend | |
4 | SQL | 3 | 2 | 2 | Backend | |
5 | RoR | 3 | 3 | 3 | Fullstack | |
2 | HAML | 3 | 1 | 1 | Frontend | |
(5 строк) | |
postgres=# SELECT* | |
postgres-# FROM questions | |
postgres-# JOIN tests | |
postgres-# ON questions.category_id = tests.id; | |
id | body | category_id | id | title | level | category_id | |
----+----------+-------------+----+-------+-------+------------- | |
1 | Why? | 2 | 2 | HAML | 3 | 1 | |
2 | Why not? | 1 | 1 | Ruby | 2 | 2 | |
4 | Who? | 2 | 2 | HAML | 3 | 1 | |
5 | How? | 3 | 3 | CSS | 2 | 1 | |
(4 строки) | |
https://gist.github.com/Sandstorm750/d3b5c9bab5c1b41b2c4de21ef32e50da#file-sql-L118
Здесь необходимо выбрать только названия тестов и категорий. Также желательно чтобы в итоговом отношении было ясно какой атрибут названия к чему относится.
https://gist.github.com/Sandstorm750/d3b5c9bab5c1b41b2c4de21ef32e50da#file-sql-L131
Здесь также необходимо выбрать только содержание текста вопросов и названия тестов.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Sandstorm750/d3b5c9bab5c1b41b2c4de21ef32e50da#file-sql-L18
Здесь видимо должен иначе атрибут называться.