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
69 changes: 69 additions & 0 deletions Week2/assigments/aggregateFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { client } from './database.js';

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.

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

Copy link
Author

Choose a reason for hiding this comment

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


async function exercise4Aggregates() {
try {
await client.connect();

const papersCountQuery = `
SELECT rp.paper_title,
COUNT(ap.author_id) AS author_count

Choose a reason for hiding this comment

The 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();
10 changes: 10 additions & 0 deletions Week2/assigments/database.js
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,
});
36 changes: 36 additions & 0 deletions Week2/assigments/joins.js
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();
41 changes: 41 additions & 0 deletions Week2/assigments/key.js
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();
162 changes: 162 additions & 0 deletions Week2/assigments/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Week2/assigments/package.json
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"
}
}
Loading