当前位置:网站首页>Mysql database DQL exercise

Mysql database DQL exercise

2022-06-22 22:35:00 Fire eye Dragon

Table and data for exercise 1 :

CREATE TABLE student(
id INT,
name VARCHAR(20),
gender VARCHAR(20),
chinese INT,
english INT,
math INT
);

INSERT INTO student(id, name, gender, chinese, english, math) VALUES (1, ' Zhang Ming ', ' male ', 89, 78, 90);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES  (2, ' Li Jin ', ' male ', 67, 53, 95);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES (3, ' Wang Wu ', ' Woman ', 87, 78, 77);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES  (4, ' Li Yi ', ' Woman ', 88, 98, 92);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES (5, ' Li Cai ', ' male ', 82, 84, 67);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES (6, ' Zhang Bao ', ' male ', 55, 85, 45);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES  (7, ' Huang Rong ', ' Woman ', 75, 65, 30);
INSERT INTO student(id, name, gender, chinese, english, math) VALUES  (7, ' Huang Rong ', ' Woman ', 75, 65, 30);

Exercise one :

  • Query the information of all students in the table
  • Look up the names of all the students and their English scores
  • Filter duplicate data in table
  • Count the total score of each student
  • Add... To the total score of all students 10 Points of specialty points of
  • Use aliases to indicate student scores
  • Query English score greater than 90 Classmate
  • Total query score greater than 200 All students
  • Query English scores in 80-90 Between the students
  • Query English score is not in 80-90 Between the students
  • Query math score is 89,90,91 Classmate
  • Check the English scores of all students surnamed Li
  • Query math score 80 And Chinese is divided into 80 Classmate
  • Query English 80 Or total score 200 Classmate
  • Output the math scores in descending order
  • After sorting the total score, output , And then output it from high to low
  • Sort and output the total scores of students surnamed Li
  • How many boys and girls are there , And output the number of people in descending order

Answer 1 :

--   Query the information of all students in the table  
SELECT * FROM student;
--   Look up the names of all the students and their English scores  
SELECT name,english FROM student;
--   Filter duplicate data in table  
SELECT DISTINCT * FROM student;

--  Count the total score of each student  
SELECT name,chinese+english+math as total_score FROM student;

--   Add... To the total score of all students 10 Points of specialty points of 
SELECT name,chinese+english+math+10 as total_score FROM student;
--   Use aliases to indicate student scores 
SELECT name,chinese ' Chinese language and literature ',english ' English ',math ' mathematics ' FROM student;
--   Query English score greater than 90 Classmate  
SELECT * FROM student WHERE english > 90;
--   Total query score greater than 200 All students  
SELECT * FROM student WHERE (chinese+english+math) > 200;
SELECT name,chinese+english+math+10 as total_score FROM student WHERE (chinese+english+math) > 200;


--   Query English scores in 80-90 Between the students 
SELECT * FROM student WHERE english >=80 AND english<90;
--   Query English score is not in 80-90 Between the students 
SELECT * FROM student WHERE  NOT (english >=80 AND english<90);
SELECT * FROM student WHERE  english not BETWEEN 80 and 90;
SELECT * FROM student WHERE  english>=80 and english<=90;

--   Query math score is 89,90,91 Classmate 
SELECT * FROM student WHERE math=89 or math=90 or math = 91;
SELECT * FROM student WHERE math in (89,90,91);
--   Check the English scores of all students surnamed Li 
SELECT * FROM student WHERE name  LIKE ' Li %';
--    Query math score 80 And Chinese is divided into 80 Classmate 
SELECT name,math,chinese FROM student WHERE math = 80 AND chinese=80;
--   Query English 80 Or total score 200 Classmate 
SELECT * FROM student WHERE english = 80 and (chinese+math+english)=200;
--  Output the math scores in descending order 
SELECT * FROM student ORDER BY math DESC;
--   After sorting the total score, output , And then output it from high to low 
SELECT * FROM student ORDER BY (chinese+math+english) DESC;
--   Sort and output the total scores of students surnamed Li 
SELECT * FROM student WHERE name like' Li %' ORDER BY (chinese+math+english) DESC;
--   How many boys and girls are there , And output the number of people in descending order 
SELECT gender,COUNT(*) FROM student GROUP BY gender ORDER BY COUNT(*) DESC;

Tables and data for exercise 2 :

CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate date,
sal INT,
comm INT,
deptno INT
);

INSERT INTO emp VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20);
INSERT INTO emp VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
INSERT INTO emp VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
INSERT INTO emp VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20);
INSERT INTO emp VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
INSERT INTO emp VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30);
INSERT INTO emp VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10);
INSERT INTO emp VALUES (7788, 'SCOTT', 'ANALYST', 7566, '7987-04-19', 3000, NULL, 20);
INSERT INTO emp VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10);
INSERT INTO emp VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
INSERT INTO emp VALUES (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, NULL, 20);
INSERT INTO emp VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30);
INSERT INTO emp VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20);
INSERT INTO emp VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);

Exercise 2 :

  • It is not listed in ascending order of employee number 10 Employee information of department No
  • The second letter of the query name is not “A” And the salary is greater than 1000 Employee information for , In descending order of annual salary
  • Average salary for each department
  • Ask for the highest salary in each department
  • Ask for the highest salary for each position in each department
  • The average salary is greater than 2000 Department number of
  • The average salary of the Department will be greater than 1500 The department number of the , Rank by department average salary in descending order
  • Choose the name of the employee in the company who has the bonus , Wages
  • Find out the difference between the maximum wage and the minimum wage

Answer two

--   It is not listed in ascending order of employee number 10 Employee information of department No 
SELECT * FROM emp WHERE deptno !=10 ORDER BY empno ASC;
--   The second letter of the query name is not “A” And the salary is greater than 1000 Employee information for , In descending order of annual salary 
SELECT * FROM emp WHERE ename NOT LIKE'_A%' AND sal >1000 ORDER BY (12*sal+IFNULL(comm,0)) DESC;
--  notes :ifnull(comm,0) If comm The value of is null, As 0, Otherwise, it's still the original value .
--   Average salary for each department 
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno;
--   Ask for the highest salary in each department 
SELECT deptno,MAX(sal) FROM emp GROUP BY deptno;
--   Ask for the highest salary for each position in each department 
SELECT deptno,job,MAX(sal) FROM emp GROUP BY deptno,job;
--   The average salary is greater than 2000 Department number of 
SELECT deptno,AVG(sal) FROM emp  GROUP BY deptno HAVING AVG(sal)>2000;
--   The average salary of the Department will be greater than 1500 The department number of the , Rank by department average salary in descending order 
SELECT deptno,AVG(sal) FROM emp  GROUP BY deptno HAVING AVG(sal)>1500  ORDER BY AVG(sal) DESC;
--   Choose the name of the employee in the company who has the bonus , Wages 
SELECT * FROM emp WHERE comm is NOT NULL;
--   Find out the difference between the maximum wage and the minimum wage 
SELECT MAX(sal)-MIN(sal) FROM emp; 
原网站

版权声明
本文为[Fire eye Dragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221824155449.html