Ivy Professional School
Rating
Learning/SQL Data Drill #16

Daily Employee Salary Calculation

Last updated

Calculate daily salary of each employee based on login-logout swipe activity and hourly salary rate.

SQLIntermediateWindow FunctionsLEADCTETIMESTAMPDIFFDate ConversionSalary Analysis

Your Objective

Calculate daily salary of each employee based on login-logout swipe activity and hourly salary rate.

The Setup

Your dataset contains two employee attendance and compensation tables: 1. A Salary table with employee name and hourly salary rate 2. A Swipe table with login and logout swipe activity timestamps Analyze employee swipe activity and calculate salary earned by each employee on a daily basis.

Table Schema

Salary
ColumnTypeKey / Notes
employee_idINT
nameVARCHAR(100)
salary_per_hour_$FLOAT
Swipe
ColumnTypeKey / Notes
employee_idINTFK
activity_typeVARCHAR(10)
activity_timeDATETIME

Sample Data

Salary — 4 rows

employee_idnamesalary_per_hour_$
1Jeremy16.78
2Nicole25.87
3Naomi22.45
4Lyanne19.27

Swipe — 20 rows

employee_idactivity_typeactivity_time
1login2024-07-23 08:00:00
1logout2024-07-23 12:00:00
1login2024-07-23 13:00:00
1logout2024-07-23 17:00:00
2login2024-07-23 09:00:00
2logout2024-07-23 11:00:00
2login2024-07-23 12:00:00
2logout2024-07-23 15:00:00
1login2024-07-24 08:30:00
1logout2024-07-24 12:30:00
2login2024-07-24 09:30:00
2logout2024-07-24 10:30:00
3login2024-07-24 09:00:00
3logout2024-07-24 13:45:00
3login2024-07-24 14:30:00
3logout2024-07-24 17:30:00
4login2024-07-23 10:30:00
4logout2024-07-23 15:00:00
4login2024-07-23 16:30:00
4logout2024-07-23 19:00:00

Your Task

Daily Employee Salary Calculation: What is the total salary of each employee for each working day? The final result table should have name, activity_date, and salary columns.

Your output should look like this (partial example):

nameactivity_datesalary
Jeremy2024-07-23134.24
Jeremy2024-07-2467.12
Lyanne2024-07-23134.89

Before You Solve

  • →Use CAST(activity_time AS DATE) to group swipes by working date.
  • →Use LEAD(activity_time) to pair each login row with the next swipe timestamp.
  • →Filter only login rows after calculating the next swipe time.
  • →Use TIMESTAMPDIFF(minute, login_time, logout_time) / 60 to calculate working hours.
  • →Multiply total daily working hours by salary_per_hour_$ and round to two decimals.
Explore Enterprise Solutions