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 query
  • result (varchar): The search result returned
  • position (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_name can be null or any varchar value
  • position is an integer from 1 to 500
  • rating is 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
Query Performance AnalyticsAnalyzing Search Query Quality & User SatisfactionQuality Score Calculation๐Ÿ” Dog Queries:โ€ข Rating 5, Position 1 โ†’ 5.00โ€ข Rating 5, Position 2 โ†’ 2.50โ€ข Rating 1, Position 200 โ†’ 0.01Average = 2.50Poor Query Analysis๐Ÿ“Š Dog Queries:โ€ข Total queries: 3โ€ข Poor queries (rating < 3): 1โ€ข Poor percentage: 1/3 ร— 100Result = 33.33%Final Dashboard Report๐Ÿ• Dog: Quality 2.50, Poor 33.33%โœจ One SQL query calculates both metrics efficiently!
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.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.5K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen