MySQL实战

task7

mysql> create table if not exists Employee (
    -> Id int not null primary key,
    -> Name VARCHAR(50) not null,
    -> Salary int not null,
    -> DepartmentId int not null
    -> );
mysql> insert into Employee values(1,'Joe',70000,1);
mysql> insert into Employee values(2,'Henry',80000,2);
mysql> insert into Employee values(3,'Sam',60000,2);
mysql> insert into Employee values(4,'Max',90000,1);
mysql> select * from Employee;
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
|  1 | Joe   |  70000 |            1 |
|  2 | Henry |  80000 |            2 |
|  3 | Sam   |  60000 |            2 |
|  4 | Max   |  90000 |            1 |
+----+-------+--------+--------------+
mysql> create table if not exists Department (
    -> Id int not null primary key,
    -> Name VARCHAR(50) not null
    -> );
mysql> insert into Department values(1,'IT');
mysql> insert into Department values(2,'Sales');
mysql> select * from Department;
+----+-------+
| Id | Name  |
+----+-------+
|  1 | IT    |
|  2 | Sales |
+----+-------+
mysql> select E.Name as Name,E.Salary,D.Name as department
    -> from Employee E join Department D on E.DepartmentId=D.Id
    -> where (E.Salary,E.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);
+-------+--------+------------+
| Name  | Salary | department |
+-------+--------+------------+
| Henry |  80000 | Sales      |
| Max   |  90000 | IT         |
+-------+--------+------------+

task8

mysql> create table if not exists seat (
    -> id int not null primary key,
    -> student VARCHAR(50) not null
    -> );
mysql> insert into seat values(1,'Abbot');
mysql> insert into seat values(2,'Doris');
mysql> insert into seat values(3,'Emerson');
mysql> insert into seat values(4,'Green');
mysql> insert into seat values(5,'Jeames');
mysql> select * from seat;
+----+---------+
| id | student |
+----+---------+
|  1 | Abbot   |
|  2 | Doris   |
|  3 | Emerson |
|  4 | Green   |
|  5 | Jeames  |
+----+---------+
mysql> select id,student from (
    -> select id-1 as id, student from seat where mod(id,2)=0
    -> union
    -> select id+1 as id, student from seat where mod(id,2)=1
    -> and id!=(select count(*) from seat)
    -> union
    -> select id,student from seat where mod(id,2)=1
    -> and id=(select count(*) from seat)
    -> ) seat order by id;
+----+---------+
| id | student |
+----+---------+
|  1 | Doris   |
|  2 | Abbot   |
|  3 | Green   |
|  4 | Emerson |
|  5 | Jeames  |
+----+---------+

task9

mysql> create TABLE scores(
    -> Id int not null,
    -> Score float(3,2)
    -> );
mysql> insert into scores values(1,3.50);
mysql> insert into scores values(2,3.65);
mysql> insert into scores values(3,4.00);
mysql> insert into scores values(4,3.85);
mysql> insert into scores values(5,4.00);
mysql> insert into scores values(6,3.65);
mysql> select Score,
    -> (select count(distinct Score)
    -> from scores where Score>=s.Score) rank
    -> from scores s order by score desc;
+-------+------+
| Score | rank |
+-------+------+
|  4.00 |    1 |
|  4.00 |    1 |
|  3.85 |    2 |
|  3.65 |    3 |
|  3.65 |    3 |
|  3.50 |    4 |
+-------+------+

你可能感兴趣的:(mysql)