Skip to content

Instantly share code, notes, and snippets.

@bhargavkulk
Created February 25, 2021 15:14
Show Gist options
  • Save bhargavkulk/89af241534b4509d214c80f0bc019d64 to your computer and use it in GitHub Desktop.
Save bhargavkulk/89af241534b4509d214c80f0bc019d64 to your computer and use it in GitHub Desktop.
Scenario 1 answers lab 4
-- 1
select ssid
from students
order by marks desc
limit 1 offset 1;
-- 2
select branch, count(branch)
from students
group by branch
having count(branch) >= 3;
-- 3
select branch, avg(marks)
from students
group by branch
having avg(marks) > (
select avg(marks)
from students
where gender = 'FEMALE'
);
-- 4
select branch, avg(marks)
from students
where gender = "MALE"
group by branch
having avg(marks) > (
select avg(marks)
from students
where gender = 'FEMALE'
);
-- 5
with males(branch, avg_marks) as (
select branch, avg(marks)
from (select * from students where gender = 'MALE') m
group by branch
),
females (branch, avg_marks) as (
select branch, avg(marks)
from (select * from students where gender = 'FEMALE') f
group by branch
)
select males.branch, males.avg_marks
from males, females
where males.branch = females.branch and males.avg_marks > females.avg_marks;
-- 6
select max(m.marks) - max(f.marks)
from (select marks from students where gender = 'MALE') m, (select marks from students where gender = 'FEMALE') f;
-- 7
select count(*)
from students
where 90 > marks and marks > 70 and branch = 'CS';
-- 8
update students
set gender = lower(gender)
where ssid <> '';
select gender from students;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment