0% found this document useful (0 votes)
19 views3 pages

Sas Leetcode

The document outlines two SQL problems from LeetCode: the first is about combining two tables (Person and Address) to generate a report with names and addresses, while the second problem involves retrieving the second highest salary from an Employee table. Solutions are provided using SQL queries and SAS code for both problems. The first solution utilizes a left join, and the second involves calculating maximum salaries and handling cases with no second highest salary.

Uploaded by

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

Sas Leetcode

The document outlines two SQL problems from LeetCode: the first is about combining two tables (Person and Address) to generate a report with names and addresses, while the second problem involves retrieving the second highest salary from an Employee table. Solutions are provided using SQL queries and SAS code for both problems. The first solution utilizes a left join, and the second involves calculating maximum salaries and handling cases with no second highest salary.

Uploaded by

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

175.

Combine Two Tables | Easy | LeetCode


Table: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for
each person in the Person table, regardless if there is an address for each
of those people:

FirstName, LastName, City, State

Answer:

data PersonAddress;

merge Person(in=p) Address(in=a);

by PersonId;

if p; //left join

keep FirstName LastName City State;

run;
176. Second Highest Salary | Easy | LeetCode
Write a SQL query to get the second highest salary from
the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

For example, given the above Employee table, the query should
return 200 as the second highest salary. If there is no second highest
salary, then the query should return null .

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

Solution:

proc means data=Employee noprint;


var Salary;
output out=MaxSalary max=MaxSalary;
run;

/* Step 2: Finding the second highest salary */

data SecondHighestSalary;
if _N_ = 1 then set MaxSalary;
set Employee;
if Salary < MaxSalary then output;
run;

proc means data=SecondHighestSalary noprint;


var Salary;
output out=SecondHighest max=SecondHighestSalary;
run;

/* Step 3: Handleing the case where there is no second highest salary


*/
data FinalResult;
set SecondHighest;
if _N_ = 1 then output;
else do;
SecondHighestSalary = .;
output;
end;
run;

proc print data=FinalResult noobs;


var SecondHighestSalary;
run;

You might also like