SQL Miscellaneous Assignment Solution.docx
SQL Miscellaneous Assignment Solution.docx
2.
ills
SELECT merchant,
SUM(CASE WHEN payment_mode = ‘cash’ THEN amount ELSE 0 END) AS
cash_amount,
SUM(CASE WHEN payment_mode = ‘online’ THEN amount ELSE 0 END) AS
Sk
online_amount
FROM payments_data GROUP BY merchant;
3.
4.
w
Select student_id,
sum(case when subject = ‘ENGLISH’ then marks else 0 end) as ENGLISH,
ro
5.
Select * from number_pairs t1 where NOT EXISTS ( select * from number_pairs t2 where t1.B=t2.A
and t1.A=t2.B and t1.A > t2.B);
6.
Select *,
row_number() over(partition by sorted_str, price) as rank
from (Select *,
case when city1<city2 then concat(city1,city2)
when city1>city2 then concat(city2,city1)
end as sorted_str
from travel_data)
Where rank = 1;
7.
ills
Select employee_name,
department_id,
employee_salary
from (Select *,
Sk
row_number() over(partition by department_id order by employee_salary desc) as
rank
From employee_salary)
Where rank<=3;
8.
a
at
Since we consider only the required courses in our GPA calculation, we need to exclude
optional courses using WHERE is_required = TRUE. We need the average GPA per
student per year, so we will GROUP BY both the student_id and the school_year
D
columns and take the average of the gpa column. Lastly, we only keep rows where the
student has an average GPA higher than 3.5, which can be implemented using
HAVING. Let’s put everything together:
w
SELECT
student_id,
ro
school_year,
AVG(gpa) AS avg_gpa
FROM gpa_history
WHERE is_required = TRUE
G
9.
Select t1.student_id,t1.student_name,t2.no_of_classes
From (select student_id,count(1) as no_of_classes from table_2 group by student_id) t2
Inner join table_1 t1
On t1.student_id = t2.student_id
11.
12.
select distinct t1.user_id,case when t2.user_id is not null then 'TRUE' else 'FALSE' end as
ills
overlap
from table t1
left join table2 t2
on t1.user_id != t2.user_id
and t1.start_date <= t2.end_date
Sk
and t1.end_date >= t2.start_date
a
at
D
w
ro
G