SQL Assignment 5
SQL Assignment 5
We would like to find the total weight of dogs grouped by age. But only return those
groups with a total weight larger than 12.
Write your query here
select * from (select
age, sum(weight) over(partition by age) as total_weight
from dogs)as a where a.total_weight>12 ;
2.
Employee Department Salary
E1 D1 1200
E2 D2 1500
E3 D1 800
E4 D1 1100
E5 D2 1400
E6 D3 700
E7 D2 500
E8 D3 900
E9 D1 1200
E10 D3 1000
We would like to find the average salary of employees grouped by department. But only
return those groups with an average salary weight larger than 1000.
Write your query here
select * from(select employee,department ,salary , avg(salary)over (partition by
department )as avg_salary from employees) where avg_salary>1000;
3. Get The running total of salaries of Employees from employee table(employee_id, name,
join_date,salary) ordering by the joining date of employee
Write your query here
select employee_id,name,join_date,salary,sum(salary)over (partition by name
order by join_date)as Running_totalsalary from employee;
4. In an Entrance exam students 10 will be selected, Write a query to rank the students
according to their marks (students having same marks should be ranked as same) The
Students table contains(Student_id, Student_name, Roll_number_Marks)
Write your query here
select student_id,student_name,roll_number_marks,dense_rank()over ( order
by Roll_number_Marks desc) as dense_rank
from students
limit 10;
5. In the above question the criteria of selection has changed and now the students ranked
up to 10 will be selected i.e. we can have more than 10 students.
Write a query to get the desired output.
Write your query here
select student_id,student_name,roll_number_marks,dense_rank()over ( order
by Roll_number_Marks desc) as dense_rank
from students ;
6. We have a stock data which is updated daily We predict the daily stock price using the
lag(1)criteria Write a qwerty to get the lag(1) value from the Stocks table(date,price)
Write your query here
select date, price,lag(price,1)over ( order by date)as current_price
from stocks;
10. Write the correct syntax of the NTILE function and also state the use of this function
a. Write your answer here
Select *, ntile() over (partition by order by )as group
From table;
It is used to divide the rows within a partition equally as possible into n groups,
And assign each row its group number