Mysql single table query, multi-table query exercises

1. Single-table query
Material: Table name: worker-- the fields in the table are all in Chinese, such as department number, salary, employee number to participate in work, etc.

Create table:

CREATE TABLE worker (
 部门号 int(11) NOT NULL,
 职工号 int(11) NOT NULL,
 工作时间 date NOT NULL,
 工资 float(8,2) NOT NULL,
 政治面貌 varchar(10) NOT NULL DEFAULT '群众',
 姓名 varchar(20) NOT NULL,
 出生日期 date NOT NULL,
 PRIMARY KEY (职工号)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

INSERT INTO worker  VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO worker VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO worker VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO worker  VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO worker  VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO worker  VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');

1. Display the basic information of all employees.   

select * from worker;

2. Query the department numbers of the departments to which all employees belong, and do not display duplicate department numbers.  

select distinct 部门号 from worker ;

3. Find the number of all employees.  

select count(*) 员工人数 from worker;

4. List the highest and lowest wages.

select max(工资),min(工资) from worker;

  5. List the average salary and total salary of employees.

select avg(工资) 平均工资,SUM(工资) 总工资 FROM worker;

6. Create a new table with only the employee number, name and work participation, called the work date table. 

CREATE TABLE `工作日期` (
 `职工号` INT(11)     NOT NULL,
 `姓名` VARCHAR(20)     NOT NULL,
`工作时间` DATE     NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

7. Display the age of all employees. 

SELECT 姓名,DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(出生日期)), '%Y')+0 AS age FROM worker;

8. List the employee number, name and date of birth of all employees surnamed Liu.

SELECT 职工号,姓名,出生日期 FROM worker WHERE 姓名 LIKE '刘%';

9. List the names and working dates of employees born after 1960.

SELECT 姓名,工作时间 FROM worker WHERE 出生日期>1960-01-01;

10. List the names of all employees whose salary is between 4000-5000. 

SELECT 姓名 FROM worker WHERE 工资 BETWEEN 4000 AND 5000;

11. List all employee numbers, names, and party members whose department numbers are 101 and 102.  

SELECT 职工号,姓名,政治面貌 FROM worker WHERE 部门号=101 or 部门号=102;

12. Sort the employees in the employee table worker according to the order of birth.

SELECT * FROM worker ORDER BY 出生日期;

13. Display the employee numbers and names of the top 3 employees with the highest salary. 

SELECT 职工号,姓名 FROM worker ORDER BY 工资 DESC LIMIT 3;

14. Find the number of party members in each department. 

SELECT 部门号,COUNT(*) 党员人数 FROM worker WHERE 政治面貌='党员' GROUP BY 部门号;

15. Statistics of wages and average wages of various departments

SELECT 工资,AVG(工资) 平均工资 FROM worker GROUP BY 部门号;

16. List the department numbers and the total number of people whose total number is greater than 3.

SELECT 部门号,COUNT(*) 总人数 FROM worker GROUP BY 部门号 HAVING 总人数>3;

Two, multi-table query

-- 创建student和score表
CREATE  TABLE student (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY ,
name  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4) ,
birth  YEAR,
department  VARCHAR(20) ,
address  VARCHAR(50)
);
-- 创建score表。SQL代码如下:
CREATE  TABLE score (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  AUTO_INCREMENT ,
stu_id  INT(10)  NOT NULL ,
c_name  VARCHAR(20) ,
grade  INT(10)
);

2. Add records to the student table and score table

-- 向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
-- 向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

3. Query all records of the student table

SELECT * FROM student;

4. Query the 2nd to 4th records of the student table

SELECT * FROM student LIMIT 1,3;

5. Query the student number (id), name (name) and department (department) information of all students from the student table

SELECT id 学号,name 姓名,department 院系 FROM student;

6. Query the information of students from the computer department and the English department from the student table

SELECT * FROM student WHERE department='计算机系' or department='英语系';

7. Query the information of students aged 18~22 from the student table

SELECT  * FROM student where birth>2000 and  birth<2002;

8. Query how many people are in each department from the student table

SELECT department,count(*) 人数 from student group by department;

9. Query the highest score of each subject from the score table

SELECT c_name,max(grade) from score group by c_name;

10. Query Li Si's test subjects (c_name) and test results (grade)

select c.c_name,c.grade from student s inner join score c on s.id=c.stu_id where s.name='李四';

11. Query all student information and test information by means of connection

select * from student inner join score on student.id=score.stu_id;

12. Calculate the total grade of each student

select stu_id,sum(grade) 总成绩 from score group by stu_id;

13. Calculate the average grade for each exam subject

select c_name,avg(grade) 平均成绩 from score group by c_name;

14. Query the information of students whose computer scores are lower than 95

select  s.* from student s inner join score c on s.id=c.stu_id WHERE grade<95 and c_name='计算机';

15. Query the information of students who take both computer and English exams

select s.* from student s inner join score c on s.id=c.stu_id where c_name='计算机' and c_name='英语';

16. Sort computer test scores from high to low

select grade from score where c_name='计算机' order by grade desc;

17. Query the student ID number from the student table and score table, and then merge the query results

select s.name,c.stu_id from student s inner join score c on s.id=c.stu_id;

18. Query the name, department, examination subjects and results of students surnamed Zhang or Wang

select name,department,c_name,grade from student s  inner join score c  on s.id=c.stu_id where  s.name like '张%' or name like '王%';

19. Query the names, ages, departments, examination subjects and grades of students from Hunan

select name,department,c_name,grade from student s inner join score c on s.id=c.stu_id where address like '湖南%';

Guess you like

Origin blog.csdn.net/weixin_62107875/article/details/127484073