- Table:
Courses
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| student | varchar |
| class | varchar |
+-------------+---------+
(student, class) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates the name of a student and the class in which they are enrolled.
Write a solution to find all the classes that have at least five students.
Return the result table in any order.
제출한 쿼리 :
SELECT class
FROM Courses
GROUP BY CLASS
HAVING COUNT(student) >= 5;
- Table:
Followers
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| follower_id | int |
+-------------+------+
(user_id, follower_id) is the primary key (combination of columns with unique values) for this table.
This table contains the IDs of a user and a follower in a social media app where the follower follows the user.
Write a solution that will, for each user, return the number of followers.
Return the result table ordered by user_id
in ascending order.
제출한 쿼리 :
SELECT user_id
, COUNT(follower_id) AS "followers_count"
FROM Followers
GROUP BY user_id
ORDER BY user_id
- Table:
MyNumbers
+-------------+------+
| Column Name | Type |
+-------------+------+
| num | int |
+-------------+------+
This table may contain duplicates (In other words, there is no primary key for this table in SQL).
Each row of this table contains an integer.
A single number is a number that appeared only once in the MyNumbers
table.
Find the largest single number. If there is no single number, report null
.
제출한 쿼리 :
SELECT IFNULL((SELECT num
FROM mynumbers
GROUP BY num
HAVING COUNT(*) = 1
ORDER BY num DESC LIMIT 1), null) AS "num"
- Table:
Customer
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicates rows.
customer_id
is not NULL.
product_key is a foreign key (reference column) to Product
table.
Table: Product
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key (column with unique values) for this table.
Write a solution to report the customer ids from the Customer
table that bought all the products in the Product
table.
Return the result table in any order.
제출한 쿼리 :
SELECT customer_id
FROM Customer
GROUP BY customer_id
HAVING COUNT(DISTINCT product_key) = (
SELECT COUNT(product_key) FROM Product)
1731. The Number of Employees Which Report to Each Employee (Easy)
- Table:
Employees
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| employee_id | int |
| name | varchar |
| reports_to | int |
| age | int |
+-------------+----------+
employee_id is the column with unique values for this table.
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
Return the result table ordered by employee_id
.
제출한 쿼리 :
SELECT mgr.employee_id
, mgr.name
, COUNT(e.employee_id) AS "reports_count"
, ROUND(AVG(e.age)) AS "average_age"
FROM Employees e
JOIN Employees mgr
ON e.reports_to = mgr.employee_id
GROUP BY employee_id
ORDER BY employee_id;
오늘의 TMI
너무 졸리다....... 피곤해.... 하지만 난 오늘 밤 새야 하는걸
나도 카페인 먹고 싶다