Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Week2/assingment/3.1keys.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
author_id INT PRIMARY KEY,
author_name VARCHAR(255),
university VARCHAR(255),
date_of_birth DATE,
h_index INT,
gender VARCHAR(20)
);

ALTER TABLE authors
ADD mentor INT;

ALTER TABLE authors
ADD CONSTRAINT fk_mentor
FOREIGN KEY (mentor) REFERENCES authors(author_id);
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!!

93 changes: 93 additions & 0 deletions Week2/assingment/3.2relationship.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
CREATE TABLE authors (
author_id INT PRIMARY KEY,
author_name VARCHAR(255),
university VARCHAR(255),
date_of_birth DATE,
h_index INT,
gender VARCHAR(20),
mentor INT,
CONSTRAINT fk_author_mentor
FOREIGN KEY (mentor) REFERENCES authors(author_id)
);


CREATE TABLE research_papers (
paper_id INT PRIMARY KEY,
paper_title VARCHAR(255),
conference VARCHAR(255),
publish_date DATE
);

CREATE TABLE author_papers (
author_id INT,
paper_id INT,
PRIMARY KEY (author_id, paper_id),
FOREIGN KEY (author_id) REFERENCES authors(author_id),
FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id)
);

INSERT INTO authors (author_id, author_name, university, date_of_birth, h_index, gender, mentor) VALUES
(1, 'Alice Johnson', 'MIT', '1980-05-12', 40, 'F', NULL),
(2, 'Bob Smith', 'Harvard', '1975-09-22', 55, 'M', 1),
(3, 'Clara Lee', 'Stanford', '1988-03-10', 32, 'F', 1),
(4, 'David Brown', 'Oxford', '1982-12-01', 29, 'M', 2),
(5, 'Eva Green', 'Cambridge', '1990-07-16', 27, 'F', 3),
(6, 'Frank Moore', 'ETH Zurich', '1979-11-05', 48, 'M', 2),
(7, 'Grace Kim', 'Seoul University', '1987-04-21', 35, 'F', 3),
(8, 'Henry Wilson', 'TU Delft', '1983-06-14', 38, 'M', 1),
(9, 'Isabella Garcia', 'UPenn', '1992-08-13', 20, 'F', 5),
(10,'Jack Miller', 'Carnegie Mellon', '1986-01-19', 30, 'M', 6),
(11,'Karen Adams', 'UCLA', '1984-02-28', 42, 'F', 2),
(12,'Leo Turner', 'Toronto University', '1991-09-30', 25, 'M', 7),
(13,'Mia Scott', 'NYU', '1985-10-25', 39, 'F', 11),
(14,'Noah Bennett', 'Columbia', '1989-12-12', 33, 'M', 3),
(15,'Olivia Clark', 'Princeton', '1993-03-09', 18, 'F', 5);

INSERT INTO research_papers (paper_id, paper_title, conference, publish_date) VALUES
(1, 'Deep Learning in Robotics', 'ICRA', '2020-05-10'),
(2, 'Quantum Computing Advances', 'QIP', '2021-01-15'),
(3, 'Neural Networks Optimization', 'NeurIPS', '2019-12-05'),
(4, 'AI in Healthcare', 'AAAI', '2020-02-10'),
(5, 'Reinforcement Learning Methods', 'ICML', '2021-07-18'),
(6, 'Graph Neural Networks', 'ICLR', '2022-04-25'),
(7, 'Computer Vision Trends', 'CVPR', '2018-06-22'),
(8, 'Natural Language Processing Models', 'ACL', '2022-05-13'),
(9, 'Autonomous Systems', 'RSS', '2019-06-10'),
(10,'Distributed Machine Learning', 'KDD', '2020-08-24'),
(11,'Brain-Inspired Computing', 'CogSci', '2018-07-18'),
(12,'Ethics in AI', 'AIES', '2021-03-12'),
(13,'Robotics Path Planning', 'ICRA', '2022-05-11'),
(14,'Federated Learning', 'NeurIPS', '2021-12-03'),
(15,'Sparse Neural Nets', 'ICML', '2020-07-20'),
(16,'Speech Recognition Models', 'INTERSPEECH', '2019-09-15'),
(17,'Cryptography with AI', 'Crypto', '2022-08-07'),
(18,'Machine Learning Security', 'USENIX', '2021-08-02'),
(19,'Vision Transformers', 'CVPR', '2021-06-15'),
(20,'Probabilistic Models', 'UAI', '2020-07-30'),
(21,'Bio-informatics and ML', 'ISMB', '2022-07-12'),
(22,'Large-Scale Datasets', 'BigData', '2019-12-20'),
(23,'AI for Climate Science', 'AGU', '2021-12-10'),
(24,'Neuroscience + AI', 'COSYNE', '2020-03-05'),
(25,'Robust ML Models', 'ICML', '2022-07-25'),
(26,'Explainable AI', 'IJCAI', '2020-07-12'),
(27,'GANs Advances', 'ECCV', '2018-10-08'),
(28,'Meta Learning', 'ICLR', '2019-04-10'),
(29,'Hybrid Quantum AI', 'QIP', '2022-01-22'),
(30,'AI-Driven Optimization', 'GECCO', '2020-07-11');

