-
Notifications
You must be signed in to change notification settings - Fork 9
DARYNA-TKACHENKO-W2-DATABASE #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { client } from './database.js'; | ||
|
|
||
| async function exercise4Aggregates() { | ||
| try { | ||
| await client.connect(); | ||
|
|
||
| const papersCountQuery = ` | ||
| SELECT rp.paper_title, | ||
| COUNT(ap.author_id) AS author_count | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs, but you might want to be more specific and declare where you want to select the paper titles and author ids from. Otherwise, postgres will implicitly assume which table to use. Which in this case is okay, but in a real production database may cause issues. |
||
| FROM research_papers AS rp | ||
| LEFT JOIN author_paper AS ap | ||
| ON rp.paper_id = ap.paper_id | ||
| GROUP BY rp.paper_title | ||
| ORDER BY rp.paper_title; | ||
| `; | ||
| const papersCountResult = await client.query(papersCountQuery); | ||
| console.table(papersCountResult.rows); | ||
|
|
||
| const femalePapersQuery = ` | ||
| SELECT COUNT(ap.paper_id) AS total_papers_by_female_authors | ||
| FROM authors AS a | ||
| JOIN author_paper AS ap | ||
| ON a.author_id = ap.author_id | ||
| WHERE a.gender = 'Female'; | ||
| `; | ||
| const femalePapersResult = await client.query(femalePapersQuery); | ||
| console.table(femalePapersResult.rows); | ||
|
|
||
| const avgHIndexQuery = ` | ||
| SELECT a.university, | ||
| AVG(a.h_index) AS avg_h_index | ||
| FROM authors AS a | ||
| GROUP BY a.university | ||
| ORDER BY a.university; | ||
| `; | ||
| const avgHIndexResult = await client.query(avgHIndexQuery); | ||
| console.table(avgHIndexResult.rows); | ||
|
|
||
| const papersByUniversityQuery = ` | ||
| SELECT a.university, | ||
| COUNT(ap.paper_id) AS total_papers | ||
| FROM authors AS a | ||
| LEFT JOIN author_paper AS ap | ||
| ON a.author_id = ap.author_id | ||
| GROUP BY a.university | ||
| ORDER BY a.university; | ||
| `; | ||
| const papersByUniversityResult = await client.query(papersByUniversityQuery); | ||
| console.table(papersByUniversityResult.rows); | ||
|
|
||
| const hIndexMinMaxQuery = ` | ||
| SELECT a.university, | ||
| MIN(a.h_index) AS min_h_index, | ||
| MAX(a.h_index) AS max_h_index | ||
| FROM authors AS a | ||
| GROUP BY a.university | ||
| ORDER BY a.university; | ||
| `; | ||
| const hIndexMinMaxResult = await client.query(hIndexMinMaxQuery); | ||
| console.table(hIndexMinMaxResult.rows); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error:", error); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| exercise4Aggregates(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import pkg from 'pg'; | ||
| const { Client } = pkg; | ||
|
|
||
| export const client = new Client({ | ||
| user: 'hyfuser', | ||
| host: 'localhost', | ||
| database: 'authors_db', | ||
| password: 'hyfpassword', | ||
| port: 5432, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { client } from './database.js'; | ||
|
|
||
| async function exercise3Joins() { | ||
| try { | ||
| await client.connect(); | ||
|
|
||
| const mentorQuery = ` | ||
| SELECT a.author_name AS author, | ||
| m.author_name AS mentor | ||
| FROM authors a | ||
| LEFT JOIN authors m ON a.mentor = m.author_id | ||
| ORDER BY a.author_id; | ||
| `; | ||
| const mentorResult = await client.query(mentorQuery); | ||
| console.log("Authors and their mentors:"); | ||
| console.table(mentorResult.rows); | ||
|
|
||
| const papersQuery = ` | ||
| SELECT a.*, rp.paper_title | ||
| FROM authors a | ||
| LEFT JOIN author_paper ap ON a.author_id = ap.author_id | ||
| LEFT JOIN research_papers rp ON ap.paper_id = rp.paper_id | ||
| ORDER BY a.author_id, rp.paper_id; | ||
| `; | ||
| const papersResult = await client.query(papersQuery); | ||
| console.log("Authors and their papers:"); | ||
| console.table(papersResult.rows); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error:", error); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| exercise3Joins(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { client } from './database.js'; | ||
|
|
||
| async function setupAuthorsTable() { | ||
| try { | ||
| await client.connect(); | ||
|
|
||
| const sql = ` | ||
| DROP TABLE IF EXISTS author_paper CASCADE; | ||
| DROP TABLE IF EXISTS research_papers CASCADE; | ||
| DROP TABLE IF EXISTS authors CASCADE; | ||
|
|
||
| CREATE TABLE authors | ||
| ( | ||
| author_id SERIAL PRIMARY KEY, | ||
| author_name VARCHAR(255), | ||
| university VARCHAR(255), | ||
| date_of_birth DATE, | ||
| h_index INT, | ||
| gender VARCHAR(10) | ||
| ); | ||
|
|
||
| ALTER TABLE authors | ||
| ADD COLUMN mentor INT; | ||
|
|
||
| ALTER TABLE authors | ||
| ADD CONSTRAINT fk_mentor | ||
| FOREIGN KEY (mentor) | ||
| REFERENCES authors (author_id) | ||
| ON DELETE SET NULL; | ||
| `; | ||
|
|
||
| await client.query(sql); | ||
| console.log('Authors table created'); | ||
| } catch (err) { | ||
| console.error('Problem creating authors table', err); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| setupAuthorsTable(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "assigments", | ||
| "type": "module", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "database.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "pg": "^8.16.3" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file name contains a trailing whitespace. This can cause issues for running your scripts. Be careful of whitespace when naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dashaaaa21 This is still unresolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crevulus thank you, I fixed https://github.com/dashaaaa21/databases-cohort54/tree/DARYNA-TKACHENKO-WEEK2-DATABASE/Week2/assigments