diff --git a/Week1/databases/meetup/index.js b/Week1/databases/meetup/index.js new file mode 100644 index 000000000..1c92760fa --- /dev/null +++ b/Week1/databases/meetup/index.js @@ -0,0 +1,73 @@ +const { Client } = require('pg'); +async function main(){ +const client = new Client({ + user: 'hyfuser', + host: 'localhost', + database: 'meetup', + password: 'hyfpassword', + port: 5432, +}); +try{ + await client.connect(); + console.log("Connected successfully to the database"); + const createInviteeTableQuery=`CREATE TABLE IF NOT EXISTS invitee( + invitee_no SERIAL PRIMARY KEY, + invitee_name VARCHAR(255) NOT NULL, + invitee_by VARCHAR(255) NOT NULL);`; + + const creatRoomTableQuery=`CREATE TABLE IF NOT EXISTS room( + room_no SERIAL PRIMARY KEY, + room_name VARCHAR(255) NOT NULL, + floor_number SMALLINT NOT NULL);`; + + const creatMeetingTableQuery=`CREATE TABLE IF NOT EXISTS meeting( + meeting_no SERIAL PRIMARY KEY, + meeting_title VARCHAR(255) NOT NULL, + start_time TIMESTAMP NOT NULL, + end_time TIMESTAMP NOT NULL, + room_no INT NOT NULL REFERENCES room(room_no));`; + + await client.query(createInviteeTableQuery); + await client.query(creatRoomTableQuery); + await client.query(creatMeetingTableQuery); + console.log("Tables created successfully"); + + const insertInviteeQuery = ` + INSERT INTO Invitee (invitee_name,INVITEE_BY ) VALUES + ('Alice', 'John'), + ('Bob', 'Jane'), + ('Charlie', 'John'), + ('David', 'Alice'), + ('Eve', 'Bob'); + `; + const insertRoomQuery = ` + INSERT INTO room (room_name, floor_number) VALUES + ('Boardroom A', 1), + ('Conference Room 201', 2), + ('Boardroom B', 1), + ('Huddle Room 301', 3), + ('Main Hall', 1); + `; + const insertMeetingsQuery = ` + INSERT INTO Meeting (meeting_title, start_time, end_time, room_no) VALUES + ('Project Kickoff', '2025-08-25 09:00:00', '2025-08-25 10:30:00', 1), + ('Marketing Strategy', '2025-08-26 11:00:00', '2025-08-26 12:00:00', 3), + ('Quarterly Review', '2025-08-27 14:00:00', '2025-08-27 15:00:00', 5), + ('Team Sync', '2025-08-28 10:00:00', '2025-08-28 10:30:00', 2), + ('Client Demo', '2025-08-29 16:00:00', '2025-08-29 17:00:00', 4); + `; + await client.query(insertInviteeQuery); + await client.query(insertRoomQuery); + await client.query(insertMeetingsQuery); + console.log("Sample data inserted successfully"); + + +}catch(err){ + console.error("Error during database operation:", err); +} +finally{ + await client.end(); + console.log("Database connection closed") +} +} +main(); \ No newline at end of file diff --git a/Week1/databases/meetup/world.js b/Week1/databases/meetup/world.js new file mode 100644 index 000000000..cce8ebac1 --- /dev/null +++ b/Week1/databases/meetup/world.js @@ -0,0 +1,73 @@ +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("Connected successfully to the database"); + + + const selectNamesOfCountryQuery = `SELECT name FROM country WHERE population > 8000000;`; + const res = await client.query(selectNamesOfCountryQuery); + console.log(res.rows); + + + const selectNamesOfCountry = `SELECT name FROM country WHERE name LIKE '%land%';`; + const res1 = await client.query(selectNamesOfCountry); + console.log(res1.rows); + + + const selectNamesOfCity = `SELECT name FROM city WHERE population BETWEEN 500000 AND 1000000;`; + const res2 = await client.query(selectNamesOfCity); + console.log(res2.rows); + + + const selectCountriesOfContinent = `SELECT name FROM country WHERE continent='Europe';`; + const res3 = await client.query(selectCountriesOfContinent); + console.log(res3.rows); + + + const selectCountriesOrderBySurfaceArea = `SELECT * FROM country ORDER BY surfacearea DESC;`; + const res4 = await client.query(selectCountriesOrderBySurfaceArea); + console.log(res4.rows); + + + const selectCitiesNamesOfCountry = `SELECT name FROM city WHERE countrycode='NLD';`; + const res5 = await client.query(selectCitiesNamesOfCountry); + console.log(res5.rows); + + + const selectPopulationOfCity = `SELECT population FROM city WHERE name='Rotterdam';`; + const res6 = await client.query(selectPopulationOfCity); + console.log(res6.rows); + + + const selectTopCountriesBySurfaceArea = `SELECT name, surfacearea FROM country ORDER BY surfacearea DESC LIMIT 10;`; + const res7 = await client.query(selectTopCountriesBySurfaceArea); + console.log(res7.rows); + + + const selectTopPopulatedCities = `SELECT name, population FROM city ORDER BY population DESC LIMIT 10;`; + const res8 = await client.query(selectTopPopulatedCities); + console.log(res8.rows); + + + const selectPopulationOfWorld = `SELECT SUM(population) AS total_population FROM country;`; + const res9 = await client.query(selectPopulationOfWorld); + console.log(res9.rows); + + } catch (err) { + console.error('Error executing query', err.stack); + } finally { + await client.end(); + } +} + +main();