└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ## Problem 1: 2 | Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), 3 | 4 | find the top 100 students based on their 5 | 6 | average grades sorted descendingly by the average grade and in case multiple students have the same average grade, 7 | sort them lexicographically in ascending order by their names. 8 | 9 | Your query should output a table with the following columns (name, average_grade). 10 | 11 | #### Solution: 12 | ```SQL 13 | SELECT DISTINCT students.name AS name, avg(grade) AS average_grade FROM grades 14 | RIGHT JOIN students ON students.id = grades.student_id 15 | GROUP BY students.name 16 | ORDER BY avg(grade) DESC, students.name ASC 17 | LIMIT 100 18 | ``` 19 | 20 | ___ 21 | ## Problem 2: 22 | 23 | For the same tables in **Problem 1**, for each student, get all the courses that he/she is enrolled in along with the grade he/she scored for each course. 24 | Order the result by the student name in ascending order and if there is a tie, break it with the course name in ascending order and 25 | if there is a tie break it with the grade in ascending order. The final result should have 3 columns with names (name, course, grade). 26 | 27 | #### Solution: 28 | 29 | ```SQL 30 | SELECT DISTINCT students.name AS name,courses.name AS course, grades.grade AS grade 31 | FROM ((grades 32 | INNER JOIN students ON students.id = grades.student_id 33 | INNER JOIN courses ON courses.id = grades.course_id)) 34 | ORDER BY students.name ASC, courses.name ASC, grades.grade ASC 35 | ``` 36 | 37 | ___ 38 | 39 | ## Problem 3: 40 | For the same tables in **Problem 1**, get the name of the most popular course (the one where the most students are enrolled) and if there is a tie, get the course that's lexicographically the smallest. 41 | #### Solution: 42 | ```SQL 43 | SELECT c.name 44 | FROM courses c 45 | INNER JOIN grades g ON g.course_id = c.id 46 | GROUP BY c.id, c.name 47 | ORDER BY COUNT(*) DESC, c.name 48 | LIMIT 1; 49 | ``` 50 | ___ 51 | ## Problem 4: 52 | Given a table called "bugs" with the following columns (id, token, title, category, device, reported_at, created_at, updated_at). 53 | Select all distinct bug categories. 54 | #### Solution: 55 | ```SQL 56 | SELECT DISTINCT category FROM bugs 57 | ``` 58 | ___ 59 | 60 | ## Problem 5: 61 | For the same table in **Problem 4**, find how many bugs were created on "2019-03-01" or later. Your query should produce a table with one column called "count". 62 | This problem is graded partially, 10% on correctness (your query gets the correct count) and 90% on performance (your query makes use of available indexes). 63 | 64 | ***Scored 10% only*** 65 | "Not Optimal solution" 66 | 67 | ```SQL 68 | SHOW INDEXES FROM bugs; 69 | ``` 70 | 71 | ```JSON 72 | "records": [ 73 | { 74 | "Table": "bugs", 75 | "Non_unique": 0, 76 | "Key_name": "PRIMARY", 77 | "Seq_in_index": 1, 78 | "Column_name": "id", 79 | "Collation": "A", 80 | "Cardinality": 9791826, 81 | "Sub_part": null, 82 | "Packed": null, 83 | "Null": "", 84 | "Index_type": "BTREE", 85 | "Comment": "", 86 | "Index_comment": "" 87 | }, 88 | { 89 | "Table": "bugs", 90 | "Non_unique": 1, 91 | "Key_name": "index_bugs_on_category_and_token_and_reported_at", 92 | "Seq_in_index": 1, 93 | "Column_name": "category", 94 | "Collation": "A", 95 | "Cardinality": 1, 96 | "Sub_part": null, 97 | "Packed": null, 98 | "Null": "YES", 99 | "Index_type": "BTREE", 100 | "Comment": "", 101 | "Index_comment": "" 102 | }, 103 | { 104 | "Table": "bugs", 105 | "Non_unique": 1, 106 | "Key_name": "index_bugs_on_category_and_token_and_reported_at", 107 | "Seq_in_index": 2, 108 | "Column_name": "token", 109 | "Collation": "A", 110 | "Cardinality": 29946, 111 | "Sub_part": null, 112 | "Packed": null, 113 | "Null": "YES", 114 | "Index_type": "BTREE", 115 | "Comment": "", 116 | "Index_comment": "" 117 | }, 118 | { 119 | "Table": "bugs", 120 | "Non_unique": 1, 121 | "Key_name": "index_bugs_on_category_and_token_and_reported_at", 122 | "Seq_in_index": 3, 123 | "Column_name": "reported_at", 124 | "Collation": "A", 125 | "Cardinality": 6085027, 126 | "Sub_part": null, 127 | "Packed": null, 128 | "Null": "YES", 129 | "Index_type": "BTREE", 130 | "Comment": "", 131 | "Index_comment": "" 132 | } 133 | ] 134 | } 135 | ``` 136 | ```SQL 137 | SELECT COUNT(x.created_at) AS count 138 | FROM bugs AS y 139 | USE INDEX (PRIMARY,index_bugs_on_category_and_token_and_reported_at) 140 | INNER JOIN bugs AS x ON x.id=y.id 141 | WHERE x.created_at >= '2019-03-01 142 | ``` 143 | 144 | ___ 145 | 146 | ## Problem 6: 147 | 148 | For the same table in **Problem 4**, find the title of the bug with token = "token660" and reported_at on "2020-08-30". 149 | This problem is graded partially, 10% on correctness (your query gets the correct count) and 90% on performance (your query makes use of available indexes). 150 | 151 | ***Scored 10% only*** "Not Optimal Solution" 152 | ```SQL 153 | SELECT x.title FROM bugs 154 | AS y USE INDEX (index_bugs_on_category_and_token_and_reported_at) 155 | JOIN bugs AS x ON x.id=y.id 156 | WHERE y.token ='token660' AND y.reported_at = '2020-08-30' 157 | ``` 158 | --------------------------------------------------------------------------------