From ff618e10a2657f561ff0c15ecebb25b3edeb1a9a Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Wed, 28 May 2025 15:38:42 -0700 Subject: [PATCH 1/6] Added Q1 SQL solution --- Q1_Solution.sql | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Q1_Solution.sql 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 From 48d229eb4012cd96e730421a06a9705cf7c3d197 Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Wed, 28 May 2025 20:54:27 -0700 Subject: [PATCH 2/6] Added Q1 SQL solution --- SQL1_Q2_Question.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 SQL1_Q2_Question.sql 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 From c631e5c93a5b2dfb4f2102f3694d1a4875218718 Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Wed, 28 May 2025 21:01:47 -0700 Subject: [PATCH 3/6] Added Q2 and Q3 SQL solutions --- Q3_Sql1_solution.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Q3_Sql1_solution.sql diff --git a/Q3_Sql1_solution.sql b/Q3_Sql1_solution.sql new file mode 100644 index 0000000..2b2d026 --- /dev/null +++ b/Q3_Sql1_solution.sql @@ -0,0 +1,15 @@ +with rank_email_duplicates as +( +select + id + , email + , row_number() over(partition by email order by id) as rownum +from + Person +) +select + id + , email +from + rank_email_duplicates +where rownum = 1 \ No newline at end of file From 0be5d56cb20b9f8f4804bde3dd0adcd3a101d02d Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Fri, 30 May 2025 22:22:08 -0700 Subject: [PATCH 4/6] Sql1 Q2 DECLARE SET Method --- Q1_Solution_UsingDECLARE_SET | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Q1_Solution_UsingDECLARE_SET 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 From f77804cc752c5cd7bc92defdc8ced6d7b3779d32 Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Fri, 30 May 2025 23:59:59 -0700 Subject: [PATCH 5/6] edited sql1_q3 --- Q3_Sql1_solution.sql | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Q3_Sql1_solution.sql b/Q3_Sql1_solution.sql index 2b2d026..b7be626 100644 --- a/Q3_Sql1_solution.sql +++ b/Q3_Sql1_solution.sql @@ -1,15 +1,12 @@ -with rank_email_duplicates as +with ranked_emails as ( -select - id - , email - , row_number() over(partition by email order by id) as rownum -from - Person + select + id + , email + , row_number() over(partition by email order by id) as rnk + from + Person ) -select - id - , email -from - rank_email_duplicates -where rownum = 1 \ No newline at end of file +delete +from person p +where p.id in (select ranked_emails.id from ranked_emails where rnk > 1) \ No newline at end of file From 39b4e380a13e5f8323cd68ebbc2e28f29c39b6b1 Mon Sep 17 00:00:00 2001 From: Arushi Bhambri Date: Sat, 31 May 2025 00:07:29 -0700 Subject: [PATCH 6/6] Sql1 Q3 Cross Join solution --- sql1_q3_crossjoin | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sql1_q3_crossjoin 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