From 45c9884a84339976b57986525a92c04e8564083b Mon Sep 17 00:00:00 2001 From: Jianxin Date: Wed, 5 Feb 2025 15:17:58 +0100 Subject: [PATCH 1/3] homework w1 --- databases/week1/database-hw-w1.sql | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 databases/week1/database-hw-w1.sql diff --git a/databases/week1/database-hw-w1.sql b/databases/week1/database-hw-w1.sql new file mode 100644 index 0000000..5596fc3 --- /dev/null +++ b/databases/week1/database-hw-w1.sql @@ -0,0 +1,50 @@ +SELECT * FROM users; +SELECT * FROM task; +SELECT * FROM status; + +-- Find out how many tasks are in the task table +SELECT count(id) FROM task; + +-- Find out how many tasks in the task table do not have a valid due date +SELECT count(id) FROM task +WHERE due_date is NULL; + +-- Find all the tasks that are marked as done +SELECT * FROM task +WHERE status_id IN (SELECT id FROM status WHERE name LIKE "%done%"); + +-- Find all the tasks that are not marked as done +SELECT * FROM task +WHERE status_id NOT IN (SELECT id FROM status WHERE name LIKE "%done%"); + +-- Get all the tasks, sorted with the most recently created first +SELECT * FROM task +ORDER BY created DESC; + +-- Get the single most recently created task +SELECT * FROM task +ORDER BY created DESC +LIMIT 1; + +-- 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%"; + +-- Get the title and status (as text) of all tasks +SELECT task.title AS task_title, status.name AS task_status +FROM task +LEFT JOIN status on task.status_id = status.id; + +-- Get the name of each status, along with a count of how many tasks have that status +SELECT status.name AS task_status, count(task.title) AS task_count +FROM task +LEFT JOIN status on task.status_id = status.id +GROUP BY task_status; + +-- Get the names of all statuses, sorted by the status with most tasks first +SELECT status.name AS task_status, count(task.title) AS task_count +FROM task +LEFT JOIN status on task.status_id = status.id +GROUP BY task_status +ORDER BY task_count DESC; From 7c6b922564d5292384cd82bc05ec4ce91af6bc43 Mon Sep 17 00:00:00 2001 From: Jianxin Date: Wed, 5 Feb 2025 15:47:58 +0100 Subject: [PATCH 2/3] homework update --- databases/week1/database-hw-w1.sql | 4 ---- 1 file changed, 4 deletions(-) diff --git a/databases/week1/database-hw-w1.sql b/databases/week1/database-hw-w1.sql index 5596fc3..1ded520 100644 --- a/databases/week1/database-hw-w1.sql +++ b/databases/week1/database-hw-w1.sql @@ -1,7 +1,3 @@ -SELECT * FROM users; -SELECT * FROM task; -SELECT * FROM status; - -- Find out how many tasks are in the task table SELECT count(id) FROM task; From e8eb589d555ed4003ef41154fd9ed85d2e751aa0 Mon Sep 17 00:00:00 2001 From: Jianxin Date: Sat, 8 Feb 2025 22:40:46 +0100 Subject: [PATCH 3/3] update after review --- databases/week1/database-hw-w1.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/databases/week1/database-hw-w1.sql b/databases/week1/database-hw-w1.sql index 1ded520..09b9a65 100644 --- a/databases/week1/database-hw-w1.sql +++ b/databases/week1/database-hw-w1.sql @@ -7,11 +7,11 @@ WHERE due_date is NULL; -- Find all the tasks that are marked as done SELECT * FROM task -WHERE status_id IN (SELECT id FROM status WHERE name LIKE "%done%"); +WHERE status_id = (SELECT id FROM status WHERE name = 'Done'); -- Find all the tasks that are not marked as done SELECT * FROM task -WHERE status_id NOT IN (SELECT id FROM status WHERE name LIKE "%done%"); +WHERE status_id NOT IN (SELECT id FROM status WHERE name = 'Done'); -- Get all the tasks, sorted with the most recently created first SELECT * FROM task @@ -30,17 +30,17 @@ OR description LIKE "%database%"; -- Get the title and status (as text) of all tasks SELECT task.title AS task_title, status.name AS task_status FROM task -LEFT JOIN status on task.status_id = status.id; +INNER JOIN status on task.status_id = status.id; -- Get the name of each status, along with a count of how many tasks have that status SELECT status.name AS task_status, count(task.title) AS task_count FROM task -LEFT JOIN status on task.status_id = status.id +INNER JOIN status on task.status_id = status.id GROUP BY task_status; -- Get the names of all statuses, sorted by the status with most tasks first SELECT status.name AS task_status, count(task.title) AS task_count FROM task -LEFT JOIN status on task.status_id = status.id +INNER JOIN status on task.status_id = status.id GROUP BY task_status ORDER BY task_count DESC;