Query operation experiment of MySQL database table data

one. Verification experiment
Topic requirements:
Perform information query on the bumen table and yuangong table of the company's departmental employee management database. The definition of Bumen table and yuangong table is shown in the table.
insert image description here
insert image description here
Practice data of bumen table:
INSERT INTO bumen(1001,'Personnel Department','Personnel Management','Beijing');
INSERT INTO bumen(1002,'Research Department','R&D Products','Beijing');
INSERT INTO bumen(1003,'Production Department','Product Production','Tianjin');
INSERT INTO bumen(1004,'Sales Department','Product Sales','Shanghai');
insert image description here
Practice data of yuangong table:
INSERT INTO yuangong( 8001,'Han Peng','Male',25,1002,4000,'Beijing Haidian District');
INSERT INTO yuangong(8002,'Zhang Feng','Male',26,1001,2500,'Beijing Changping District ');
INSERT INTO yuangong(8003,'Ouyang','Male',20,1003,1500,'Yongzhou City, Hunan Province');
INSERT INTO yuangong(8004,'Wang Wu','Male',30,1001, 3500,'Shunyi District, Beijing');
INSERT INTO yuangong(8005,'Ouyang Baby','Female',21,1002,3000,'Changping District, Beijing');
INSERT INTO yuangong(8006,'Huyan',' Male',28,1003,1800,'Nankai District, Tianjin');
insert image description here
Then query records in the bumen table and yuangong table. The query requirements are as follows:
(1) Query all records in the yuangong table.
SELECT * FROM yuangong;
insert image description here
or list all field names of the yuangong table.
SELECT id,name,sex,age,d_id,salary,address FROM yuangong;
insert image description here
(2) Query the fourth to fifth records of the yuangong table.
SELECT id,name,sex,age,d_id,salary,address FROM yuangong LIMIT 3,2;
insert image description here
(3) Query the department number (d_id), department name (d_name) and department function (function) from the bumen table.
SELECT d_id,d_name,function FROM bumen;
insert image description here
(4) Query the personnel information of the personnel department and the scientific research department from the yuangong table. First query the department numbers of the personnel department and scientific research department from the bumen table. Then go to the yuangong table to query the employee's information.
SELECT * FROM yuangong
WHERE d_id=ANY(
SELECT d_id FROM bumen
WHERE d_name IN('Personnel Department','Research Department'));
Or use the following code.
SELECT * FROM yuangong
WHERE d_idIN(
SELECT d_id FROM bumen
WHERE d_name='Personnel Department' ORd_name='Research Department');
insert image description here
(5) Query the information of employees whose age is between 25 and 30 from the yuangong table. There are two ways to query.
The first method:
SELECT * FROM yuangong WHERE age BETWEEN 25 AND 30;
The second method:
SELECT * FROM yuangong WHERE age>=25 AND age<=30;
insert image description here
(6) Query how many employees there are in each department. First group by department number, and then use the COUNT() function to calculate the number of people in each group.
SELECT d_id, COUNT(id) FROM yuangong GROUP BY d_id;
insert image description here
or name COUNT(id) sum.
SELECT d_id,COUNT(id) AS sum FROM yuangong GROUP BY d_id;
insert image description here
(7) Query the maximum salary of each department. First group by department number, and then use the MAX() function to calculate the maximum value.
SELECT d_id, MAX(salary) FROM yuangong GROUP BY d_id;
insert image description here
(8) Query bumen table and yuangong table by left join.
Use LEFT JOINON to achieve left join.
SELECT bumen.d_id,d_name,function,bumen.address,id,name,age,sex,salary,yuangong.address
FROM bumen LEFT JOIN yuangong ON yuangong.d_id=bumen.d_id;
insert image description here
(9) Calculate the total salary of each department. First group by department number, and then use the SUM() function to sum.
SELECT d_id, SUM(salary) FROM yuangong GROUP BY d_id;
insert image description here
(10) Query the yuangong table and arrange them in descending order of salary.
SELECT * FROM yuangong ORDER BY salary DESC;
insert image description here
(11) Query the department number from the bumen table and yuangong table, and then use UNION to combine the query results.
SELECT d_id FROM yuangong UNION SELECT d_id FROM bumen;
insert image description here
(12) The query home is the name, age, and home address of employees in Beijing. The LIKE keyword is used here.
SELECT name, age, address FROM yuangong WHERE address LIKE 'Beijing%';
insert image description here
2. Design experiment The
query will be performed on the student table and the score table. The definitions of the Student table and the score table are shown in the table:
insert image description here
After the table is created successfully, check the structure of the two tables.
Student practice data are as follows:
901, 'Zhang Jun', 'male', 1985, 'Computer Department', 'Beijing Haidian District'
902, 'Zhang Chao', 'male', 1986, 'Chinese Department', 'Beijing Changping District'
903,'Zhang Mei','Female',1990,'Chinese Department','Yongzhou City, Hunan Province'
904,'Li Wuyi','Male',1990,'English Department','Fuxin City, Liaoning Province' 905,'Wang Fang','Female',1991,
'English Department','Xiamen City, Fujian Province'
906 ,'Wang Gui','Male',1988,'Department of Computer Science','Hengyang City, Hunan Province'score
insert image description here
table exercise data are as follows:
01,'Computer',901,98
02,'English',,901,80
03, 'Computer', 902, 65
04, 'Chinese', 902, 88
05, 'Chinese', 903
, 95 06, 'Computer', 903, 70
07, 'English', 904, 92
08, 'English', 905, 94
09, 'Computer', 906, 90
10, 'English', 906, 85
insert image description here
and then perform table operations according to the following requirements:
(1) Query all records in the student table.
Method 1: use "*".
SELECT * FROM student;
insert image description here
Method 2: List all column names.
SELECT num,name,sex,birthday,bumen,address FROM student;
insert image description here
(2) Query the second to fourth records of the student table.
SELECT num,name,sex,birthday,bumen,address FROM student LIMIT 3,2;
insert image description here
(3) Query the student number, name and department information of all students from the student table.
SELECT num,name,bumen FROM student;
insert image description here
Query the information of students from the Department of Computer Science and Department of English.
Method 1: Use the IN keyword
SELECT
*
FROM
student
WHERE
bumen IN ('Computer Department', 'English Department');
insert image description here
Method 2: Use the OR keyword
SELECT
*
FROM
student
WHERE
bumen = 'Computer Department' OR bumen= 'English Department ';
insert image description here
(5) Query the information of students whose birthdays are from 1986 to 1990 from the student table.
Method 1: Use the BETWEEN AND keyword to query
SELECT * FROM student WHERE birthday BETWEEN 1986 AND 1990;
insert image description here
Method 2: Use the AND keyword and comparison operators.
SELECT * FROM student WHERE birthday>=1986 AND birthday<=1990;
insert image description here
(6) Query the number of students in each department in the student table, and use the alias sum_of_bumen for the counted number of people.
SELECT bumen,COUNT(num) AS sum_of_bumen FROM student GROUP BY bumen;
insert image description here
(7) Query the highest score of each subject from the score table.
SELECT stu_id,MAX(grade) FROM score GROUP BY stu_id;
insert image description here
(8) Query Li Wuyi's test subjects (c_name) and test results (grade).
SELECT
C_name,
grade
FROM
score
WHERE
stu_id = ANY (
SELECT
num
FROM
student
WHERE
NAME IN ( 'Li Wuyi' ))
insert image description here
(9) Query all student information and exam information by means of connection query.
insert image description here
insert image description here
(10) Calculate the total score of each student (the student's name needs to be displayed).
insert image description here
(11) Calculate the average score of each test subject.
Select C_name,AVG(grade) AVG_C_name from score
Group by C_name;
insert image description here
(12) Query the information of students whose computer scores are lower than 95.
select * FROM student where num in (select stu_id from score where C_name = 'computer' and grade <95);
insert image description here
(13) Query the information of students who take computer and English exams at the same time.
select * FROM student where num = ANY(select Stu_id from score where Stu_id in(select Stu_id from score where C_name ='Computer') and C_name ='English'); (14) Sort the computer scores from high to low
insert image description here
.
select stu_id, grade from score where C_name =-'computer' order by grade DESC;
insert image description here
(15) Query the student's student ID from the student table and score table, and then combine the query results.
insert image description here
(16) Inquire about the names, departments, examination subjects and grades of students surnamed Zhang or Wang.
select s.name,s.bumen,c.C_name,c.grade from student s,score c where (s.name LIKE '张%' or s.name like '王%')and s.num =c.stu_id ;
insert image description here
(17) The query is the name, age, department, examination subjects and grades of students from Hunan.
select s.name,s.bumen,c.C_name,c.grade from student s,score c where s.address like 'Hunan%' and s.num =c.stu_id;
insert image description here

Guess you like

Origin blog.csdn.net/m0_55726741/article/details/129248013