MySQL study notes punch card-4

4.1 MySQL combat

#Learning content#
Data import and export Export
any MySQL table created before, and it is in CSV/XLS format, and
then import the CSV/XLS table into the database

-Export table (email table, xls format)
Insert picture description here
right-click-Export Wizard-select Excel data sheet ( .xls)-set the export path and file name-continue to the next step-export success*

-result
Insert picture description here

--Import the email (*.xls) table exported in the previous step,
right-click the table under the target database, and select Import Wizard
Insert picture description here

Select Excel file (*.xls)-select the file to be imported (email.xls)-check the table name (the table name can be changed, in order not to duplicate the original table name, this table uses the table name by default: sheet1)

-result
Insert picture description here

operation

Project 7: The employee with the highest salary in each department (difficulty: medium)
creates an Employee table, which contains all employee information, and each employee has its corresponding Id, salary and department Id.
±—±------±-------±-------------+
| Id | Name | Salary | DepartmentId |
±—±------ ±-------±-------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
±—±------±-------±-------------+
Create a Department table, including all departments of the company information.
±—±---------+
| Id | Name |
±—±---------+
| 1 | IT |
| 2 | Sales |
±—±------ ---+
Write a SQL query to find the highest paid employee in each department. For example, according to the table given above, Max has the highest salary in the IT department, and Henry has the highest salary in the Sales department.
±-----------±---------±-------+
| Department | Employee | Salary |
±-----------±---------±-------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
±-----------±---------±-------+

-Create table (Employee)

CREATE TABLE Employee
(Id INT NOT NULL PRIMARY KEY,
 name VARCHAR(255) NOT NULL,
 salary INT NOT NULL,
 department INT NOT NULL);

-result

-Insert data

INSERT INTO Employee VALUES(1, 'Joe', 70000, 1);
INSERT INTO Employee VALUES(2, 'Henry', 80000, 2);
INSERT INTO Employee VALUES(3, 'Sam', 60000, 2);
INSERT INTO Employee VALUES(4, 'Max', 90000, 1);

-result
Insert picture description here

-Create table (Department)

CREATE TABLE Department
(Id INT NOT NULL PRIMARY KEY,
 Name VARCHAR(255) NOT NULL);

-result
Insert picture description here

-Insert data

INSERT INTO Department VALUES(1, 'IT');
INSERT INTO Department VALUES(2, 'Sales');

-result
Insert picture description here

-Code (write a SQL query to find the employee with the highest salary in each department. For example, according to the table given above, Max has the highest salary in the IT department, and Henry has the highest salary in the Sales department)

SELECT E.name, D.Name
FROM Employee AS E
JOIN Department AS D
ON E.department = D.id
GROUP BY D.Name;
HAVING E.salary = max(E.salary)

-result
Insert picture description here

Item 8: Changing seats (difficulty: medium)

Xiaomei is an information technology teacher in a middle school. She has a seat table, which is usually used to store the names of students and their corresponding seat ids.
The id in the column is continuously increasing.
Xiaomei wants to change the seats of two adjacent students.
Can you help her write a SQL query to output the result Xiaomei wants?
Please create a seat table as shown below:
Example:
±--------±--------+
| id | student |
±--------±------ --+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
±--------±--------+
If data is entered Is the above table, the output result is as follows:
±--------±--------+
| id | student |
±--------±------ --+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
±--------±--------+
Note:
If If the number of students is odd, there is no need to change the seat of the last student.

-Create a table (seat)

CREATE TABLE seat
(id INT NOT NULL PRIMARY KEY,
 student VARCHAR(255) NOT NULL);

-Insert data

INSERT INTO seat VALUES (1, 'Abbot');
INSERT INTO seat VALUES (2, 'Doris');
INSERT INTO seat VALUES (3, 'Emerson');
INSERT INTO seat VALUES (4, 'Green');
INSERT INTO seat VALUES (5, 'Jeames');

-result
Insert picture description here

-Code (I want to change the seats of two adjacent students, if the number of students is odd, you do not need to change the seat of the last student)

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)) s 
ORDER BY id;

-result
Insert picture description here

