Interview Questions/SQL/Queries Quality and Percentage

Queries Quality and Percentage

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have a Queries table. Each row represents a search query result with a position and a rating. Define: - quality: the average of (rating / position) for each query_name, rounded to 2 decimal places. - poor_query_percentage: the percentage of results with rating < 3 for each query_name, rounded to 2 decimal places. Write a SQL query to report the query_name, quality, and poor_query_percentage for each query. Return the result in any order.

Database Schema

Queries

Column NameTypeDescription
query_nameVARCHARName of the search query
resultVARCHARResult returned
positionINTPosition in results (1 to 500)
ratingINTUser rating (1 to 5)

Example

Queries

query_nameresultpositionrating
DogGolden Retriever15
DogGerman Shepherd25
DogMule2001
CatShirazi52
CatSiamese33
CatSphynx74

Output

query_namequalitypoor_query_percentage
Dog2.533.33
Cat0.6633.33

Explanation:

Dog quality = ((5/1)+(5/2)+(1/200))/3 = 2.50. Dog poor% = 1/3 * 100 = 33.33. Cat quality = ((2/5)+(3/3)+(4/7))/3 = 0.66. Cat poor% = 1/3 * 100 = 33.33.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Queries

query_nameresultpositionrating
DogGolden Retriever15
DogGerman Shepherd25
DogMule2001
CatShirazi52
CatSiamese33
CatSphynx74

Output

query_namequalitypoor_query_percentage
Dog2.533.33
Cat0.6633.33