SQL
Lab
Microsoft Microsoft Interview Question 01

License
Utilization
Rate

Calculate the percentage of assigned licenses that are actively used.

Table Schema

Inspect
Table

interactive
ColumnType
license_idinteger
user_idinteger
assigned_datedate
statusvarchar
ColumnType
user_idinteger
last_active_datedate

Sample Data

Input
Output

Sample Input: licenses
license_iduser_idassigned_datestatus
11012024-01-01active
21022024-01-02active
31032024-01-03inactive
41042024-01-04active
51052024-01-05active
Sample Input: usage_logs
user_idlast_active_date
1012024-02-01
1022024-02-10
1042024-01-15
Expected Output
utilization_rate
75.00

SQL Editor

Run
Query

postgresql
Waiting for query

utilization_rate
75.00

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT ROUND(100.0 * COUNT(u.user_id) / COUNT(l.license_id), 2) AS utilization_rate
FROM licenses l
LEFT JOIN usage_logs u ON l.user_id = u.user_id
WHERE l.status = 'active';

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving