Day #1: SQL Questions
Shashwata Saha
Senior Data engineer @ ThoughtWorks | Discovering, analyzing, transforming, warehousing, and tracking Big Data by end-to-end ETL data pipeline using Spark, AWS, Snowflake, SQL | GitHub licensed
I will try to document, my solutions of the leetcode SQL 50 study plan.
The question for today is https://leetcode.com/problems/students-and-examinations/description/?envType=study-plan-v2&envId=top-sql-50
The Problem
Where we are given 3 tables:
Table: Students
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| student_id | int |
| student_name | varchar |
+---------------+---------+
student_id is the primary key
?
Table: Subjects
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| subject_name | varchar |
+--------------+---------+
subject_name is the primary key
?
Table: Examinations
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| student_id | int |
| subject_name | varchar |
+--------------+---------+
There is no primary key. It may contain duplicates.
?
领英推荐
Write a solution to find the number of times each student attended each exam.
Return the result table ordered by student_id and subject_name.
The Intuition:
The Solution:
select re.student_id,re.student_name,re.subject_name, sum(re.attended) as attended_exams
from
(select
s.student_id,
s.student_name,
sub.subject_name,
if(e.subject_name is null,0,1) as attended
from
students s
cross join
subjects sub
left join
examinations e
on s.student_id=e.student_id and sub.subject_name=e.subject_name
) re
group by re.student_id,re.subject_name
order by re.student_id,re.subject_name;
The Optimized Solution:
select
s.student_id,
s.student_name,
sub.subject_name,
count(e.subject_name) as attended_exams
from
students s
cross join
subjects sub
left join
examinations e
on s.student_id=e.student_id and sub.subject_name=e.subject_name
group by s.student_id,sub.subject_name
order by s.student_id,sub.subject_name;