Skip to content

Commit 0ebc81f

Browse files
committed
task: solution task #584 leetcode
1 parent 607ca1b commit 0ebc81f

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
-- Задача 570.
2+
-- Напишите SQL-запрос, чтобы сообщить менеджерам, по крайней мере, с пятью прямыми подчиненными.
3+
-- Вывести: таблицу результатов в любом порядке.
4+
5+
USE leetcode
6+
7+
IF EXISTS(SELECT *
8+
FROM INFORMATION_SCHEMA.TABLES
9+
WHERE TABLE_NAME = N'Employee')
10+
BEGIN
11+
DROP TABLE Employee
12+
PRINT 'Table delete successfully'
13+
END
14+
ELSE
15+
BEGIN
16+
CREATE TABLE Employee
17+
(
18+
id int,
19+
name varchar(255),
20+
department varchar(255),
21+
managerId int
22+
)
23+
24+
INSERT INTO Employee (id, name, department, managerId) VALUES (101, 'John', 'A', null)
25+
INSERT INTO Employee (id, name, department, managerId) VALUES (102, 'Dan', 'A', 101)
26+
INSERT INTO Employee (id, name, department, managerId) VALUES (103, 'James', 'A', 101)
27+
INSERT INTO Employee (id, name, department, managerId) VALUES (104, 'Amy', 'A', 101)
28+
INSERT INTO Employee (id, name, department, managerId) VALUES (105, 'Anne', 'A', 101)
29+
INSERT INTO Employee (id, name, department, managerId) VALUES (106, 'Ron', 'B', 101)
30+
31+
PRINT 'Table create successfully'
32+
END
33+
GO
34+
35+
-- Solution
36+
SELECT
37+
e1.name
38+
FROM
39+
Employee e1
40+
INNER JOIN
41+
Employee e2
42+
ON
43+
e1.id = e2.managerId
44+
GROUP BY
45+
e1.name
46+
HAVING
47+
COUNT(e1.name) >= 5
48+
GO
49+
50+
-- Solution
51+
SELECT
52+
e.name
53+
FROM
54+
Employee e
55+
WHERE (SELECT
56+
COUNT(managerId)
57+
FROM
58+
Employee
59+
WHERE
60+
managerId = e.id) >= 5
61+
GO
62+
63+
64+
-- Input:
65+
-- Employee table:
66+
+-----+-------+------------+-----------+
67+
| id | name | department | managerId |
68+
+-----+-------+------------+-----------+
69+
| 101 | John | A | None |
70+
| 102 | Dan | A | 101 |
71+
| 103 | James | A | 101 |
72+
| 104 | Amy | A | 101 |
73+
| 105 | Anne | A | 101 |
74+
| 106 | Ron | B | 101 |
75+
+-----+-------+------------+-----------+
76+
77+
-- Output:
78+
+------+
79+
| name |
80+
+------+
81+
| John |
82+
+------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
-- Задача 570.
2+
-- Напишите SQL-запрос, чтобы сообщить имена клиентов, на которые клиент не ссылается с помощью.
3+
-- id = 2
4+
-- Вывести: таблицу результатов в любом порядке.
5+
6+
USE leetcode
7+
8+
IF EXISTS(SELECT *
9+
FROM INFORMATION_SCHEMA.TABLES
10+
WHERE TABLE_NAME = N'Customer')
11+
BEGIN
12+
DROP TABLE Customer
13+
PRINT 'Table delete successfully'
14+
END
15+
ELSE
16+
BEGIN
17+
CREATE TABLE Customer
18+
(
19+
id int,
20+
name varchar(25),
21+
referee_id int
22+
)
23+
24+
INSERT INTO Customer (id, name, referee_id) VALUES (1, 'Will', null)
25+
INSERT INTO Customer (id, name, referee_id) VALUES (2, 'Jane', null)
26+
INSERT INTO Customer (id, name, referee_id) VALUES (3, 'Alex', 2)
27+
INSERT INTO Customer (id, name, referee_id) VALUES (4, 'Bill', null)
28+
INSERT INTO Customer (id, name, referee_id) VALUES (5, 'Zack', 1)
29+
INSERT INTO Customer (id, name, referee_id) VALUES (6, 'Mark', 2)
30+
31+
PRINT 'Table create successfully'
32+
END
33+
GO
34+
35+
-- Solution
36+
SELECT
37+
name
38+
FROM
39+
Customer
40+
WHERE
41+
referee_id IS NULL
42+
OR
43+
referee_id <> 2
44+
GO
45+
46+
-- Solution
47+
SELECT
48+
name
49+
FROM
50+
Customer
51+
WHERE
52+
referee_id != 2
53+
OR
54+
referee_id IS NULL
55+
GO
56+
57+
-- Solution
58+
SELECT
59+
name
60+
FROM
61+
Customer
62+
WHERE
63+
isnull(referee_id, 0) != 2
64+
GO
65+
66+
67+
-- Input:
68+
-- Customer table:
69+
+----+------+------------+
70+
| id | name | referee_id |
71+
+----+------+------------+
72+
| 1 | Will | null |
73+
| 2 | Jane | null |
74+
| 3 | Alex | 2 |
75+
| 4 | Bill | null |
76+
| 5 | Zack | 1 |
77+
| 6 | Mark | 2 |
78+
+----+------+------------+
79+
80+
-- Output:
81+
+------+
82+
| name |
83+
+------+
84+
| Will |
85+
| Jane |
86+
| Bill |
87+
| Zack |
88+
+------+

0 commit comments

Comments
 (0)