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

database week1/jianxin #148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
46 changes: 46 additions & 0 deletions databases/week1/database-hw-w1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- 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%");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not block the review because your query works, but I still suggest you follow this advice to get more clear and meaningful code.

You use IN when there is a list of possible values. Although it works in this case, there is only one status for "Done", so you could do SELECT * FROM task WHERE status_id = (...) and this statement would be a bit more clear.

Similarly, in the subquery you are searching for statuses whose name contain done. There is only one status for "Done", so you could do SELECT id FROM status WHERe name = 'Done', which again is a bit more clear.


-- 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The column status_id in the table task cannot be null (this can be checked in the CREATE TABLE statement here). This means that this LEFT JOIN is no different from an INNER JOIN or JOIN (which is the same as an INNER JOIN).

This doesn't block the review either, but I suggest that you use LEFT or RIGHT joins only when they make a difference from an INNER, i.e.: when the column you are joining on can be null.


-- 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;