Queries Quality and Percentage — LeetCode 1211 Python Solution

EasyDatabase
Problem
#1211
Reading time
3 min

Table schema

SQL
Table: Queries +-------------+---------+ | Column Name | Type | +-------------+---------+ | query_name | varchar | | result | varchar | | position | int | | rating | int | +-------------+---------+ This table may have duplicate rows. This table contains information collected from some queries on a database.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| query_name  | varchar |
| result      | varchar |
| position    | int     |
| rating      | int     |
+-------------+---------+
This table may have duplicate rows.
This table contains information collected from some queries on a database.
The position column has a value from 1 to 500.
The rating column has a value from 1 to 5. Query with rating less than 3 is a poor query.

Python solution

Python
import duckdb
import pandas as pd

def solution(queries: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Queries", queries)
    return con.execute("""SELECT
    query_name,
    ROUND(AVG(rating / position), 2) AS quality,
    ROUND(AVG(rating < 3) * 100, 2) AS poor_query_percentage
FROM Queries
WHERE query_name IS NOT NULL
GROUP BY 1;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1211. Queries Quality and Percentage?
LeetCode 1211. Queries Quality and Percentage is rated Easy on LeetCode.
What topics does LeetCode 1211. Queries Quality and Percentage cover?
LeetCode 1211. Queries Quality and Percentage is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview