-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql task.sql
More file actions
61 lines (38 loc) · 1.74 KB
/
sql task.sql
File metadata and controls
61 lines (38 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- get all statuses, not repeating, alphabetically ordered
SELECT DISTINCT status FROM tasks ORDER BY status ASC;
-- get the count of all tasks in each project, order by tasks count descending
SELECT COUNT(*) AS tasksByProject FROM tasks GROUP BY project_id ORDER BY tasksByProject DESC;
-- get the count of all tasks in each project, order by projects names
SELECT COUNT(t.id) FROM tasks t
JOIN projects p ON t.project_id = p.id
GROUP BY p.name ORDER BY p.name ASC;
-- get the tasks for all projects having the name beginning with "N" letter
SELECT t.name FROM tasks t
JOIN projects p ON t.project_id = p.id
WHERE p.name LIKE 'N%';
-- get the list of all projects containing the 'a' letter
-- in the middle of the name, and show the tasks count near each project.
-- Mention that there can exist projects without tasks and tasks with project_id = NULL
SELECT (
SELECT COUNT(*) FROM tasks t WHERE t.project_id = p.id
) AS tasksByProject, p.name FROM projects p
WHERE p.name LIKE '%a%';
-- get the list of tasks with duplicate names. Order alphabetically
SELECT t.name FROM tasks t
GROUP BY t.name
HAVING COUNT(*) > 1
ORDER BY t.name ASC
-- get list of tasks having several exact matches of both name and status, from the project 'Garage'. Order by matches count
SELECT t.name FROM tasks t
JOIN projects p ON t.project_id = p.id
WHERE p.name = 'Garage'
GROUP BY t.name, t.status
HAVING COUNT(*) > 1
ORDER BY COUNT(*);
-- get the list of project names having more than 10 tasks in status 'completed'. Order by project_id
SELECT p.name FROM projects p
JOIN tasks t ON t.project_id = p.id
WHERE t.status = 'completed'
GROUP BY p.name, t.project_id
HAVING COUNT(*) > 10
ORDER BY t.project_id