Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

SELECT+ Rank, Datediff

$
0
0

Hi all,

I've done a little exercise in an online course with two questions that has no DB attached.

Would really appreciate it if someone just approve I've done them right/explain why i'm wrong.

Many thanks in advance!!

Q1:

Table employees: Id, name, managerId (references employees(id)), department, salary Write a query that selects all employees whose salary is ranked 3rd in their department

A1:

SELECT * FROM (
SELECT name, department, salary, RANK() OVER (
PARTITION BY department 
ORDER BY salary DESC) salary_rank 
FROM sale) t
WHERE salary_rank=3;

Q2:

Table sessions: Id, userId, duration, data_timestamp

Write a query that selects userId and average session duration for each “Good User”. Good User - a user with an average of at least 5 sessions in a week

A2:

SELECT userId, AVG(duration), 
FROM sessions
WHERE AVG(SELECT DATEADD(
DAY, -DATEDIFF(DAY, 0, [data_timestamp]) % 7, [data_timestamp]) 
AS [Week of],
  COUNT(*) AS Count
FROM sessions) => 5; 



Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>