Source:
Disclaimer: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me
Article Directory
How to use distinct in MySQL
Test table:
- Operate on multiple columns
select distinct orderid, foodName, price from table_3;
When distinct is applied to multiple fields, the scope of its application isAll fields after it, Not just a field next to it, and distinct can only be placed in front of all fields. The following statement is wrong:
select orderid, foodName, distinct price from table_3;
- Distinct does not filter NULL, that is, the returned result contains NULL values.
619. The largest number that appears only once
Title description
Link: https://leetcode-cn.com/problems/biggest-single-number
The num field of the table my_numbers contains many numbers, including many repeated numbers.
Can you write a SQL query and find the largest number among the numbers that have only appeared once?
±–+
|num|
±–+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
For the sample data given above, your query statement should return the following results :
±–+
|num|
±–+
| 6 |
Note:
If there is no digit that only appears once, output null.
answer
# Write your MySQL query statement below
select max(num) as num from
(
select num from my_numbers group by num having count(num)=1
)as template_table
select
ifnull(
(
select num from
(
select num from my_numbers group by num having count(*)=1 order by num desc
)as template_table
limit 1)
,null) as num;
1076. Project Staff II
Link: https://leetcode-cn.com/problems/project-employees-ii
Title description
Table: Project
±------------±--------+
| Column Name | Type |
±------------±------- -+
| project_id | int |
| employee_id | int |
±------------±--------+The
primary key is (project_id, employee_id).
employee_id is the foreign key of the Employee table.
Table: Employee
±-----------------±--------+
| Column Name | Type |
±-----------------±--------+
| employee_id | int |
| name | varchar |
| experience_years | int |
±-----------------±--------+
主键是 employee_id。
Write a SQL query to report the items with the most employees.
The query result format is as follows:
Project table:
±------------±------------+
| project_id | employee_id |
±------------±------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 4 |
±------------±------------+
Employee table:
±------------±-------±-----------------+
| employee_id | name | experience_years |
±------------±-------±-----------------+
| 1 | Khaled | 3 |
| 2 | Ali | 2 |
| 3 | John | 1 |
| 4 | Doe | 2 |
±------------±-------±-----------------+
Result table:
±------------+
| project_id |
±------------+
| 1 |
±------------ +The
first project has 3 employees and the second project has 2 employees.
answer
# Write your MySQL query statement below
select project_id
from Project
group by project_id
having count(distinct employee_id) >= all
(
select count(distinct employee_id) from Project group by project_id
)
SELECT PROJECT_ID
FROM PROJECT
GROUP BY 1
HAVING COUNT(*) >= ALL (SELECT COUNT(*)
FROM PROJECT
GROUP BY PROJECT_ID);
178. Score Ranking
Link: https://leetcode-cn.com/problems/rank-scores
Title description
Write a SQL query to achieve score ranking.
If the two scores are the same, the two scores rank (Rank) the same. Please note that the next ranking after the split should be the next consecutive integer value. In other words, there should be no "gap" between the rankings.
±—±------+
| Id | Score |
±—±------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
±—±------+
For example, according to the Scores table given above, your query should return (ranked from highest to lowest score):
±------±-----+
| Score | Rank |
±------±-----+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
±------±-----+
Important note: For MySQL solutions, if you want to escape the reserved words used as column names, you can use the key Use apostrophes before and after words. E.gRank
answer
SELECT SCORE, DENSE_RANK() OVER(ORDER BY SCORE DESC) AS 'RANK'
FROM SCORES;
select a.Score as Score,
(select count(distinct b.Score) from Scores b where b.Score >= a.Score) as "Rank"
from Scores a order by a.Score desc
There is no rank function in mysql! ! ! !
Refer to http://fellowtuts.com/mysql/query-to-obtain-rank-function-in-mysql/How to implement rank function in mysql