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
62 changes: 62 additions & 0 deletions Week2/assignment/Aggregate_Functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "database_assingnment",
});
console.log(" Connected to database: database_assingnment");

const [paperAuthors] = await connection.execute(`
select
rp.paper_id,
rp.paper_title,
count(ap.author_id) as number_of_authors
from research_Papers rp
join author_paper ap on rp.paper_id = ap.paper_id
group by rp.paper_id, rp.paper_title
`);
console.log("all research papers and number of authors that wrote them");
console.table(paperAuthors);

//
const [femalePaperCount] = await connection.execute(`
select count(distinct ap.paper_id) as total_unique_female_papers
from author_paper ap
join authors a ON ap.author_id = a.author_id
where a.gender = 'female'
`);
console.log("total research papers by femal authors");
console.table(femalePaperCount);

//
const [avgHIndexPerUni] = await connection.execute(`
select university, avg(h_index) as average_h_index
from authors
group by university
`);
console.log("Average h-index per university");
console.table(avgHIndexPerUni);

//
const [totalPapersPerUni] = await connection.execute(`
select a.university, count(distinct ap.paper_id) as total_unique_papers
from authors a
join author_paper ap ON a.author_id = ap.author_id
group by a.university
`);
console.log("Total research papers per university");
console.table(totalPapersPerUni);

//
const [minMaxHIndex] = await connection.execute(`
select university, min(h_index) as min_h_index, max(h_index) as max_h_index
from authors
group by university
`);
console.log("min and max h-index per university:");
console.table(minMaxHIndex);

//
await connection.end();
34 changes: 34 additions & 0 deletions Week2/assignment/joins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "database_assingnment",
});
console.log(" Connected to database: database_assingnment");

const [authorsWithMentors] = await connection.execute(`
select a.author_name as author,
m.author_name as mentor
from authors a left join authors m on a.mentor = m.author_id

`);
console.log("authors and thier mentors");
console.table(authorsWithMentors);

//

const [authorsAndPapers] = await connection.execute(`
select a.author_name as author,
rp.paper_title as paper 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

`);

console.log("Authors and their papers:");
console.table(authorsAndPapers);
//

await connection.end();
40 changes: 40 additions & 0 deletions Week2/assignment/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "database_assingnment",
});
console.log(" Connected to database: database_assingnment");
//
await connection.execute(`DROP TABLE IF EXISTS author_paper`);
await connection.execute(`DROP TABLE IF EXISTS research_papers`);
await connection.execute(`DROP TABLE IF EXISTS authors`);

await connection.execute(`
create table authors (
author_id INT auto_increment primary key,
author_name varchar(100),
university varchar(100),
date_of_birth date,
h_index INT,
gender varchar(10),
mentor INT,
foreign key (mentor) REFERENCES authors(author_id)
);

`);

console.log('Table "authors" created with mentor as foreign key');

//
await connection.execute(`
insert into authors (author_name, university, date_of_birth, h_index, gender)
values
('rizan', 'hyf', '1998-01-01', 45, 'male'),
('gea', 'Stanford', '1975-05-15', 38, 'female'),
('araz', 'Oxford', '1992-11-23', 29, 'male');
`);

await connection.end();
143 changes: 143 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": "Aggregate_Functions.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"mysql2": "^3.14.1"
}
}
61 changes: 61 additions & 0 deletions Week2/assignment/relationships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "database_assingnment",
});
console.log(" Connected to database: database_assingnment");

await connection.execute(`drop table if exists author_paper`);
await connection.execute(`drop table if exists research_papers`);

/// Clean up data and reset auto-increment

await connection.execute(`ALTER TABLE authors AUTO_INCREMENT = 1`);

await connection.execute(`
create table research_papers(
paper_id int auto_increment primary key,
paper_title varchar(100),
conference varchar(100),
publish_date date

)
`);
console.log("Table 'research_papers' created");

await connection.execute(`
create table author_paper (
author_id INT,
paper_id INT,
primary key (author_id, paper_id),
foreign key (author_id) references authors(author_id) on delete CASCADE,
foreign key (paper_id) references research_papers(paper_id) on delete CASCADE
);
`);
console.log("Table 'author_paper'junction table created");

await connection.execute(`
insert into research_papers (paper_title, conference, publish_date)
values
('AI in Healthcare', 'NeurIPS', '2022-12-01'),
('Quantum Computing Basics', 'QCon', '2023-06-15'),
('Climate Change Models', 'UNConf', '2021-09-20');
`);

console.log("Sample research papers inserted");

//
await connection.execute(`
insert into author_paper (author_id, paper_id)
values
(1, 1),
(1, 2),
(2, 2),
(3, 3);
`);
console.log("Sample author-paper relationships inserted");

await connection.end();
Loading