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;