diff --git a/Problem1.sql b/Problem1.sql new file mode 100644 index 0000000..47bfa22 --- /dev/null +++ b/Problem1.sql @@ -0,0 +1,10 @@ +WITH CTE AS( +SELECT fail_date as 'dat' , 'failed' As period_state , rank() OVER(ORDER by fail_date) as 'rnk' from failed + WHERE YEAR(fail_date) = 2019 +UNION ALL +SELECT success_date as 'dat' , 'succeeded' As period_state, rank() OVER(ORDER by success_date) as 'rnk' from Succeeded +WHERE YEAR(success_date) = 2019 +) +SELECT period_state , MIN(dat) as start_date , MAX(dat) as end_date FROM (SELECT * , rank() OVER(order by dat) - rnk as diff from CTE)as y +group by diff, period_state +order by start_date; \ No newline at end of file diff --git a/Problem2.sql b/Problem2.sql new file mode 100644 index 0000000..6c30f87 --- /dev/null +++ b/Problem2.sql @@ -0,0 +1,12 @@ +WITH first as( + SELECT name as 'America', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' From student WHERE continent = 'America' +), +second as( + SELECT name as 'Asia', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' From student WHERE continent = 'Asia' +), +third as( + SELECT name as 'Europe', ROW_NUMBER() OVER(ORDER BY name) AS 'rnk' From student WHERE continent = 'Europe' +) + +SELECT America , Asia , Europe FROM second RIGHT JOIN first on first.rnk = second.rnk LEFT JOIN third +ON first.rnk = third.rnk; \ No newline at end of file diff --git a/Problem3.sql b/Problem3.sql new file mode 100644 index 0000000..72bdcc2 --- /dev/null +++ b/Problem3.sql @@ -0,0 +1,19 @@ +WITH companyAvg as( + Select date_format(pay_date,'%Y-%m') as 'pay_month', + AVG(amount) as 'companyavg' from salary group by pay_month +), +deptAvg as( + Select date_format(pay_date,'%Y-%m') as 'pay_month', + department_id, AVG(amount) as 'deptavg' from salary join employee + ON salary.employee_id = employee.employee_id + group by department_id, pay_month +) +SELECT deptAvg.pay_month, department_id, +( + CASE + WHEN deptavg>companyavg THEN 'higher' + WHEN deptavg