0% found this document useful (0 votes)
36 views5 pages

SQL Leetcode Solutions

Uploaded by

patilnandini2406
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)
36 views5 pages

SQL Leetcode Solutions

Uploaded by

patilnandini2406
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

LeetCode SQL Problem Solving

Questions With Solutions

LeetCod
e

175. Combine Two Tables Easy 1 LeetCode


Table: Person

TEXT

+----------+------+
1 Column Name 1 Type 1
+-----------+-------+
1
Personld int
1 FirstName varchar
1 LastName varchar
+---------------------+--------------+

Personld is the primary key column for this table.


Table: Address

TEXT

+ +
1 Column Name 1 Type
+ +
1 Addressld 1 int
1
Personld 1 int
1 City 1 varchar
1 State 1 varchar
+ ++

Addressld 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:

TEXT

FirstName, LastName, City, State

Solution

SELECT [Link], [Link], [Link], [Link]


FROM Person p

LEFT JOIN Address a


ON [Link] = [Link];

176. Second Highest Salary 1 Easy 1 LeetCode

Write a SQL query to get the second highest salary from the Employee
table.
TEXT

+- -+-------------+
1 Id 1 Salary 1
+ -+ +
-
11 1 100

12 1 200

1 1 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.

TEXT

+---------------------------+
1 SecondHighestSalary 1
+---------------------------+
1 200 1
+ +

Solution

SELECT Max(Salary) SecondHighestSalary


FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee)

WITH CTE AS (SELECT DISTINCT


Salary FROM Employee

ORDER BY Salary DESC


LIMIT 2)

SELECT Salary as SecondHighestSalary

FROM CTE
ORDER BY Salary Asc
LIMIT 1;
WITH CTE AS
(

SELECT Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS
DENSERANK FROM Employee

SELECT Salary SecondHighestSalary


FROM CTE

WHERE DENSERANK = 2;

177. Nth Highest Salary 1 Medium 1 LeetCode


Write a SQL query to get the nth highest salary from the Employee table.

TEXT

-I-- --I------------------------+

1 Id 1 Salary 1
+ -+ +
-
1 1 1 100
1
12 1 200 1
13 1 300 1
+- - -- +
+

For example, given the above Employee table, the nth highest salary where n = 2 is
200. If there is no nth highest salary, then the query should return null.

TEXT

1 getNthHighestSalary(2) 1
+ -------------+
1 200 1
+ ------------------------------+
Solution

Ii
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N-1;
RETURN(
SELECT DISTINCT Salary FROM Employee ORDER BY Salary
DESC LIMIT 1 OFFSET N

);
END

178. Rank Scores 1 Medium 1 LeetCode


Write a SQL query to rank scores. If there is a tie between two scores, both should
have the same ranking. Note that after a tie, the next ranking number should be the
next consecutive integer value. In other words, there should be no "holes" between
ranks.

TEXT

+- -+----------------+

I Id I Score I
+
1
13.50 1
I 2
13.65 1
I3 14.00 1
I4 13.85 1
I5 14.00 1
I6 13.65 1

For example, given the above scores table, your query should generate the
following report (order by highest score):

You might also like