DB_dictionary_190102_MySQLDictionaryFromLeetCode

1.Swap Salary

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.
For example:

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

After running your query, the above salary table should have the following rows:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

Solution:

update salary
set
    sex= case sex
        when 'm' then 'f'
        else 'm'
    end;

2.626. Exchange Seats

Mary is a teacher in a middle school and she has a table seat storing students’ names and their corresponding seat ids.

The column id is continuous increment.
Mary wants to change seats for the adjacent students.
Can you write a SQL query to output the result for Mary?

idstudent
1Abbot
2Doris
3Emerson
4Green
5Jeames

For the sample input, the output is:

idstudent
1Doris
2Abbot
3Green
4Emerson
5Jeames

Note:
If the number of students is odd, there is no need to change the last one’s seat.

#TIPS
#case
#   when xx then yy
#   when xx then yy
#   else yy
# MOD(xx,num)

select
(
    case
            when MOD(id,2)!=0 and counts!=id then id+1
            when MOD(id,2)!=0 and counts=id then id
            else id-1
        end
) as id,student
from seat,
(
    select count(*) as counts from seat
) as seat_counts
#Every derived table must have its own alias
order by id asc
select
(
    case
            when id%2=1 and id!=maxV then id+1
            when id%2=1 and id=maxV then id
            else id-1
        end
) as id,student
from seat,(
select max(id) as maxV from seat)as maxs
order by id asc

选择id是经过调整的id,因为对应的另一列无法调整,再选择student,这两个变量都是从seat表出来的,后续的括号中计算的是前面选择id时用的中间变量

3.601. Human Traffic of Stadium

X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people

Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).

For example, the table stadium:

iddatepeople
12017-01-0110
22017-01-02109
32017-01-03150
42017-01-0499
52017-01-05145
62017-01-061455
72017-01-07199
82017-01-08188

For the sample data above, the output is:

iddatepeople
52017-01-05145
62017-01-061455
72017-01-07199
82017-01-08188

通过笛卡儿积解决问题
my version

select distinct t1.*
from stadium t1 , stadium t2 , stadium t3
where t1.people>100 and t2.people>100 and t3.people>100
and
(
    (t1.id-t2.id=-1 and t1.id-t3.id=-2 and t2.id-t3.id=-1)
    # t1,t2,t3
    or
    (t1.id-t2.id=1 and t1.id-t3.id=-1 and t2.id-t3.id=-2)
    # t2,t1,t3
    or
    (t1.id-t2.id=2 and t1.id-t3.id=1 and t2.id-t3.id=-1)
    #t2,t3,t1
)
order by t1.id

example version

select distinct t1.*
from stadium t1, stadium t2, stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
and
(
	  (t1.id - t2.id = 1 and t1.id - t3.id = 2 and t2.id - t3.id =1)  -- t1, t2, t3
    or
    (t2.id - t1.id = 1 and t2.id - t3.id = 2 and t1.id - t3.id =1) -- t2, t1, t3
    or
    (t3.id - t2.id = 1 and t2.id - t1.id =1 and t3.id - t1.id = 2) -- t3, t2, t1
)
order by t1.id
;

4.197. Rising Temperature

Given a Weather table, write a SQL query to find all dates’ Ids with higher temperature compared to its previous (yesterday’s) dates.

Id(INT)RecordDate(DATE)Temperature(INT)
12015-01-0110
22015-01-0225
32015-01-0320
42015-01-0430

For example, return the following Ids for the above Weather table:

Id
2
4

my solution:

select t1.Id from
Weather t1,Weather t2
where
t1.id>t2.id and t1.Temperature>t2.Temperature
and t2.id = t1.id-1

leetcode standard solution:
DATEDIFF(A,B)=1 如果A-B=1即为真

SELECT
    weather.id AS 'Id'
FROM
    weather
        JOIN
    weather w ON DATEDIFF(weather.RecordDate, w.RecordDate) = 1
        AND weather.Temperature > w.Temperature
;

5.180. Consecutive Numbers

Write a SQL query to find all numbers that appear at least three times consecutively.

IdNum
11
21
31
42
51
62
72

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

ConsecutiveNums
1

my solution:

select Num as ConsecutiveNums from
(
    select t1.Num from
    logs t1,logs t2,logs t3
    where
    t1.num=t2.num and t1.num=t3.num and t2.num=t3.num
    and
    (
        t1.id-t2.id=-1 and t1.id-t3.id=-2 
        and t2.id-t3.id=-1 #t1 t2 t3
    )
) as cnt

