Employee Pay with Overtime
Beginner Mode
Scenario
You work in the payroll department of a company and have two tables: one with employee details and another with hours worked and hourly rates.
Task
Write a Snowflake SQL query that:
- Joins
{{ ref("employees") }}with{{ ref("payroll") }}onemployee_id - Calculates a
paycolumn using these rules: ifhours_worked <= 40, pay ishours_worked * hourly_rate; ifhours_worked > 40, pay is(40 * hourly_rate) + ((hours_worked - 40) * hourly_rate * 1.5) - Returns only
employee_id,name,position, andpay
Schema
employees
| Column | Type | Description |
|---|---|---|
| employee_id | Integer | Unique employee identifier |
| name | String | Employee full name |
| age | Integer | Employee age |
| position | String | Job title |
payroll
| Column | Type | Description |
|---|---|---|
| employee_id | Integer | Unique employee identifier |
| hours_worked | Float | Total hours worked in the pay period |
| hourly_rate | Float | Hourly pay rate in dollars |
Example
employees:
| employee_id | name | age | position |
|---|---|---|---|
| 1 | Sara | 29 | Designer |
| 2 | Tom | 34 | Engineer |
| 3 | Mia | 26 | Analyst |
| 4 | Raj | 41 | Manager |
| 5 | Lena | 31 | Coordinator |
payroll:
| employee_id | hours_worked | hourly_rate |
|---|---|---|
| 1 | 35.0 | 20.0 |
| 2 | 45.0 | 25.0 |
| 3 | 40.0 | 30.0 |
| 4 | 50.0 | 22.0 |
| 5 | 38.0 | 18.0 |
Expected Output:
| employee_id | name | position | pay |
|---|---|---|---|
| 1 | Sara | Designer | 700.0 |
| 2 | Tom | Engineer | 1187.5 |
| 3 | Mia | Analyst | 1200.0 |
| 4 | Raj | Manager | 1210.0 |
| 5 | Lena | Coordinator | 684.0 |
Note: Sara and Lena worked under 40 hours (regular pay). Mia worked exactly 40 hours (regular pay). Tom and Raj worked over 40 hours, so their overtime hours are paid at 1.5x the hourly rate.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Airbnb