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
18 changes: 18 additions & 0 deletions Week2/Assignment/ Aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const {client} = require('./connection');
async function runQuery() {
try{
await client.query(`SELECT RP.paper_title,COUNT(PA.author_id) FROM research_papers RP LEFT JOIN papers_authors PA ON RP.paper_id=PA.paper_id GROUP BY RP.paper_title`);
await client.query(`SELECT COUNT(PA.paper_id) AS total_papers_by_female FROM author A JOIN papers_authors PA ON A.author_id = PA.author_id WHERE A.gender = 'F';`);
await client.query(`SELECT A.university,AVG(A.h_index) AS avg_h_index FROM author A GROUP BY A.university;`);
await client.query(`SELECT A.university,COUNT(PA.paper_id) AS research_papers FROM author A JOIN papers_authors PA ON A.author_id=PA.author_id GROUP BY A.university`);
await client.query(`SELECT A.university,MIN(A.h_index) AS min_h_index,MAX(A.h_index) AS max_h_index FROM author A GROUP BY A.university`);
Comment on lines +4 to +8
Copy link

Choose a reason for hiding this comment

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

Your queries are correct. For query:

SELECT COUNT(PA.paper_id) AS total_papers_by_female FROM author A JOIN papers_authors PA ON A.author_id = PA.author_id WHERE A.gender = 'F';

If a paper has two different female authors, this paper will be counted twice. Depending on the business requirements, this might be what we want. If you don't want to count the same paper twice, you need to use DISTINCT keyword.
Similar thing happens for this query too:

SELECT A.university,COUNT(PA.paper_id) AS research_papers FROM author A JOIN papers_authors PA ON A.author_id=PA.author_id GROUP BY A.university



}
catch(err){
console.error('Error executing query', err.stack);
} finally {
await client.end();
}
}
runQuery();
12 changes: 12 additions & 0 deletions Week2/Assignment/Joins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const {client} = require('./connection');
async function runQuery() {
try {
await client.query(`SELECT a.author_name,a2.author_name AS mentor_name FROM author a INNER JOIN author a2 ON a2.author_id=a.mentor`);
await client.query(`SELECT A.*,RP.paper_title FROM author A LEFT JOIN papers_authors PA ON A.author_id=PA.author_id LEFT JOIN research_papers RP ON PA.paper_id=RP.paper_id);`);

} catch (err) {
console.error('Error executing query', err.stack);
} finally {
await client.end();
}
}
28 changes: 28 additions & 0 deletions Week2/Assignment/Keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {client}=require('./connection');
async function main(){
try{
const creatTableAuthor = `
CREATE TABLE IF NOT EXISTS author(
author_id SERIAL PRIMARY KEY,
author_name VARCHAR(100) NOT NULL,
university VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
h_index INT NOT NULL,
gender CHAR(1) NOT NULL CHECK (gender IN ('M', 'F', 'O'))
);`;

const alterTableAuthor = `ALTER TABLE author ADD COLUMN mentor INT REFERENCES author(author_id);`;

await client.query(creatTableAuthor);
await client.query(alterTableAuthor);
console.log('Table created and altered successfully');

}catch(err){
console.error('Connection error',err.stack);
}
finally{
await client.end();
}

}
main();
30 changes: 30 additions & 0 deletions Week2/Assignment/Relationships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {Client}=require ('pg');
async function main(){
try{
await client.connect();
const creatTableResearch_Papers = `
CREATE TABLE IF NOT EXISTS research_papers(
paper_id SERIAL PRIMARY KEY,
paper_title VARCHAR(200) NOT NULL,
conference VARCHAR(100) NOT NULL,
publish_date DATE NOT NULL
);`;

const creatPapers_authors=`CREATE TABLE IF NOT EXISTS papers_authors(
paper_id INT REFERENCES research_papers(paper_id),
author_id INT REFERENCES author(author_id),
PRIMARY KEY (paper_id, author_id)
);`;
await client.query(creatTableResearch_Papers);
console.log('Table created successfully');


}
catch(err){
console.error('Connection error',err.stack);
}
finally{
await client.end();
}
}
main();
10 changes: 10 additions & 0 deletions Week2/Assignment/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {Client}= require('pg');
const client= new Client({
user:'hyfuser',
host:'localhost',
database:'authoers',
password:'hyfpassword',
port:5432,
});
client.connect();
module.exports=client;
73 changes: 73 additions & 0 deletions Week2/Assignment/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const {client}=require('./connection');
async function runQuery(){
try{
await client.query(`INSERT INTO author(author_name, university, date_of_birth, h_index, gender, mentor)
VALUES
('Alice Smith', 'MIT', '1980-03-15', 35, 'F', NULL),
('Bob Johnson', 'Stanford', '1975-07-22', 40, 'M', 1),
('Carol Williams', 'Harvard', '1982-11-05', 28, 'F', NULL),
('David Brown', 'Oxford', '1978-01-17', 45, 'M', 3),
('Emma Davis', 'Cambridge', '1985-09-10', 30, 'F', NULL),
('Frank Miller', 'UCLA', '1979-12-03', 50, 'M', 2),
('Grace Wilson', 'Yale', '1983-05-25', 33, 'F', NULL),
('Henry Moore', 'Princeton', '1981-08-12', 38, 'M', 5),
('Ivy Taylor', 'Columbia', '1986-02-20', 27, 'F', NULL),
('Jack Anderson', 'Caltech', '1977-06-30', 42, 'M', 4),
('Karen Thomas', 'Imperial College', '1984-10-15', 31, 'F', NULL),
('Leo Jackson', 'ETH Zurich', '1980-04-01', 36, 'M', 7),
('Mia White', 'Sorbonne', '1987-01-11', 29, 'F', NULL),
('Nick Harris', 'Tokyo University', '1976-03-22', 47, 'M', 10),
('Olivia Martin', 'Seoul National University', '1982-07-18', 34, 'F', NULL);
`);
console.log('Data inserted successfully');
await client.query(`INSERT INTO research_papers(paper_title, conference, publish_date)
VALUES
('AI in Healthcare', 'NeurIPS', '2020-06-15'),
('Quantum Computing Basics', 'QIP', '2019-05-10'),
('Machine Learning Algorithms', 'ICML', '2021-07-20'),
('Robotics Advances', 'ICRA', '2018-04-12'),
('Data Privacy Techniques', 'USENIX', '2020-11-05'),
('Blockchain in Finance', 'IEEE Blockchain', '2019-09-15'),
('Natural Language Processing', 'ACL', '2021-08-01'),
('Computer Vision Trends', 'CVPR', '2020-06-22'),
('Cybersecurity Methods', 'Black Hat', '2021-02-10'),
('Cloud Computing Models', 'IEEE Cloud', '2019-12-05'),
('Deep Learning Optimization', 'NeurIPS', '2020-12-12'),
('IoT Security Challenges', 'IoTDI', '2018-10-20'),
('Augmented Reality Systems', 'ISMAR', '2019-09-30'),
('Reinforcement Learning', 'ICML', '2020-05-18'),
('Edge Computing Applications', 'IEEE Edge', '2021-03-10'),
('Genomics Data Analysis', 'Bioinformatics', '2018-07-12'),
('Smart Cities Research', 'IEEE SmartCity', '2020-01-25'),
('Big Data Analytics', 'KDD', '2019-08-22'),
('Autonomous Vehicles', 'IV', '2021-04-05'),
('Renewable Energy Systems', 'IEEE PES', '2020-03-30'),
('Graph Neural Networks', 'NeurIPS', '2021-06-12'),
('Human-Computer Interaction', 'CHI', '2019-05-18'),
('Sentiment Analysis', 'ACL', '2020-09-20'),
('Protein Folding Prediction', 'ICML', '2021-02-14'),
('Virtual Reality Education', 'VRST', '2018-11-01'),
('Social Network Analysis', 'WWW', '2019-03-12'),
('Medical Imaging', 'MICCAI', '2020-07-15'),
('Autonomous Drones', 'ICRA', '2021-05-20'),
('Smart Home IoT', 'IEEE IoT', '2019-12-08'),
('Energy Efficient Computing', 'ISCA', '2020-10-25');`);
console.log('Data inserted successfully');

await client.query(`INSERT INTO papers_authors(paper_id, author_id)
VALUES
(1,1),(1,2),(2,3),(2,4),(3,5),(3,6),(4,7),(4,8),
(5,9),(5,10),(6,11),(6,12),(7,13),(7,14),(8,15),
(9,1),(10,2),(11,3),(12,4),(13,5),(14,6),(15,7),
(16,8),(17,9),(18,10),(19,11),(20,12),(21,13),
(22,14),(23,15),(24,1),(25,2),(26,3),(27,4),
(28,5),(29,6),(30,7);`)



}
catch(err){
console.error('Connection error',err.stack);
}

}
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.

15 changes: 15 additions & 0 deletions Week2/Assignment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "assignment",
"version": "1.0.0",
"main": "Keys.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"pg": "^8.16.3"
}
}