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
81 changes: 81 additions & 0 deletions Week2/assignment/aggregates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { client } from './db.js'

const papersWithAuthorsCount = `
SELECT
rp.paper_id,
rp.paper_title,
COUNT(ap.author_id) AS authors_count
FROM research_papers rp
LEFT JOIN authors_papers ap
ON rp.paper_id = ap.paper_id
GROUP BY rp.paper_id, rp.paper_title
ORDER BY rp.paper_id
`

const totalPapersByFemaleAuthors = `
SELECT
COUNT(DISTINCT ap.paper_id) AS total_papers_by_female_authors
FROM authors a
JOIN authors_papers ap
ON a.author_id = ap.author_id
WHERE a.gender = 'female'
`

const avgHIndexPerUniversity = `
SELECT
university,
AVG(h_index) AS avg_h_index
FROM authors
GROUP BY university
ORDER BY university
`

const totalPapersPerUniversity = `
SELECT
a.university,
COUNT(DISTINCT ap.paper_id) AS total_papers
FROM authors a
LEFT JOIN authors_papers ap
ON a.author_id = ap.author_id
GROUP BY a.university
ORDER BY a.university
`

const minMaxHIndexPerUniversity = `
SELECT
university,
MIN(h_index) AS min_h_index,
MAX(h_index) AS max_h_index
FROM authors
GROUP BY university
ORDER BY university
`

async function main() {
try {
await client.connect()
console.log('Connected to DB')

const res1 = await client.query(papersWithAuthorsCount)
console.table(res1.rows)

const res2 = await client.query(totalPapersByFemaleAuthors)
console.table(res2.rows)

const res3 = await client.query(avgHIndexPerUniversity)
console.table(res3.rows)

const res4 = await client.query(totalPapersPerUniversity)
console.table(res4.rows)

const res5 = await client.query(minMaxHIndexPerUniversity)
console.table(res5.rows)
} catch (err) {
console.error('Error in exercise4_aggregates:', err)
} finally {
await client.end()
console.log('Connection closed')
}
}

main()
9 changes: 9 additions & 0 deletions Week2/assignment/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pg from 'pg'

export const client = new pg.Client({
user: 'postgres',
host: 'localhost',
database: 'postgres',
password: '123456',
port: 5432,
})
48 changes: 48 additions & 0 deletions Week2/assignment/joins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { client } from './db.js'

const queryAuthorsWithMentors = `
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 queryAuthorsWithPapers = `
SELECT
a.author_id,
a.author_name,
a.university,
a.date_of_birth,
a.h_index,
a.gender,
rp.paper_title
FROM authors a
LEFT JOIN authors_papers ap
ON ap.author_id = a.author_id
LEFT JOIN research_papers rp
ON rp.paper_id = ap.paper_id
ORDER BY a.author_id, rp.paper_title
`

async function main() {
try {
await client.connect()
console.log('Connected to DB')

const res1 = await client.query(queryAuthorsWithMentors)
console.table(res1.rows)

const res2 = await client.query(queryAuthorsWithPapers)
console.table(res2.rows)
} catch (err) {
console.error('Error in exercise3_joins:', err)
} finally {
await client.end()
console.log('Connection closed')
}
}

main()
56 changes: 56 additions & 0 deletions Week2/assignment/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { client } from './db.js'

const createAuthorsTable = `
CREATE TABLE IF NOT EXISTS authors (
author_id SERIAL PRIMARY KEY,
author_name TEXT NOT NULL,
university TEXT,
date_of_birth DATE,
h_index INTEGER,
gender VARCHAR(10)
)
`

const addMentorColumn = `
ALTER TABLE authors
ADD COLUMN IF NOT EXISTS mentor INTEGER
`

const addMentorForeignKey = `
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'fk_authors_mentor'
AND table_name = 'authors'
) THEN
ALTER TABLE authors
ADD CONSTRAINT fk_authors_mentor
FOREIGN KEY (mentor)
REFERENCES authors(author_id)
ON DELETE SET NULL
END IF
END
$$
`

async function main() {
try {
await client.connect()
console.log('Connected to DB')

await client.query(createAuthorsTable)

await client.query(addMentorColumn)

await client.query(addMentorForeignKey)
} catch (err) {
console.error('Error in exercise1_keys:', err)
} finally {
await client.end()
console.log('Connection closed')
}
}

main()
162 changes: 162 additions & 0 deletions Week2/assignment/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/assignment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "assignment",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"pg": "8.16.3"
}
}
Loading