Queries Quality and Percentage - Problem
You're working as a data analyst for a search engine company, analyzing the performance of database queries to improve user experience. Your task is to calculate two key metrics for each query type:
- Quality Score: The average ratio of rating to position across all queries
- Poor Query Percentage: The percentage of queries with ratings below 3
Given a Queries table with columns:
query_name(varchar): The name/type of the queryresult(varchar): The search result returnedposition(int): Position in search results (1-500)rating(int): User rating (1-5, where <3 is considered poor)
Goal: Calculate quality and poor_query_percentage for each query_name, rounded to 2 decimal places.
Formula: Quality = AVG(rating/position), Poor% = (COUNT(rating<3)/COUNT(*))*100
Input & Output
example_1.sql โ Basic Query Analysis
$
Input:
Queries table:
| query_name | result | position | rating |
|------------|-------------------|----------|--------|
| Dog | Golden Retriever | 1 | 5 |
| Dog | German Shepherd | 2 | 5 |
| Dog | Mule | 200 | 1 |
| Cat | Shirazi | 5 | 2 |
| Cat | Siamese | 3 | 3 |
| Cat | Sphynx | 7 | 4 |
โบ
Output:
| query_name | quality | poor_query_percentage |
|------------|---------|----------------------|
| Cat | 2.66 | 33.33 |
| Dog | 2.50 | 33.33 |
๐ก Note:
For Dog: quality = (5/1 + 5/2 + 1/200)/3 = 2.50, poor queries = 1 out of 3 = 33.33%. For Cat: quality = (2/5 + 3/3 + 4/7)/3 = 2.66, poor queries = 1 out of 3 = 33.33%.
example_2.sql โ All Good Queries
$
Input:
Queries table:
| query_name | result | position | rating |
|------------|------------|----------|--------|
| FreshFood | Organic | 1 | 4 |
| FreshFood | Local | 2 | 5 |
| FreshFood | Seasonal | 3 | 3 |
โบ
Output:
| query_name | quality | poor_query_percentage |
|------------|---------|----------------------|
| FreshFood | 4.00 | 0.00 |
๐ก Note:
For FreshFood: quality = (4/1 + 5/2 + 3/3)/3 = 4.00, no poor queries (all ratings โฅ 3) = 0.00%.
example_3.sql โ All Poor Queries
$
Input:
Queries table:
| query_name | result | position | rating |
|------------|----------|----------|--------|
| BadSearch | Spam1 | 1 | 1 |
| BadSearch | Spam2 | 2 | 2 |
| BadSearch | Spam3 | 3 | 1 |
โบ
Output:
| query_name | quality | poor_query_percentage |
|------------|---------|----------------------|
| BadSearch | 1.17 | 100.00 |
๐ก Note:
For BadSearch: quality = (1/1 + 2/2 + 1/3)/3 = 1.17, all queries are poor (ratings < 3) = 100.00%.
Constraints
-
query_namecan be null or any varchar value -
positionis an integer from 1 to 500 -
ratingis an integer from 1 to 5 -
Poor queries are defined as those with
rating < 3 - Results must be rounded to exactly 2 decimal places
- Table may contain duplicate rows
- Return results in any order
Visualization
Tap to expand
Understanding the Visualization
1
Group Reviews
Organize all reviews by product category (query_name)
2
Calculate Quality
For each category, compute average of (rating รท position) - higher ratings in better positions score higher
3
Count Poor Reviews
Count reviews with ratings below 3 stars as percentage of total
4
Generate Report
Round both metrics to 2 decimal places for presentation
Key Takeaway
๐ฏ Key Insight: Use SQL GROUP BY with aggregate functions (AVG, SUM, COUNT) to calculate multiple related metrics in a single efficient query, avoiding the need for multiple table scans.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code