Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs-week2/yuusuf #230

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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 databases/week1/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- 1. Find out how many tasks are in the task table
SELECT count(*)
from task;


-- 2. Find out how many tasks in the task table do not have a valid due date
SELECT count (*)
from task
WHERE due_date is NULL;


-- 3. Find all the tasks that are marked as done
SELECT *
FROM task
WHERE status_id = 3; -- 3 is equal to done


-- 4. Find all the tasks that are not marked as done
SELECT *
FROM task
WHERE status_id <> 3; -- numbers that are not 3 are either not started or in progress


-- 5. Get all the tasks, sorted with the most recently created first
SELECT *
from task
ORDER BY CREATED DESC;


-- 6. Get the single most recently created task
SELECT *
from task
ORDER BY CREATED DESC limit 1;


-- 7. Get the title and due date of all tasks where the title or description contains database
SELECT title,due_date
from task
WHERE title like '%database%' OR description like '%database%';


-- 8. Get the title and status (as text) of all tasks
SELECT task.title AS tt, status.name AS sn
FROM task
JOIN status ON task.status_id = status.id;


-- 9. Get the name of each status, along with a count of how many tasks have that status
SELECT status.name AS sn, count(task.title) AS tc
FROM task
JOIN status
WHERE task.status_id = status.id
GROUP BY status.id;


-- 10. Get the names of all statuses, sorted by the status with most tasks first
SELECT status.name AS sn, count(task.title) AS tc
FROM task
JOIN status
WHERE task.status_id = status.id
GROUP BY status.id
ORDER BY tc DESC;
Binary file added databases/week2/ERD diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions databases/week2/task1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#Add a task with these attributes: title, description, created, updated, due_date, status_id, user_id
INSERT INTO task (title, description, created, updated, due_date, status_id, user_id)
VALUES ('read a book', 'complete human laws of nature', '2024-12-01', '2024-12-18', '2025-1-01', 2, 3);


#Change the title of a task
update task
set title = 'wash car'
where id = 36;


#Change a task due date
update task
set due_date = '2026-12-3'
where id = 36;


#Change a task status
update task
set status_id = 3
where id = 16;


#Mark a task as complete
update task
set status_id = 1
where id = 3;


# Delete a task
delete from task
where user_id = 1;
29 changes: 29 additions & 0 deletions databases/week2/task2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
create database School;
use School;


CREATE TABLE Class (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
beginning_date DATE,
ending_date DATE
);


CREATE TABLE Student (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
class_id INT NOT NULL,
FOREIGN KEY (class_id) REFERENCES Class(id)
);


#Create an index on the name column of the student table.
CREATE INDEX indexName ON Student(name);


#Add a new column to the class table named status which can only have the following values: not-started, ongoing, finished (hint: enumerations).
ALTER TABLE Class
ADD status ENUM('not-started', 'ongoing', 'finished');
28 changes: 28 additions & 0 deletions databases/week2/task3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--Get all the tasks assigned to users whose email ends in @spotify.com
select * from task
join user_task on task_id = user_task.task_id
join user on user_task.task_id = user.id
where user.email like '%@spotify.com';


--Get all the tasks for 'Donald Duck' with status 'Not started'
SELECT *
FROM task
JOIN user_task ON task.id = user_task.task_id
join user on user_id = user.id
JOIN status on status_id = status.id
where user.name = 'Donald Duck' and status.name = 'not started';


--Get all the tasks for 'Maryrose Meadows' that were created in september (hint: month(created)=month_number)
select * from task
join user_task on task.id = user_task.task_id
join user on user.id = user_task.user_id
where user.name = 'Maryrose Meadows' and month(created) = 9;


--Find how many tasks where created in each month, e.g. how many tasks were created in october, how many tasks were created in november, etc. (hint: use group by)
SELECT MONTH(created) AS month,
COUNT(*) AS task_amount
FROM task
GROUP BY month;
32 changes: 32 additions & 0 deletions databases/week2/task4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
address VARCHAR(255)
);


CREATE TABLE Products (
product_id INT PRIMARY KEY,
name VARCHAR(255),
price INT,
description VARCHAR(255)
);


CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);


CREATE TABLE Order_Items (
order_item_id INT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);
48 changes: 48 additions & 0 deletions databases/week3/additional_queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Get meals that has a price smaller than a specific price fx 90
SELECT * from meal where price < 70;



-- Get meals that still has available reservations
SELECT meal.* FROM meal
LEFT JOIN reservation ON reservation.meal_id = meal.id
GROUP BY meal.id
HAVING COUNT(reservation.id) < meal.max_reservations;



-- Get meals that partially match a title. Rød grød med will match the meal with the title Rød grød med fløde
SELECT * from meal where title LIKE '%rød grød med fløde%';



-- Get meals that has been created between two dates
SELECT * FROM meal WHERE created_date BETWEEN '2025-01-01' AND '2025-2-13';



-- Get only specific number of meals fx return only 5 meals
SELECT * from meal limit 2;



-- Get the meals that have good reviews
SELECT meal.title AS meal,meal.description,meal.location,review.title as review,review.description,review.stars from meal
JOIN review on review.meal_id = meal.id
WHERE review.stars >= 4



