Tasks Count in the Weekend - Problem

You are given a Tasks table with information about task submissions.

Write a SQL query to report:

  • The number of tasks submitted during the weekend (Saturday, Sunday) as weekend_cnt
  • The number of tasks submitted during the working days (Monday-Friday) as working_cnt

Return the result in any order.

Table Schema

Tasks
Column Name Type Description
task_id PK int Primary key, unique task identifier
assignee_id int ID of the person assigned to the task
submit_date date Date when the task was submitted
Primary Key: task_id
Note: Each row represents a task submission with its assignee and submission date

Input & Output

Example 1 — Mixed Weekend and Working Day Submissions
Input Table:
task_id assignee_id submit_date
1 1 2022-06-04
2 1 2022-06-06
3 2 2022-06-05
Output:
weekend_cnt working_cnt
2 1
💡 Note:

Task 1 submitted on 2022-06-04 (Saturday), Task 2 on 2022-06-06 (Monday), and Task 3 on 2022-06-05 (Sunday). So we have 2 weekend submissions and 1 working day submission.

Example 2 — All Working Days
Input Table:
task_id assignee_id submit_date
4 3 2022-06-07
5 4 2022-06-08
6 5 2022-06-09
Output:
weekend_cnt working_cnt
0 3
💡 Note:

All tasks were submitted on working days: Tuesday, Wednesday, and Thursday. Result shows 0 weekend submissions and 3 working day submissions.

Example 3 — All Weekend Submissions
Input Table:
task_id assignee_id submit_date
7 6 2022-06-11
8 7 2022-06-12
Output:
weekend_cnt working_cnt
2 0
💡 Note:

Both tasks were submitted on weekend days: Saturday and Sunday. Result shows 2 weekend submissions and 0 working day submissions.

Constraints

  • 1 ≤ task_id ≤ 10000
  • 1 ≤ assignee_id ≤ 1000
  • submit_date is a valid date

Visualization

Tap to expand
Tasks Count in the Weekend INPUT id task_name submit_date 1 Task A 2024-01-06 2 Task B 2024-01-08 3 Task C 2024-01-07 4 Task D 2024-01-09 5 Task E 2024-01-13 Day Classification: WEEKEND Sat, Sun WEEKDAY Mon-Fri 6,7,13 8,9 ALGORITHM STEPS 1 Extract Day of Week Use DAYOFWEEK() or DATENAME(weekday) 2 Classify Each Day Sat/Sun = Weekend Mon-Fri = Working 3 Use CASE Statement CASE WHEN day IN (1,7) THEN 'Weekend' 4 COUNT and GROUP BY Aggregate results by day type category SELECT CASE WHEN... GROUP BY day_type COUNT (*) FINAL RESULT day_type count Weekend 3 Working 2 Visual Summary 60% 40% Weekend: 3 Working: 2 Key Insight: Use DAYOFWEEK() function to extract the day number (1=Sunday, 7=Saturday in MySQL). Then use CASE WHEN with GROUP BY to categorize and count tasks by day type efficiently. TutorialsPoint - Tasks Count in the Weekend | Optimal Solution
Asked in
Amazon 23 Facebook 18 Google 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen