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
22 changes: 22 additions & 0 deletions Week1/databases/connectDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pkg from 'pg';
const { Client } = pkg;

// Reusable function for connecting
export async function connectDB() {
const client = new Client({
user: 'hyfuser',
host: 'localhost',
database: 'world',
password: 'hyfpassword',
port: 5432,
});

try {
await client.connect();
console.log(`Connected to database: ${client.database}`);
return client; // return client so queries can be made outside
} catch (error) {
console.error('Connection error:', error);
throw error;
}
}
102 changes: 102 additions & 0 deletions Week1/databases/setupMeetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import pkg from 'pg';
const { Client } = pkg;

// Connect to default "postgres" database to create/drop meetup
const defaultClient = new Client({
user: 'hyfuser',
host: 'localhost',
database: 'postgres', // connect to default DB first
password: 'hyfpassword', // update this
port: 5432,
});

async function setupDatabase() {
try {
await defaultClient.connect();

// Drop and create the meetup database
await defaultClient.query(`DROP DATABASE IF EXISTS meetup;`);
await defaultClient.query(`CREATE DATABASE meetup;`);
console.log('Database "meetup" created.');

await defaultClient.end();

// Connect to the newly created meetup database
const client = new Client({
user: 'hyfuser',
host: 'localhost',
database: 'meetup',
password: 'hyfpassword', // update this
port: 5432,
});
await client.connect();

// Create tables
await client.query(`
CREATE TABLE Invitee (
invitee_no INT,
invitee_name VARCHAR(100),
invited_by VARCHAR(100)
);
`);

await client.query(`
CREATE TABLE Room (
room_no INT,
room_name VARCHAR(64),
floor_number INT
);
`);

await client.query(`
CREATE TABLE Meeting (
meeting_no INT,
meeting_title VARCHAR(64),
starting_time TIMESTAMP,
ending_time TIMESTAMP,
room_no INT
);
`);

console.log('Tables created.');

// Insert sample rows
await client.query(`
INSERT INTO Invitee (invitee_no, invitee_name, invited_by) VALUES
(1, 'Alice Johnson', 'Bob Smith'),
(2, 'Bob Smith', 'Carol White'),
(3, 'Carol White', 'David Lee'),
(4, 'David Lee', 'Alice Johnson'),
(5, 'Eve Brown', 'Bob Smith');
`);

await client.query(`
INSERT INTO Room (room_no, room_name, floor_number) VALUES
(101, 'Blue Room', 1),
(102, 'Green Room', 1),
(201, 'Yellow Room', 2),
(202, 'Red Room', 2),
(301, 'Conference Hall', 3);
`);

await client.query(`
INSERT INTO Meeting (meeting_no, meeting_title, starting_time, ending_time, room_no) VALUES
(1, 'Project Kickoff', '2025-09-01 09:00:00', '2025-09-01 10:00:00', 101),
(2, 'Design Review', '2025-09-02 11:00:00', '2025-09-02 12:30:00', 102),
(3, 'Team Standup', '2025-09-03 09:30:00', '2025-09-03 10:00:00', 201),
(4, 'Client Presentation', '2025-09-04 14:00:00', '2025-09-04 15:30:00', 202),
(5, 'Retrospective', '2025-09-05 16:00:00', '2025-09-05 17:00:00', 301);
`);

console.log('Data inserted.');

await client.end();
console.log('Setup complete.');

} catch (err) {
console.error('Error:', err);
await defaultClient.end();
}
}

setupDatabase();
6 changes: 3 additions & 3 deletions Week1/databases/world.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
SET standard_conforming_strings = on;

-- Connect to the world database or create it
-- DROP DATABASE IF EXISTS world; -- Uncomment if needed
-- CREATE DATABASE world;
-- \c world;
DROP DATABASE IF EXISTS world; -- Uncomment if needed
CREATE DATABASE world;
\c world;

BEGIN;

Expand Down
94 changes: 94 additions & 0 deletions Week1/databases/worldStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { connectDB } from './connectDatabase.js';

