Top Profitable Drugs
Beginner Mode
Objective
Write a SQL query to identify the top 3 most profitable drugs from pharmacy sales data.
Table Schema:
- pharmacy_sales
| Column | Type | Description |
|---|---|---|
| product_id | INTEGER | Unique identifier for each product |
| drug_name | VARCHAR | Name of the drug |
| units_sold | INTEGER | Number of units sold |
| total_sales | DECIMAL | Total sales revenue |
| cogs | DECIMAL | Cost of goods sold |
Task Requirements:
- Calculate total profit for each drug (total_sales - cogs)
- Sum profits across all records for each drug
- Select only the top 3 most profitable drugs
- Sort results by profit in descending order
Output columns: drug_name, total_profit
Examples
Example 1:
Output:
Input:
| pharmacy_sales | ||||
|---|---|---|---|---|
| cogs | drug_name | product_id | total_sales | units_sold |
| 2000 | Aspirin | 1 | 5000 | 100 |
| 3000 | Ibuprofen | 2 | 7500 | 150 |
| 3500 | Paracetamol | 3 | 8000 | 200 |
| 2500 | Amoxicillin | 4 | 6000 | 80 |
| drug_name | total_profit |
|---|---|
| Ibuprofen | 4500 |
| Paracetamol | 4500 |
| Amoxicillin | 3500 |
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 →
Meta