diff --git a/Q1_Solution.sql b/Q1_Solution.sql new file mode 100644 index 0000000..d5eb053 --- /dev/null +++ b/Q1_Solution.sql @@ -0,0 +1,7 @@ +select + name + , population + , area +from + World +where (area >= 3000000) or (population >= 25000000) \ No newline at end of file diff --git a/Q1_Solution_UsingDECLARE_SET b/Q1_Solution_UsingDECLARE_SET new file mode 100644 index 0000000..06c69a4 --- /dev/null +++ b/Q1_Solution_UsingDECLARE_SET @@ -0,0 +1,14 @@ +CREATE FUNCTION GetNthHighestSalary(N INT) RETURNS INT +BEGIN + DECLARE M INT + SET M = N-1 + RETURN + ( + select + distinct salary + from + Employee + order by desc + limit M, 1 + ); + END \ No newline at end of file diff --git a/Q3_Sql1_solution.sql b/Q3_Sql1_solution.sql new file mode 100644 index 0000000..b7be626 --- /dev/null +++ b/Q3_Sql1_solution.sql @@ -0,0 +1,12 @@ +with ranked_emails as +( + select + id + , email + , row_number() over(partition by email order by id) as rnk + from + Person +) +delete +from person p +where p.id in (select ranked_emails.id from ranked_emails where rnk > 1) \ No newline at end of file diff --git a/SQL1_Q2_Question.sql b/SQL1_Q2_Question.sql new file mode 100644 index 0000000..c3f66d0 --- /dev/null +++ b/SQL1_Q2_Question.sql @@ -0,0 +1,12 @@ +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT +BEGIN + RETURN ( +with cte as ( + select + * + , dense_rank() over(order by salary desc) as rnk + from Employee +) +select distinct ifnull(salary, null) from cte where rnk = N +); +END \ No newline at end of file diff --git a/sql1_q3_crossjoin b/sql1_q3_crossjoin new file mode 100644 index 0000000..5ddc97b --- /dev/null +++ b/sql1_q3_crossjoin @@ -0,0 +1,4 @@ +DELETE p1 +from person p1 +cross join person p2 where p1.email = p2.email +and p1.id > p2.id \ No newline at end of file