Item 9: Score ranking (difficulty: medium)

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 place after the tie should be the next consecutive integer value. In other words, there should be no "gap" between the rankings.
Create the following score table:
±—±------+
| 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 |
±------±-----+

-Create a table (Score)

CREATE TABLE score
(Id INT NOT NULL PRIMARY KEY,
 Score DECIMAL(5,2) NOT NULL);

-Insert data

INSERT INTO score VALUES (1, 3.50);
INSERT INTO score VALUES (2, 3.65);
INSERT INTO score VALUES (3, 4.00);
INSERT INTO score VALUES (4, 3.85);
INSERT INTO score VALUES (5, 4.00);
INSERT INTO score VALUES (6, 3.65);

-Code (write a SQL query to achieve score ranking. If two scores are the same, the two scores rank (Rank) is the same)

SELECT s1.Score, COUNT(DISTINCT s2.Score) AS Rank
FROM score AS s1
JOIN score AS s2 
ON s1.Score <= s2.Score
GROUP BY s1.Id
ORDER BY s1.Score DESC;

-result
Insert picture description here

4.2 MySQL Actual Combat-Complex Project

#operation#

Project 10: Itinerary and users (difficulty: difficult)

The trip information of all taxis is stored in the Trips table. Each trip has a unique key Id. Client_Id and Driver_Id are foreign keys of Users_Id in the Users table. Status is an enumeration type, and the enumeration members are ('completed','cancelled_by_driver','cancelled_by_client').
±—±----------±----------±--------±---------------- ---±---------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
±—±----------±--------- -±--------±-------------------±---------+
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|
| 3 | 3 | 12 | 6 | completed |2013-10-01|
| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|
| 5 | 1 | 10 | 1 | completed |2013-10-02|
| 6 | 2 | 11 | 6 | completed |2013-10-02|
| 7 | 3 | 12 | 6 | completed |2013-10-02|
| 8 | 2 | 12 | 12 | completed |2013-10-03|
| 9 | 3 | 10 | 12 | completed |2013-10-03|
| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|
±—±----------±-- --------±--------±-------------------±--------+
Users table storage All users. Each user has a unique key Users_Id. Banned indicates whether this user is banned, and Role is an enumerated type that indicates ('client','driver','partner').
±---------±-------±-------+
| Users_Id | Banned | Role |
±---------±----- --±-------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
±---------±-------±-------+
Write a SQL statement to find out Cancellation rate for non-banned users from October 1, 2013 to October 3, 2013. Based on the above table, your SQL statement should return the following result, with the cancellation rate to two decimal places.
±-----------±------------------+
| Day | Cancellation Rate |
±-----------±------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
±-----------±------------------+

-Create table (Trips)

CREATE TABLE Trips
(Id INT NOT NULL PRIMARY KEY,
 Client_Id INT NOT NULL,
 Driver_Id INT NOT NULL,
 City_Id INT NOT NULL,
 Status enum('completed', 'cancelled_by_driver', 'cancelled_by_client'),
 Request_at DATE);

-Insert data (Trips)

INSERT INTO Trips VALUES (1, 1, 10, 1, 'completed', '2013-10-01');
INSERT INTO Trips VALUES (2, 2, 11, 1, 'cancelled_by_driver', '2013-10-01');
INSERT INTO Trips VALUES (3, 3, 12, 6, 'completed', '2013-10-01');
INSERT INTO Trips VALUES (4, 4, 13, 6, 'cancelled_by_client', '2013-10-01');
INSERT INTO Trips VALUES (5, 1, 10, 1, 'completed', '2013-10-02');
INSERT INTO Trips VALUES (6, 2, 11, 6, 'completed', '2013-10-02');
INSERT INTO Trips VALUES (7, 3, 12, 6, 'completed', '2013-10-02');
INSERT INTO Trips VALUES (8, 2, 12, 12, 'completed', '2013-10-03');
INSERT INTO Trips VALUES (9, 3, 10, 12, 'completed', '2013-10-03');
INSERT INTO Trips VALUES (10, 4, 13, 12, 'cancelled_by_driver', '2013-10-03');

-Create table (Users)

