直接用navicate:
然后
一路下去就OK:
直接用导出功能:
OK!
创建Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
创建Department 表,包含公司所有部门的信息。
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
(1)新建表
CREATE TABLE Employee(
Id INT PRIMARY KEY,
Name VARCHAR(10),
Salary INT,
DepartmentId INT)
CREATE TABLE Department(
Id INT PRIMARY KEY,
Name VARCHAR(10))
(2)插入数据
INSERT INTO Employee VALUE('1','Joe','70000','1');
INSERT INTO Employee VALUE('2','Henry','80000','2');
INSERT INTO Employee VALUE('3','Sam','60000','2');
INSERT INTO Employee VALUE('4','Max','90000','1');
INSERT INTO Department VALUE('1','IT');
INSERT INTO Department VALUE('2','Sales');
(3)查询
SELECT c1.department,c1.employee,c1.Salary
FROM
(SELECT Department.name AS department,Employee.name As employee,Salary
FROM Employee INNER JOIN Department
ON Employee.DepartmentId=Department.Id) AS c1,
(SELECT Department.name AS department,Employee.name As employee,Salary
FROM Employee INNER JOIN Department
ON Employee.DepartmentId=Department.Id) AS c2
WHERE c1.department=c2.department
AND c1.Salary>c2.Salary;
这里说一句,只要FROM里的东西是一张表,都可以在FROM里,我们这里最外层FROM里的东西是:
SELECT Department.name AS department,Employee.name As employee,Salary
FROM Employee INNER JOIN Department
ON Employee.DepartmentId=Department.Id;
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。 其中纵列的 id 是连续递增的 小美想改变相邻俩学生的座位。 你能不能帮她写一个 SQL query 来输出小美想要的结果呢? 请创建如下所示seat表: 示例:
假如数据输入的是上表,则输出结果如下:
注意: 如果学生人数是奇数,则不需要改变最后一个同学的座位。
(1)新建表
CREATE TABLE seat(
id INT ,
student VARCHAR(20)
)
(2)插入数据
INSERT INTO seat VALUE('1','Abbot');
INSERT INTO seat VALUE('2','Doris');
INSERT INTO seat VALUE('3','Emerson');
INSERT INTO seat VALUE('4','Green');
INSERT INTO seat VALUE('5','Jeames');
(3)查询
SELECT *
FROM (
-- 遇到偶数往上移一个位置
SELECT id-1 AS id,student
FROM seat
WHERE id%2=0
UNION
-- 遇到奇数往下移一个位置
SELECT id+1 AS id,student
FROM seat
WHERE id%2=1 AND (id+1)<=(SELECT COUNT(*) FROM seat)
UNION
-- 处理最后一个位置,这里只考虑奇数情况,保持不变(偶数已经在第一步里处理了)
SELECT id AS id,student
FROM seat
WHERE id%2=1 AND(id+1)>(SELECT COUNT(*) FROM seat)
) AS c1
ORDER BY id ASC;
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。 创建以下score表:
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):
(1)新建表
CREATE TABLE scores(
ID INT,
Score FLOAT);
(2)插入数据
INSERT INTO scores VALUE('1','3.50');
INSERT INTO scores VALUE('2','3.65');
INSERT INTO scores VALUE('3','4.00');
INSERT INTO scores VALUE('4','3.85');
INSERT INTO scores VALUE('5','4.00');
INSERT INTO scores VALUE('6','3.65');
(3)查询
SELECT score,(SELECT COUNT(DISTINCT score)
FROM scores
WHERE score>=s.score) AS ran_k
FROM scores AS s
ORDER BY score DESC;