forked from HackYourFuture/databases
-
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
Open
dashaaaa21
wants to merge
3
commits into
HackYourAssignment:main
Choose a base branch
from
dashaaaa21:DARYNA-TKACHENKO-WEEK2-DATABASE
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 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 | ||
| FROM research_papers rp | ||
| LEFT JOIN author_paper 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 a | ||
| JOIN author_paper 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 university, | ||
| AVG(h_index) AS avg_h_index | ||
| FROM authors | ||
| GROUP BY university | ||
| ORDER BY 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 a | ||
| LEFT JOIN author_paper 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 university, | ||
| MIN(h_index) AS min_h_index, | ||
| MAX(h_index) AS max_h_index | ||
| FROM authors | ||
| GROUP BY university | ||
| ORDER BY university; | ||
| `; | ||
|
|
||
| const hIndexMinMaxResult = await client.query(hIndexMinMaxQuery); | ||
| console.table(hIndexMinMaxResult.rows); | ||
|
|
||
| } catch (error) { | ||
| console.error("Error:", error); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| exercise4Aggregates(); |
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
| 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, | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| 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.author_id, | ||
| a.author_name, | ||
| a.university, | ||
| a.date_of_birth, | ||
| a.h_index, | ||
| a.gender, | ||
| a.mentor, | ||
| 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(); | ||
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
| 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.
Oops, something went wrong.
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
| 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" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
There's an easier way to select all columns without having to write out each one. Can you remember what it is?