CREATE TABLE Users
(Users_Id INT NOT NULL PRIMARY KEY,
 Banned enum('Yes', 'No'),
 Role enum('client', 'driver', 'partner'));

-Insert data (Users)

INSERT INTO Users VALUES(1, 'No', 'client');
INSERT INTO Users VALUES(2, 'Yes', 'client');
INSERT INTO Users VALUES(3, 'No', 'client');
INSERT INTO Users VALUES(4, 'No', 'client');
INSERT INTO Users VALUES(10, 'No', 'driver');
INSERT INTO Users VALUES(11, 'No', 'driver');
INSERT INTO Users VALUES(12, 'No', 'driver');
INSERT INTO Users VALUES(13, 'No', 'driver');

-Code (check out the cancellation rate of non-banned users from October 1, 2013 to October 3, 2013, the cancellation rate (Cancellation Rate) should be two decimal places)

SELECT a.request_at AS Day,
ROUND(
SUM(CASE WHEN a.status = 'completed' THEN 0 ELSE 1 END)/COUNT(*),2) 
AS 'Cancellation Rate'
FROM trips a
JOIN users b
ON a.Client_Id = b.Users_Id AND b.Banned = 'No'
WHERE a.Request_at BETWEEN '2013-10-01'AND '2013-10-03'
GROUP BY a.Request_at;

-Results
Insert picture description here
http://www.mamicode.com/info-detail-2651451.html

Project 11: Top 3 high-paid employees in each department (difficulty: medium)

Clear the employee table in item 7, and re-insert the following data (in fact, insert 5 and 6 more rows):
±—±------±-------±------- ------+
| Id | Name | Salary | DepartmentId |
±—±------±-------±-------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
±—±------±-------±-------------+
Write a SQL query to find the top three employees in each department . For example, according to the table given above, the query result should return:
±-----------±---------±-------+
| Department | Employee | Salary |
±-----------±---------±-------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
±-----------±---------±-------+

In addition, please consider implementing the function of the top N high wage employees in each department.

-Create table (Employee)

CREATE TABLE Employee
(Id INT NOT NULL PRIMARY KEY,
 Name VARCHAR(255) NOT NULL,
 Salary INT NOT NULL,
 Departmentld INT NOT NULL);

-Insert data (Employee)

INSERT INTO Employee VALUES(1, 'Joe', 70000, 1);
INSERT INTO Employee VALUES(2, 'Henry', 80000, 2);
INSERT INTO Employee VALUES(3, 'Sam', 60000, 2);
INSERT INTO Employee VALUES(4, 'Max', 90000, 1);
INSERT INTO Employee VALUES(5, 'Janet', 69000, 1);
INSERT INTO Employee VALUES(6, 'Randy', 85000, 1);

-Create table (Department)

CREATE TABLE Department
(Id INT NOT NULL PRIMARY KEY,
 Name VARCHAR(255) NOT NULL);

-Insert data (Department)

INSERT INTO Department VALUES(1, 'IT');
INSERT INTO Department VALUES(2, 'Sales');

-Code (write a SQL query to find the top three employees in each department)

SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary
FROM Employee AS e1
JOIN Employee AS e2
ON e1.Departmentld = e2.Departmentld 
AND E2.Salary >= E1.Salary 
JOIN Department AS d
ON e1.Departmentld = d.Id
GROUP BY e1.Name
HAVING COUNT(DISTINCT e2.Salary) <=3
ORDER BY d.name, e1.Salary DESC;

-result
Insert picture description here

Project Twelve Score Ranking-(Difficulty: Medium)

It is still yesterday’s score table, which implements the ranking function, but the ranking is not continuous, as follows:
±------±-----+
| Score | Rank |
±------±---- -+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 3 |
| 3.65 | 4 |
| 3.65 | 4 |
| 3.50 | 6 |
±------±-----

-Code

SELECT Score,
(SELECT COUNT(*) FROM score AS s2 
WHERE s2.Score > s1.Score)+1 AS 'Rank'
FROM score AS s1
ORDER BY Score DESC;

Guess you like

Origin blog.csdn.net/Baby1601tree/article/details/89058929