-- Get reservations for a specific meal sorted by created_date
SELECT * FROM meal
join reservation on reservation.meal_id = meal.id
where meal.id = 1
order by reservation.created_date



-- Sort all meals by average number of stars in the reviews
SELECT meal.* ,avg(review.stars) as average_rate from meal
join review on review.meal_id = meal.id
group by meal.id
order by average_rate desc;
74 changes: 74 additions & 0 deletions databases/week3/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
CREATE DATABASE meal_sharing;
USE meal_sharing;


--created the tables
CREATE TABLE meal (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`location` VARCHAR(255) NOT NULL,
`when` DATETIME NOT NULL,
`max_reservations` INT NOT NULL,
`price` DECIMAL(10, 0) NOT NULL,
`created_date` DATE NOT NULL
)


CREATE TABLE reservation (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`number_of_guests` INT NOT NULL,
`meal_id` INT NOT NULL,
`created_date` DATE NOT NULL,
`contact_phonenumber` VARCHAR(255) NOT NULL,
`contact_name` VARCHAR(255) NOT NULL,
`contact_email` VARCHAR(255) NOT NULL,
FOREIGN KEY (meal_id) REFERENCES meal(id)
)


CREATE TABLE review (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`meal_id` INT NOT NULL,
`stars` INT NOT NULL,
`created_date` DATE NOT NULL,
FOREIGN KEY (meal_id) REFERENCES meal(id)
)





--data insertion for meal table
insert into meal (title,description,location,`when`,max_reservations,price,created_date)
VALUES ('Lasagna', 'Enjoy a delicious lasagna', 'Rome', '2025-05-01 10:00:00', 10, 50, '2025-02-15');

insert into meal (title,description,location,`when`,max_reservations,price,created_date)
VALUES ('Chicken biryani', 'Indian biryani', 'Mumbai', '2025-06-13 7:30:00', 8, 70, '2025-02-07');

insert into meal (title,description,location,`when`,max_reservations,price,created_date)
VALUES ('Samosa', 'Homemade Somali samosa', 'Mogadishu', '2025-07-25 22:14:00', 4, 100, '2025-02-22');


--data insertion for reservation table
INSERT INTO reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES (3, 1, '2025-04-02', '12345678', 'Benjamin Jensen', '[email protected]');

INSERT INTO reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES (5,2, '2025-03-13', '11223344', 'Mohamed Ali', '[email protected]');

INSERT INTO reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES (4, 3, '2025-06-24', '87654321', 'Leyla Abdirahman', '[email protected]');


--data insertion for review table
INSERT INTO review (title, description, meal_id, stars, created_date)
VALUES ('Average', 'The meal was okay, but could be better.', 1, 3, '2025-05-02');

INSERT INTO review (title, description, meal_id, stars, created_date)
VALUES ('Delicious Experience', 'The flavors were amazing!', 2, 4, '2025-06-18');

INSERT INTO review (title, description, meal_id, stars, created_date)
VALUES ('Great Meal', 'Loved the food!', 3, 4, '2025-07-16');
20 changes: 20 additions & 0 deletions databases/week3/meal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--Get all meals
SELECT * FROM meal;


--Add a new meal
insert into meal (title,description,location,`when`,max_reservations,price,created_date)
VALUES ('Tacos', 'Mexican tacos', 'Mexico', '2025-09-01 15:30:00', 3, 25, '2025-02-26');


--Get a meal with any id, fx 1
SELECT * FROM meal WHERE id = 2;


--Update a meal with any id, fx 1. Update any attribute fx the title or multiple attributes
UPDATE meal set location = 'Pakistan' WHERE id = 2;


--Delete a meal with any id, fx 1
DELETE from meal WHERE id = 4;

24 changes: 24 additions & 0 deletions databases/week3/reservation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--Get all reservations
SELECT * FROM reservation;


--Add a new reservation
INSERT INTO reservation (number_of_guests, meal_id, created_date, contact_phonenumber, contact_name, contact_email)
VALUES (6, 1, '2025-03-15', '34567890', 'Qays Qayser', '[email protected]');


--Get a reservation with any id, fx 1
SELECT * FROM reservation WHERE id = 3;


--Update a reservation with any id, fx 1. Update any attribute fx the title or multiple attributes
UPDATE reservation set contact_phonenumber = 34343434 WHERE id = 3;


--Delete a reservation with any id, fx 1
DELETE from reservation WHERE id = 4;





24 changes: 24 additions & 0 deletions databases/week3/review.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--Get all reviews
SELECT * FROM review;


--Add a new review
INSERT INTO review (title, description, meal_id, stars, created_date)
VALUES ('Delicious biryani', 'The flavors were extraordinary!', 2, 5,'2025-06-19' );


--Get a review with any id, fx 1
SELECT * FROM review WHERE id = 2;


--Update a review with any id, fx 1. Update any attribute fx the title or multiple attributes
UPDATE review set title = 'Excellent' WHERE id = 3;


--Delete a review with any id, fx 1
DELETE from review WHERE id = 4;





Loading