INSERT INTO author_papers (author_id, paper_id) VALUES
(1,1),(1,3),(1,10),
(2,2),(2,14),(2,18),
(3,3),(3,6),(3,25),
(4,4),(4,11),
(5,5),(5,21),
(6,6),(6,18),(6,29),
(7,7),(7,8),
(8,9),(8,20),
(9,12),(9,23),
(10,10),(10,24),
(11,11),(11,14),(11,26),
(12,13),(12,28),
(13,15),(13,19),
(14,16),(14,27),
(15,17),(15,30);
13 changes: 13 additions & 0 deletions Week2/assingment/3.3joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
a.author_name AS author,
m.author_name AS mentor
FROM authors a
LEFT JOIN authors m ON a.mentor = m.author_id;

SELECT
a.author_name,
rp.paper_title
FROM authors a
LEFT JOIN author_papers ap ON a.author_id = ap.author_id
LEFT JOIN research_papers rp ON ap.paper_id = rp.paper_id;

35 changes: 35 additions & 0 deletions Week2/assingment/3.4aggregate_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
SELECT
rp.paper_title,
COUNT(ap.author_id) AS author_count
FROM research_papers rp
LEFT JOIN author_papers ap ON rp.paper_id = ap.paper_id
GROUP BY rp.paper_id, rp.paper_title;


SELECT COUNT(*) AS women_papers
FROM author_papers ap
JOIN authors a ON ap.author_id = a.author_id
WHERE a.gender = 'F';


SELECT
university,
AVG(h_index) AS avg_h_index
FROM authors
GROUP BY university;


SELECT
a.university,
COUNT(ap.paper_id) AS paper_count
FROM authors a
LEFT JOIN author_papers ap ON a.author_id = ap.author_id
GROUP BY a.university;


SELECT
university,
MIN(h_index) AS min_h_index,
MAX(h_index) AS max_h_index
FROM authors
GROUP BY university;
Binary file added Week2/prep/Database ER diagram (crow's foot).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week2/prep/Lucid connection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 111 additions & 0 deletions Week2/prep/w1-prep-exe.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
-- Очищаем существующие таблицы (для повторного запуска) -- Drop existing tables (for re-running the script)
DROP TABLE IF EXISTS cuisines CASCADE;
DROP TABLE IF EXISTS main_ingredients CASCADE;
DROP TABLE IF EXISTS ingredients CASCADE;
DROP TABLE IF EXISTS categories CASCADE;
DROP TABLE IF EXISTS cooking_methods CASCADE;
DROP TABLE IF EXISTS recipes CASCADE;
DROP TABLE IF EXISTS recipe_categories CASCADE;
DROP TABLE IF EXISTS recipe_ingredients CASCADE;
DROP TABLE IF EXISTS recipe_methods CASCADE;
DROP TABLE IF EXISTS recipe_ingredient_amounts CASCADE;



---------------------------------------------------------
-- 1. Таблица кухонь (Italian, Chinese, Japanese…) -- Table of Cuisines (Italian, Chinese, Japanese…)
---------------------------------------------------------
CREATE TABLE cuisines (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
);

---------------------------------------------------------
-- 2. Таблица основных ингредиентов (мясо, рыба, овощи) -- Table of Main Ingredients (meat, fish, vegetables)
---------------------------------------------------------
CREATE TABLE main_ingredients (
id SERIAL PRIMARY KEY,
name VARCHAR(150) NOT NULL UNIQUE
);

---------------------------------------------------------
-- 3. Таблица всех возможных ингредиентов (морковь, масло, соль) -- Table of All Possible Ingredients (carrot, oil, salt)
---------------------------------------------------------
CREATE TABLE ingredients (
id SERIAL PRIMARY KEY,
name VARCHAR(150) NOT NULL UNIQUE
);

---------------------------------------------------------
-- 4. Таблица категорий (суп, салат, десерт, завтрак) -- Table of Categories (soup, salad, dessert, breakfast)
---------------------------------------------------------
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
);

