Skip to content

Commit abc518c

Browse files
committed
feat(sql): solutions
1 parent 46bba24 commit abc518c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

SQL/0175. Combine Two Tables.sql

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- Table: Person
2+
--
3+
-- +-------------+---------+
4+
-- | Column Name | Type |
5+
-- +-------------+---------+
6+
-- | personId | int |
7+
-- | lastName | varchar |
8+
-- | firstName | varchar |
9+
-- +-------------+---------+
10+
-- personId is the primary key column for this table.
11+
-- This table contains information about the ID of some persons and their first and last names.
12+
--
13+
-- Table: Address
14+
--
15+
-- +-------------+---------+
16+
-- | Column Name | Type |
17+
-- +-------------+---------+
18+
-- | addressId | int |
19+
-- | personId | int |
20+
-- | city | varchar |
21+
-- | state | varchar |
22+
-- +-------------+---------+
23+
-- addressId is the primary key column for this table.
24+
-- Each row of this table contains information about the city and state of one person with ID = PersonId.
25+
--
26+
-- Write an SQL query to report the first name, last name, city, and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.
27+
-- Return the result table in any order.
28+
-- The query result format is in the following example.
29+
--
30+
-- Example 1:
31+
--
32+
-- Input:
33+
-- Person table:
34+
-- +----------+----------+-----------+
35+
-- | personId | lastName | firstName |
36+
-- +----------+----------+-----------+
37+
-- | 1 | Wang | Allen |
38+
-- | 2 | Alice | Bob |
39+
-- +----------+----------+-----------+
40+
-- Address table:
41+
-- +-----------+----------+---------------+------------+
42+
-- | addressId | personId | city | state |
43+
-- +-----------+----------+---------------+------------+
44+
-- | 1 | 2 | New York City | New York |
45+
-- | 2 | 3 | Leetcode | California |
46+
-- +-----------+----------+---------------+------------+
47+
-- Output:
48+
-- +-----------+----------+---------------+----------+
49+
-- | firstName | lastName | city | state |
50+
-- +-----------+----------+---------------+----------+
51+
-- | Allen | Wang | Null | Null |
52+
-- | Bob | Alice | New York City | New York |
53+
-- +-----------+----------+---------------+----------+
54+
-- Explanation:
55+
-- There is no address in the address table for the personId = 1 so we return null in their city and state.
56+
-- addressId = 1 contains information about the address of personId = 2.
57+
58+
SELECT Person.FirstName, Person.LastName, Address.City, Address.State
59+
FROM Person
60+
LEFT JOIN Address
61+
ON Person.PersonId = Address.PersonId;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Table: Employee
2+
--
3+
-- +-------------+---------+
4+
-- | Column Name | Type |
5+
-- +-------------+---------+
6+
-- | id | int |
7+
-- | name | varchar |
8+
-- | salary | int |
9+
-- | managerId | int |
10+
-- +-------------+---------+
11+
-- id is the primary key column for this table.
12+
-- Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.
13+
--
14+
--
15+
-- Write an SQL query to find the employees who earn more than their managers.
16+
--
17+
-- Return the result table in any order.
18+
--
19+
-- The query result format is in the following example.
20+
--
21+
--
22+
--
23+
-- Example 1:
24+
--
25+
-- Input:
26+
-- Employee table:
27+
-- +----+-------+--------+-----------+
28+
-- | id | name | salary | managerId |
29+
-- +----+-------+--------+-----------+
30+
-- | 1 | Joe | 70000 | 3 |
31+
-- | 2 | Henry | 80000 | 4 |
32+
-- | 3 | Sam | 60000 | Null |
33+
-- | 4 | Max | 90000 | Null |
34+
-- +----+-------+--------+-----------+
35+
-- Output:
36+
-- +----------+
37+
-- | Employee |
38+
-- +----------+
39+
-- | Joe |
40+
-- +----------+
41+
-- Explanation: Joe is the only employee who earns more than his manager.
42+
43+
SELECT a.Name AS 'Employee'
44+
FROM Employee AS a,
45+
Employee AS b
46+
WHERE a.ManagerId = b.Id
47+
AND a.Salary > b.Salary;

0 commit comments

Comments
 (0)