leetcode Sql topic summary

1. There is a courses table, there are: student (student) and class (course).

Please list all than or equal to 5 students in class.

select class from courses group by class having count(distinct student)>=5;

2.

Given a salary table, as shown below, there are m = f = male and female values. F and m are all exchange value (e.g., change the value of m f all, and vice versa). It requires only one update (the Update) statement, and no intermediate temporary table. Note that you will only write an Update statement, please do not write any Select statement.

方法1,update salary set sex = case sex when 'm'  then 'f' else 'm' end;

方法2,update salary set sex = char(ascii('m') + ascii('f') - ascii(sex));

 

3. Little America is an information technology school teacher, she has a seat seating chart, usually used to store students' names and their corresponding seat id. The id column which is continuously increasing, and the United States want to change two small student adjacent seats.

select (case  when mod(id,2)=1 and id=(select count(*) from seat) then id

                        when mod(id,2)=1 then id+1

                        else id-1

                        end ) as id ,student from seat order by id

 

4, to identify all non-video description  boring (not boring) for the film and id odd, the result press level  rating arrangement

select * from cinema where description !='boring' and mod(id,2)=1 order by rating desc;

 

5.

X city built a new stadium, the daily flow of people this information is recorded in the three information: the serial number (id), date (visit_date), the flow of people (people).

Please write a query to find the peak of human traffic. When the peak of at least three rows of continuous flow of not less than 100 people.

For example, Table stadium:

------ + ----------- + ------------ + +
| the above mentioned id | VISIT_DATE | people |
+ ------ + - + ----------- + ----------
|. 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
|. 3 | 2017- 01-03 | 150 |
|. 4 | 2017-01-04 | 99 |
|. 5 | 2017-01-05 | 145 |
|. 6 | 2017-01-06 | 1455 |
|. 7 | 2017-01-07 | 199 |
|. 8 | 2017-01-08 | 188 |
+ ----------------------- + ------ + ----------- +
for the example of the above data, The output is:

+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
+------+------------+-----------+

select distinct a.* from stadium a,stadium b,stadium c
where a.people>=100 and b.people>=100 and c.people>=100
and (
(a.id = b.id-1 and b.id = c.id -1) or
(a.id = b.id-1 and a.id = c.id +1) or
(a.id = b.id+1 and b.id = c.id +1)
) order by a.id

 

6. write an SQL query to get  Employee table the second highest salary (Salary). If the second-highest salary does not exist, then the query should return  null.

select ifnull((select distinct Salary from Employee order by Salary desc limit 1 offset 1), null ) as SecondHighestSalary;

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/human-traffic-of-stadium
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/cheng-cheng/p/11344127.html