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
94 changes: 94 additions & 0 deletions Week1/Hadidreem17/setup_meetup_db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const { Client } = require("pg");

async function main() {
const client = new Client({
user: "hyfuser",
host: "localhost",
database: "postgres",
password: "hyfpassword",
port: 5432,
});

await client.connect();

console.log("Dropping database if exists...");
await client.query("DROP DATABASE IF EXISTS meetup");

console.log("Creating database meetup...");
await client.query("CREATE DATABASE meetup");

await client.end();


const meetupClient = new Client({
user: "hyfuser",
host: "localhost",
database: "meetup",
password: "hyfpassword",
port: 5432,
});

await meetupClient.connect();

console.log("Creating tables...");

await meetupClient.query(`
CREATE TABLE Invitee (
invitee_no SERIAL PRIMARY KEY,
invitee_name VARCHAR(100),
invited_by VARCHAR(100)
);
`);

await meetupClient.query(`
CREATE TABLE Room (
room_no SERIAL PRIMARY KEY,
room_name VARCHAR(100),
floor_number INT
);
`);

await meetupClient.query(`
CREATE TABLE Meeting (
meeting_no SERIAL PRIMARY KEY,
meeting_title VARCHAR(200),
starting_time TIMESTAMP,
ending_time TIMESTAMP,
room_no INT REFERENCES Room(room_no)
);
`);

console.log("Inserting sample data...");

await meetupClient.query(`
INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Sara', 'Ali'),
('John', 'Lina'),
('Maya', 'Omar'),
('Noor', 'Samir'),
('Adam', 'Lara');ssss
`);

await meetupClient.query(`
INSERT INTO Room (room_name, floor_number) VALUES
('Blue Room', 1),
('Red Room', 2),
('Green Room', 1),
('Orange Room', 3),
('VIP Room', 5);
`);

await meetupClient.query(`
INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('Tech Meetup', NOW(), NOW() + INTERVAL '2 hours', 1),
('Startup Pitch', NOW(), NOW() + INTERVAL '1 hour', 2),
('Workshop JS', NOW(), NOW() + INTERVAL '3 hours', 3),
('Design Basics', NOW(), NOW() + INTERVAL '4 hours', 4),
('AI Conference', NOW(), NOW() + INTERVAL '5 hours', 5);
`);

console.log("All done");
await meetupClient.end();
}

main();
83 changes: 83 additions & 0 deletions Week1/Hadidreem17/world_queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const { Client } = require("pg");

async function main() {
const client = new Client({
user: "hyfuser",
host: "localhost",
database: "world",
password: "hyfpassword",
port: 5432,
});

try {
await client.connect();

console.log("\n1. Countries with population > 8 million:");
let result = await client.query(`
SELECT name FROM country WHERE population > 8000000;
`);
console.log(result.rows);

console.log("\n2. Countries that contain 'land' in their names:");
result = await client.query(`
SELECT name FROM country WHERE name ILIKE '%land%';
`);
console.log(result.rows);

console.log("\n3. Cities with population between 500k and 1 million:");
result = await client.query(`
SELECT name FROM city
WHERE population BETWEEN 500000 AND 1000000;
`);
console.log(result.rows);

console.log("\n4. Countries in Europe:");
result = await client.query(`
SELECT name FROM country WHERE continent = 'Europe';
`);
console.log(result.rows);

console.log("\n5. Countries ordered by surface area DESC:");
result = await client.query(`
SELECT name, surfacearea FROM country ORDER BY surfacearea DESC;
`);
console.log(result.rows);

console.log("\n6. All cities in the Netherlands:");
result = await client.query(`
SELECT name FROM city WHERE countrycode = 'NLD';
`);
console.log(result.rows);

console.log("\n7. Population of Rotterdam:");
result = await client.query(`
SELECT population FROM city WHERE name = 'Rotterdam';
`);
console.log(result.rows);

console.log("\n8. Top 10 countries by surface area:");
result = await client.query(`
SELECT name, surfacearea FROM country ORDER BY surfacearea DESC LIMIT 10;
`);
console.log(result.rows);

console.log("\n9. Top 10 most populated cities:");
result = await client.query(`
SELECT name, population FROM city ORDER BY population DESC LIMIT 10;
`);
console.log(result.rows);

console.log("\n10. Total population of the world:");
result = await client.query(`
SELECT SUM(population) AS world_population FROM country;
`);
console.log(result.rows);

} catch (err) {
console.error("Error while running queries:", err);
} finally {
await client.end();
}
}

main();
Loading