-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueries Quality and Percentage 12-10-22
More file actions
64 lines (45 loc) · 1.93 KB
/
Queries Quality and Percentage 12-10-22
File metadata and controls
64 lines (45 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
link --- https://www.codingninjas.com/codestudio/problems/queries-quality-and-percentage_2117108?topList=top-100-sql-problems&leftPanelTab=0
Problem Statement
Suggest Edit
Table: Queries
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| query_name | varchar |
| result | varchar |
| position | int |
| rating | int |
+-------------+---------+
There is no primary key for this table, it 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.
We define query quality as:
The average of the ratio between query rating and its position.
Write an SQL query to find each query_name, the quality.
Quality should be rounded to 2 decimal places.
The query result format is in the following example:
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 |
+------------+-------------------+----------+--------+
Result table:
+------------+---------+
| query_name | quality |
+------------+---------+
| Dog | 2.50 |
| Cat | 0.66 |
+------------+---------+
Dog queries quality is ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
Cat queries quality equals ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
--------------- solution ---------------------------
select query_name , round((avg(rating/position)),2) quality
from queries
group by 1