0% found this document useful (0 votes)
16 views2 pages

Window Function Ide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Window Function Ide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Game Play Analysis III

Medium
+10 APs
solved
Write an SQL query to report for each player and date, how many games played so far
by the player.
That is, the total number of games played by the player until that date. Check the
example for clarity.

-- Write your query here


SELECT
player_id, event_date, SUM(games_played) OVER(PARTITION BY player_id ORDER BY
event_date)
AS 'Total_Games'
FROM activity
ORDER BY player_id, Total_Games;

Write an SQL query to find the salaries of the employees after applying taxes.
Round the salary to the nearest integer.

Table: Salaries

The tax rate is calculated for each company based on the following criteria:

0% If the max salary of any employee in the company is less than $1000.
24% If the max salary of any employee in the company is in the range [1000, 10000]
inclusive.
49% If the max salary of any employee in the company is greater than $10000.
Return the result table in any order.

The query result format is in the following example.

-- Write your query here


SELECT company_id, employee_id, employee_name,
ROUND(CASE
WHEN MAX(salary) over(PARTITION BY company_id) < 1000 THEN salary
WHEN MAX(salary) over(PARTITION BY company_id) BETWEEN 1000 AND 10000 THEN
salary-(salary*.24)
ELSE salary-(salary*.49)
END,0) salary
FROM salaries;

The Most Recent Three Orders


Medium
+10 APs
unsolved
Write an SQL query to find the most recent three orders of each user. If a user
ordered less than three orders, return all of their orders.
Return the result table ordered by customer_name in ascending order and in case of
a tie by the customer_id in ascending order. If there is still a tie, order them by
order_date in descending order.

The query result format is in the following example.

Input:

Table: Orders

This table contains information about the orders made by customer_id.

Each customer has one order per day.

-- Write your query here


select customer_name, customer_id, order_id, order_date
from
(select name customer_name, c.customer_id, order_id, order_date,
rank() over(partition by c.customer_id order by order_date desc) rank
from customers c
inner join
orders o
on c.customer_id = o.customer_id) temp
where rank <=3
order by customer_name, customer_id, order_date desc;

You might also like