From 99bf2d09b465d1a65fdaf75c29e607382b60e132 Mon Sep 17 00:00:00 2001 From: Hussam Date: Sat, 30 Aug 2025 20:42:17 +0200 Subject: [PATCH 1/2] Assignment-w2-database --- Week2/Assignment/ Aggregate.js | 18 ++++ Week2/Assignment/Joins.js | 21 ++++ Week2/Assignment/Keys.js | 28 +++++ Week2/Assignment/Relationships.js | 0 Week2/Assignment/connection.js | 0 Week2/Assignment/insert.js | 27 +++++ Week2/Assignment/package-lock.json | 162 +++++++++++++++++++++++++++++ Week2/Assignment/package.json | 15 +++ 8 files changed, 271 insertions(+) create mode 100644 Week2/Assignment/ Aggregate.js create mode 100644 Week2/Assignment/Joins.js create mode 100644 Week2/Assignment/Keys.js create mode 100644 Week2/Assignment/Relationships.js create mode 100644 Week2/Assignment/connection.js create mode 100644 Week2/Assignment/insert.js create mode 100644 Week2/Assignment/package-lock.json create mode 100644 Week2/Assignment/package.json diff --git a/Week2/Assignment/ Aggregate.js b/Week2/Assignment/ Aggregate.js new file mode 100644 index 000000000..1372a7f54 --- /dev/null +++ b/Week2/Assignment/ Aggregate.js @@ -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`); + + +} +catch(err){ + console.error('Error executing query', err.stack); +} finally { + await client.end(); +} +} +runQuery(); \ No newline at end of file diff --git a/Week2/Assignment/Joins.js b/Week2/Assignment/Joins.js new file mode 100644 index 000000000..31300a30d --- /dev/null +++ b/Week2/Assignment/Joins.js @@ -0,0 +1,21 @@ +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);`); + 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,SUM(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(); + } +} \ No newline at end of file diff --git a/Week2/Assignment/Keys.js b/Week2/Assignment/Keys.js new file mode 100644 index 000000000..3e1f2bcb1 --- /dev/null +++ b/Week2/Assignment/Keys.js @@ -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(); \ No newline at end of file diff --git a/Week2/Assignment/Relationships.js b/Week2/Assignment/Relationships.js new file mode 100644 index 000000000..e69de29bb diff --git a/Week2/Assignment/connection.js b/Week2/Assignment/connection.js new file mode 100644 index 000000000..e69de29bb diff --git a/Week2/Assignment/insert.js b/Week2/Assignment/insert.js new file mode 100644 index 000000000..841c4ea81 --- /dev/null +++ b/Week2/Assignment/insert.js @@ -0,0 +1,27 @@ +const {client}=require('./connection'); +async function runQuery(){ + try{ + await client.query(`INSERT INTO author(author_name, university, date_of_birth, h_index, gender) +VALUES +('Alice Smith', 'MIT', '1980-03-15', 35, 'F'), +('Bob Johnson', 'Stanford', '1975-07-22', 40, 'M'), +('Carol Williams', 'Harvard', '1982-11-05', 28, 'F'), +('David Brown', 'Oxford', '1978-01-17', 45, 'M'), +('Emma Davis', 'Cambridge', '1985-09-10', 30, 'F'), +('Frank Miller', 'UCLA', '1979-12-03', 50, 'M'), +('Grace Wilson', 'Yale', '1983-05-25', 33, 'F'), +('Henry Moore', 'Princeton', '1981-08-12', 38, 'M'), +('Ivy Taylor', 'Columbia', '1986-02-20', 27, 'F'), +('Jack Anderson', 'Caltech', '1977-06-30', 42, 'M'), +('Karen Thomas', 'Imperial College', '1984-10-15', 31, 'F'), +('Leo Jackson', 'ETH Zurich', '1980-04-01', 36, 'M'), +('Mia White', 'Sorbonne', '1987-01-11', 29, 'F'), +('Nick Harris', 'Tokyo University', '1976-03-22', 47, 'M'), +('Olivia Martin', 'Seoul National University', '1982-07-18', 34, 'F');`); + + } + catch(err){ + console.error('Connection error',err.stack); + } + + } \ No newline at end of file diff --git a/Week2/Assignment/package-lock.json b/Week2/Assignment/package-lock.json new file mode 100644 index 000000000..0c7207618 --- /dev/null +++ b/Week2/Assignment/package-lock.json @@ -0,0 +1,162 @@ +{ + "name": "assignment", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "assignment", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "pg": "^8.16.3" + } + }, + "node_modules/pg": { + "version": "8.16.3", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.16.3.tgz", + "integrity": "sha512-enxc1h0jA/aq5oSDMvqyW3q89ra6XIIDZgCX9vkMrnz5DFTw/Ny3Li2lFQ+pt3L6MCgm/5o2o8HW9hiJji+xvw==", + "license": "MIT", + "dependencies": { + "pg-connection-string": "^2.9.1", + "pg-pool": "^3.10.1", + "pg-protocol": "^1.10.3", + "pg-types": "2.2.0", + "pgpass": "1.0.5" + }, + "engines": { + "node": ">= 16.0.0" + }, + "optionalDependencies": { + "pg-cloudflare": "^1.2.7" + }, + "peerDependencies": { + "pg-native": ">=3.0.1" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-cloudflare": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.2.7.tgz", + "integrity": "sha512-YgCtzMH0ptvZJslLM1ffsY4EuGaU0cx4XSdXLRFae8bPP4dS5xL1tNB3k2o/N64cHJpwU7dxKli/nZ2lUa5fLg==", + "license": "MIT", + "optional": true + }, + "node_modules/pg-connection-string": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.9.1.tgz", + "integrity": "sha512-nkc6NpDcvPVpZXxrreI/FOtX3XemeLl8E0qFr6F2Lrm/I8WOnaWNhIPK2Z7OHpw7gh5XJThi6j6ppgNoaT1w4w==", + "license": "MIT" + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "license": "ISC", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-pool": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.10.1.tgz", + "integrity": "sha512-Tu8jMlcX+9d8+QVzKIvM/uJtp07PKr82IUOYEphaWcoBhIYkoHpLXN3qO59nAI11ripznDsEzEv8nUxBVWajGg==", + "license": "MIT", + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.10.3", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.10.3.tgz", + "integrity": "sha512-6DIBgBQaTKDJyxnXaLiLR8wBpQQcGWuAESkRBX/t6OwA8YsqP+iVSiond2EDy6Y/dsGk8rh/jtax3js5NeV7JQ==", + "license": "MIT" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "license": "MIT", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "license": "MIT", + "dependencies": { + "split2": "^4.1.0" + } + }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "license": "MIT", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "license": "ISC", + "engines": { + "node": ">= 10.x" + } + }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "license": "MIT", + "engines": { + "node": ">=0.4" + } + } + } +} diff --git a/Week2/Assignment/package.json b/Week2/Assignment/package.json new file mode 100644 index 000000000..d8d5de974 --- /dev/null +++ b/Week2/Assignment/package.json @@ -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" + } +} From 9fe2476fc3c5c82e44507553924d34d29ee82e44 Mon Sep 17 00:00:00 2001 From: Hussam Date: Tue, 2 Sep 2025 19:03:41 +0200 Subject: [PATCH 2/2] Assignment-w2-database --- Week2/Assignment/Joins.js | 9 ---- Week2/Assignment/Relationships.js | 30 ++++++++++++ Week2/Assignment/connection.js | 10 ++++ Week2/Assignment/insert.js | 78 ++++++++++++++++++++++++------- 4 files changed, 102 insertions(+), 25 deletions(-) diff --git a/Week2/Assignment/Joins.js b/Week2/Assignment/Joins.js index 31300a30d..259c94c79 100644 --- a/Week2/Assignment/Joins.js +++ b/Week2/Assignment/Joins.js @@ -3,15 +3,6 @@ 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);`); - 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,SUM(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); diff --git a/Week2/Assignment/Relationships.js b/Week2/Assignment/Relationships.js index e69de29bb..e9f85d010 100644 --- a/Week2/Assignment/Relationships.js +++ b/Week2/Assignment/Relationships.js @@ -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(); \ No newline at end of file diff --git a/Week2/Assignment/connection.js b/Week2/Assignment/connection.js index e69de29bb..b5b3fa915 100644 --- a/Week2/Assignment/connection.js +++ b/Week2/Assignment/connection.js @@ -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; \ No newline at end of file diff --git a/Week2/Assignment/insert.js b/Week2/Assignment/insert.js index 841c4ea81..df0ffd886 100644 --- a/Week2/Assignment/insert.js +++ b/Week2/Assignment/insert.js @@ -1,23 +1,69 @@ const {client}=require('./connection'); async function runQuery(){ try{ - await client.query(`INSERT INTO author(author_name, university, date_of_birth, h_index, gender) + 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'), -('Bob Johnson', 'Stanford', '1975-07-22', 40, 'M'), -('Carol Williams', 'Harvard', '1982-11-05', 28, 'F'), -('David Brown', 'Oxford', '1978-01-17', 45, 'M'), -('Emma Davis', 'Cambridge', '1985-09-10', 30, 'F'), -('Frank Miller', 'UCLA', '1979-12-03', 50, 'M'), -('Grace Wilson', 'Yale', '1983-05-25', 33, 'F'), -('Henry Moore', 'Princeton', '1981-08-12', 38, 'M'), -('Ivy Taylor', 'Columbia', '1986-02-20', 27, 'F'), -('Jack Anderson', 'Caltech', '1977-06-30', 42, 'M'), -('Karen Thomas', 'Imperial College', '1984-10-15', 31, 'F'), -('Leo Jackson', 'ETH Zurich', '1980-04-01', 36, 'M'), -('Mia White', 'Sorbonne', '1987-01-11', 29, 'F'), -('Nick Harris', 'Tokyo University', '1976-03-22', 47, 'M'), -('Olivia Martin', 'Seoul National University', '1982-07-18', 34, 'F');`); +('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){