---------------------------------------------------------
-- 5. Таблица методов приготовления (жарка, запекание, варка) -- Table of Cooking Methods (frying, baking, boiling)
---------------------------------------------------------
CREATE TABLE cooking_methods (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
);

---------------------------------------------------------
-- 6. Главная таблица рецептов -- Main Recipes Table
---------------------------------------------------------
CREATE TABLE recipes (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
cuisine_id INT,
main_ingredient_id INT,
FOREIGN KEY (cuisine_id) REFERENCES cuisines(id),
FOREIGN KEY (main_ingredient_id) REFERENCES main_ingredients(id)
);

---------------------------------------------------------
-- 7. Связь рецепт ↔ категории (many-to-many) -- Recipe ↔ Categories Relationship (many-to-many)
---------------------------------------------------------
CREATE TABLE recipe_categories (
recipe_id INT NOT NULL,
category_id INT NOT NULL,
PRIMARY KEY (recipe_id, category_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(id),
FOREIGN KEY (category_id) REFERENCES categories(id)
);

---------------------------------------------------------
-- 8. Связь рецепт ↔ ингредиенты (many-to-many) -- Recipe ↔ Ingredients Relationship (many-to-many)
---------------------------------------------------------
CREATE TABLE recipe_ingredients (
recipe_id INT NOT NULL,
ingredient_id INT NOT NULL,
amount VARCHAR(50), -- например "200 г", "1 ст.л."
PRIMARY KEY (recipe_id, ingredient_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(id),
FOREIGN KEY (ingredient_id) REFERENCES ingredients(id)
);

---------------------------------------------------------
-- 9. Связь рецепт ↔ методы приготовления (many-to-many) -- Recipe ↔ Cooking Methods Relationship (many-to-many)
---------------------------------------------------------
CREATE TABLE recipe_methods (
recipe_id INT NOT NULL,
method_id INT NOT NULL,
PRIMARY KEY (recipe_id, method_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(id),
FOREIGN KEY (method_id) REFERENCES cooking_methods(id)
);

---------------------------------------------------------
-- 10. кол-во ингредиентов в рецепте -- Quantity of Ingredients in Recipe
---------------------------------------------------------
CREATE TABLE recipe_ingredient_amounts (
recipe_id INT NOT NULL,
ingredient_id INT NOT NULL,
amount VARCHAR(50) NOT NULL, -- например "200 г", "1 ст.л."
PRIMARY KEY (recipe_id, ingredient_id),
FOREIGN KEY (recipe_id) REFERENCES recipes(id),
FOREIGN KEY (ingredient_id) REFERENCES ingredients(id)
);
98 changes: 98 additions & 0 deletions Week2/prep/w2-prep-insert-mok-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-- ==========================================
-- Mock data inserts for Week 2 Prep Exercise
-- ==========================================

-- 1. Cuisines
INSERT INTO cuisines (name) VALUES
('Japanese')
ON CONFLICT (name) DO NOTHING;

-- 2. Main ingredients
INSERT INTO main_ingredients (name) VALUES
('Cheese'),
('Vegetables'),
('Pasta'),
('Eggs')
ON CONFLICT (name) DO NOTHING;

-- 3. Categories
INSERT INTO categories (name) VALUES
('Cake'),
('No-Bake'),
('Vegetarian'),
('Vegan'),
('Gluten-Free'),
('Japanese')
ON CONFLICT (name) DO NOTHING;

-- 4. Ingredients
INSERT INTO ingredients (name) VALUES
('Cheese'),
('Vegetables'),
('Pasta'),
('Eggs'),
('Condensed milk'),
('Cream Cheese'),
('Lemon Juice'),
('Pie Crust'),
('Cherry Jam'),
('Brussels Sprouts'),
('Sesame seeds'),
('Pepper'),
('Salt'),
('Olive oil'),
('Macaroni'),
('Butter'),
('Flour'),
('Milk'),
('Shredded Cheddar cheese'),
('Soy sauce'),
('Sugar')
ON CONFLICT (name) DO NOTHING;

-- 5. Recipes
INSERT INTO recipes (name, cuisine_id, main_ingredient_id) VALUES
('No-Bake Cheesecake', NULL, 1),
('Roasted Brussels Sprouts', NULL, 2),
('Mac & Cheese', NULL, 3),
('Tamagoyaki Japanese Omelette', 1, 4)
ON CONFLICT DO NOTHING;

-- 6. Recipe ↔ Categories
INSERT INTO recipe_categories (recipe_id, category_id) VALUES
(1, 1), (1, 2), (1, 3),
(2, 4), (2, 5),
(3, 3),
(4, 3), (4, 6)
ON CONFLICT DO NOTHING;

-- 7. Recipe ↔ Ingredients
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES
-- No-Bake Cheesecake
(1, 5, '200 ml'),
(1, 6, '250 g'),
(1, 7, '1 tbsp'),
(1, 8, '1 crust'),
(1, 9, '2 tbsp'),
-- Roasted Brussels Sprouts
(2, 10, '500 g'),
(2, 7, '1 tbsp'),
(2, 11, '1 tsp'),
(2, 12, '1 tsp'),
(2, 13, '1 tsp'),
(2, 14, '2 tbsp'),
-- Mac & Cheese
(3, 15, '200 g'),
(3, 16, '50 g'),
(3, 17, '2 tbsp'),
(3, 13, '1 tsp'),
(3, 12, '1 tsp'),
(3, 18, '200 ml'),
(3, 19, '150 g'),
-- Tamagoyaki Japanese Omelette
(4, 4, '4'),
(4, 20, '1 tbsp'),
(4, 21, '1 tsp'),
(4, 13, '1 tsp'),
(4, 14, '1 tbsp')
ON CONFLICT DO NOTHING;
24 changes: 24 additions & 0 deletions Week2/prep/w2-request.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- 1. All vegetarian recipes with potatoes (но картошки нет в данных)
SELECT r.name AS recipe_name
FROM recipes r
JOIN recipe_categories rc ON r.id = rc.recipe_id
JOIN categories c ON rc.category_id = c.id
WHERE c.name = 'Vegetarian';

-- 2. All cakes that do not need baking
SELECT r.name AS recipe_name
FROM recipes r
JOIN recipe_categories rc1 ON r.id = rc1.recipe_id
JOIN categories c1 ON rc1.category_id = c1.id
JOIN recipe_categories rc2 ON r.id = rc2.recipe_id
JOIN categories c2 ON rc2.category_id = c2.id
WHERE c1.name = 'Cake' AND c2.name = 'No-Bake';

-- 3. All vegan and Japanese recipes
SELECT r.name AS recipe_name
FROM recipes r
JOIN recipe_categories rc1 ON r.id = rc1.recipe_id
JOIN categories c1 ON rc1.category_id = c1.id
JOIN recipe_categories rc2 ON r.id = rc2.recipe_id
JOIN categories c2 ON rc2.category_id = c2.id
WHERE c1.name = 'Vegan' AND c2.name = 'Japanese';