async function DbQueries() {
const client = await connectDB();
try {
// 1. What are the names of countries with population greater than 8 million?
const result1 = await client.query(`
SELECT name
FROM country
WHERE population > 8000000;
`);
console.log('Countries with population more than 8M:', result1.rows);

// 2. What are the names of countries that have "land" in their names?
const result2 = await client.query(`
SELECT name
FROM country
WHERE name ILIKE '%land%';
`);
console.log('Countries with "land" in their names:', result2.rows);

// 3. What are the names of the cities with population in between 500,000 and 1 million?
const result3 = await client.query(`
SELECT name
FROM CITY
WHERE population BETWEEN 500000 AND 1000000;
`)
console.log('Cities with population between 500,000 and 1 million:', result3.rows);

// 4. What's the name of all the countries on the continent 'Europe'?
const result4 = await client.query(`
SELECT name
FROM country
WHERE continent = 'Europe'
`)
console.log('Countries in Europe continent:', result4.rows)

// 5. List all the countries in the descending order of their surface areas.
const result5 = await client.query(`
SELECT name
FROM country
ORDER BY surfacearea DESC
`)
console.log('countries in the descending order of their surface areas:', result5.rows)

// 6. What are the names of all the cities in the Netherlands?
const result6 = await client.query(`
SELECT name
FROM city
WHERE countrycode = 'NLD'
`)
console.log('All cities in the Netherlands:', result6.rows)

// 7. What is the population of Rotterdam?
const result7 = await client.query(`
SELECT population
FROM city
WHERE name = 'Rotterdam'
`)
console.log('Population of Rotterdam is:', result7.rows)

// 8. What's the top 10 countries by Surface Area?
const result8 = await client.query(`
SELECT name
FROM country
ORDER BY surfacearea DESC
LIMIT 10
`)
console.log('Top 10 countries by Surface Area:', result8.rows)

// 9. What's the top 10 most populated cities?
const result9 = await client.query(`
SELECT name
FROM city
ORDER BY population DESC
LIMIT 10;
`)
console.log('Top 10 most populated cities:', result9.rows)

// 10. What is the population number of the world?
const result10 = await client.query(`
SELECT SUM(population)
FROM country
`)
console.log('The population number of the world is:', result10.rows)
} catch (err) {
console.error('Error running queries:', err);
} finally {
await client.end();
console.log('Connection closed.');
}
}

DbQueries();
6 changes: 6 additions & 0 deletions Week2/assignment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_Store
11 changes: 11 additions & 0 deletions Week2/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Week 2 Assignment

## Quick Setup

1. Install dependencies:
```bash
npm install

2. Run one command:
```bash
node main.js
37 changes: 37 additions & 0 deletions Week2/assignment/connectDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pkg from 'pg';
const { Client } = pkg;

export async function setupDatabase(dropAndCreate = false) {
// Connect to default database
const defaultClient = new Client({
user: 'hyfuser',
host: 'localhost',
database: 'postgres',
password: 'hyfpassword',
port: 5432,
});

await defaultClient.connect();
console.log('Connected to default database.');

if (dropAndCreate) {
await defaultClient.query(`DROP DATABASE IF EXISTS research_db;`);
await defaultClient.query(`CREATE DATABASE research_db;`);
console.log('Database "research_db" created.');
}

await defaultClient.end();

// Connect to research_db
const client = new Client({
user: 'hyfuser',
host: 'localhost',
database: 'research_db',
password: 'hyfpassword',
port: 5432,
});

await client.connect();
console.log('Connected to "research_db".');
return client;
}
48 changes: 48 additions & 0 deletions Week2/assignment/exercise1_keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { setupDatabase } from './connectDatabase.js';

// Create authors table
async function createAuthorsTable() {
const client = await setupDatabase();
try {
await client.query(`
CREATE TABLE IF NOT EXISTS authors (
author_id SERIAL PRIMARY KEY,
author_name VARCHAR(100),
university VARCHAR(100),
date_of_birth DATE,
h_index INT,
gender VARCHAR(10)
);
`);
console.log('Table "authors" created successfully!');
} catch (error) {
console.error('Error creating table:', error);
} finally {
await client.end();
}
}

// Add mentor column
async function addMentorColumn() {
const client = await setupDatabase();
try {
await client.query(`
ALTER TABLE authors
ADD COLUMN mentor INT REFERENCES authors(author_id);
`);
console.log('Column "mentor" added successfully!');
} catch (error) {
console.error('Error adding mentor column:', error);
} finally {
await client.end();
}
}

export async function main1() {
// Drop & recreate database for a fresh start
const client1 = await setupDatabase(true);
await client1.end();
await createAuthorsTable();
await addMentorColumn();
console.log('3.1. Exercise 1: Keys completed successfully!');
}
Loading