6.196. Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

IdEmail
1john@example.com
2bob@example.com
3john@example.com

Id is the primary key column for this table.
For example, after running your query, the above Person table should have the following rows:

IdEmail
1john@example.com
2bob@example.com

Note:

Your output is the whole Person table after executing your sql. Use delete statement.
Solution:
Approach: Using DELETE and WHERE clause [Accepted]
Algorithm

By joining this table with itself on the Email column, we can get the following code.

SELECT p1.*
FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email
;

Then we need to find the bigger id having same email address with other records. So we can add a new condition to the WHERE clause like this.

SELECT p1.*
FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id
;

As we already get the records to be deleted, we can alter this statement to DELETE in the end.

MySQL

DELETE p1 FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id

7.184. Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

IdNameSalaryDepartmentId
1Joe700001
2Henry800002
3Sam600002
4Max900001

The Department table holds all departments of the company.

IdName
1IT
2Sales

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

DepartmentEmployeeSalary
ITMax90000
SalesHenry80000

Solution:

select department.name as Department,
employee.name as Employee,employee.salary as Salary
from employee
join department
on employee.departmentid=department.id
where
(employee.departmentid,employee.salary) 
in
(select departmentid,max(salary) from employee
group by departmentid)
order by Salary

8.185. Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

IdNameSalaryDepartmentId
1Joe700001
2Henry800002
3Sam600002
4Max900001
5Janet690001
6Randy850001

The Department table holds all departments of the company.

IdName
1IT
2Sales

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

DepartmentEmployeeSalary
ITMax90000
ITRandy85000
ITJoe70000
SalesHenry80000
SalesSam60000

Solution:
A top 3 salary in this company means there is no more than 3 salary bigger than itself in the company.

select e1.Name as 'Employee', e1.Salary
from Employee e1
where 3 >
(
    select count(distinct e2.Salary)
    from Employee e2
    where e2.Salary > e1.Salary
)
;

Then, we need to join the Employee table with Department in order to retrieve the department information.

SELECT
    d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM
    Employee e1
        JOIN
    Department d ON e1.DepartmentId = d.Id
WHERE
    3 > (SELECT
            COUNT(DISTINCT e2.Salary)
        FROM
            Employee e2
        WHERE
            e2.Salary > e1.Salary
                AND e1.DepartmentId = e2.DepartmentId
        )
;

9.178. Rank Scores

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.

IdScore
13.50
23.65
34.00
43.85
54.00
63.65

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

ScoreRank
4.001
4.001
3.852
3.653
3.653
3.504

Solution:

SELECT Scores.Score,COUNT(Ranking.Score) AS Rank FROM
Scores,(
            SELECT DISTINCT Score
            FROM Scores
        ) AS Ranking
WHERE Scores.Score<=Ranking.Score
GROUP BY Scores.Id,Scores.Score
ORDER BY Scores.Score DESC
SELECT
@rank
FROM
  (SELECT @rank := 0) init
update goods  
set price = (  
case   
  when price between 0 and 99 then price * 1.2  
  when price between 100 and 999 then price * 1.1  
  when price between 1000 and 1999 then price * 1.05  
  when price > 1999 then price * 1.02  
end);  
select * from goods;  

更多关于Mysql设置变量问题
https://www.cnblogs.com/genialx/p/5932558.html

SELECT Score,
@rank:= @rank + (@prev !=(@prev:=Score)) Rank
from
Scores,(SELECT @rank := 0,@prev := -100) init
order by Score desc

10.177. Nth Highest Salary

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

IdSalary
1100
2200
3300

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.

getNthHighestSalary(2)
200

My solution:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      # Write your MySQL query statement below.
      SELECT t1.Salary FROM Employee t1
      ,(
          SELECT DISTINCT Salary FROM
          Employee
        ) AS t2
      WHERE t1.Salary>=t2.Salary AND
      N=COUNT(t2.Salary)
      
      
  );
END
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    declare M int;
    set M = N-1;
  RETURN (
      
      # Write your MySQL query statement below.
      
      select distinct Salary from Employee
      order by Salary DESC limit 1 offset M
      
  );
END


Solution:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
SET M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT DISTINCT Salary FROM Employee
      ORDER BY Salary DESC LIMIT M,1
      
      
  );
END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值