SQL基础语句练习
Posted Weikun Xing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL基础语句练习相关的知识,希望对你有一定的参考价值。
文章目录
创建数据库数据表
test_db数据库
create database test_db;
use test_db;
数据都是造的,不必当真
学生表:stu
字段名称 | 字段类型 | 说明 |
---|---|---|
sid | char(6) | 学生学号 |
sname | varchar(50) | 学生姓名 |
age | int | 学生年龄 |
gender | varchar(50) | 学生性别 |
CREATE TABLE stu (
sid CHAR(6),
sname VARCHAR(50),
age INT,
gender VARCHAR(50)
);
插入数据
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
雇员表:emp
字段名称 | 字段类型 | 说明 |
---|---|---|
empno | int | 员工编号 |
ename | varchar(50) | 员工姓名 |
job | varchar(50) | 员工工作 |
mgr | int | 领导编号 |
hiredate | date | 入职日期 |
sal | decimal(7,2) | 月薪 |
comm | decimal(7,2) | 奖金 |
deptno | int | 部分编号 |
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm decimal(7,2),
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,'1987-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);
部门表:dept
字段名称 | 字段类型 | 说明 |
---|---|---|
deptno | int | 部分编码 |
dname | varchar(50) | 部分名称 |
loc | varchar(50) | 部分所在地点 |
CREATE TABLE dept(
deptno INT,
dname varchar(14),
loc varchar(13)
);
INSERT INTO dept values(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept values(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept values(30, 'SALES', 'CHICAGO');
INSERT INTO dept values(40, 'OPERATIONS', 'BOSTON');
练习题
1、查询性别为女,并且年龄小于50的记录
select * from stu where gender like 'female' and age<50;
2、查询学号为S_1001,或者姓名为liSi的记录
select * from stu where sid like 'S_1001' or sname like 'liSi';
3、查询学号为S_1001,S_1002,S_1003的记录
select * from stu where sid in ('S_1001','S_1002','S_1003');
4、查询学号不是S_1001,S_1002,S_1003的记录
select * from stu where sid not in ('S_1001','S_1002','S_1003');
5、查询年龄为null的记录
select * from stu where age is null;
6、查询年龄在20到40之间的学生记录
select * from stu where age between 20 and 40;
7、查询性别非男的学生记录
select * from stu where gender not like 'male';
select * from stu where gender<>'male';
8、查询姓名不为null的学生记录
select * from stu where sname is not null;
9、查询姓名由5个字母构成的学生记录
select * from stu where sname like '_____';
10、查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
select * from stu where sname like '____i';
11、查询姓名以“z”开头的学生记录
select * from stu where sname like 'z%';
12、查询姓名中第2个字母为“i”的学生记录
select * from stu where sname like '_i%';
13、查询姓名中包含“a”字母的学生记录
select * from stu where sname like '%a%';
14、查询所有学生记录,按年龄升序排序
select * from stu order by age;
15、查询所有学生记录,按年龄降序排序
select * from stu order by age desc;
16、查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
select * from emp order by sal desc,empno;
17、查询emp表中月薪大于2500的人数:
select count(*) as 'sal>2500' from emp where sal>2500;
18、统计月薪与佣金之和大于2500元的人数:
select count(*) as 'sal+comm>2500' from emp where sal+ifnull(comm,0)>2500;
19、查询有佣金的人数,以及有领导的人数:
SELECT COUNT(comm), COUNT(mgr) FROM emp;
20、查询所有雇员月薪和,以及所有雇员佣金和:
select sum(sal),sum(comm) from emp;
21、查询所有雇员月薪+佣金和:
select sum(sal+ifnull(comm,0)) from emp;
22、统计所有员工平均工资:
select sum(sal)/count(sal) from emp;
select avg(sal) from emp;
23、查询每个部门的部门编号和每个部门的工资和:
select deptno,sum(sal) from emp group by deptno;
24、查询每个部门的部门编号以及每个部门的人数:
select deptno,count(empno) from emp group by deptno;
25、查询每个部门的部门编号以及每个部门工资大于1500的人数:
select deptno,count(sal) from emp where sal>1500 group by deptno;
26、查询工资总和大于9000的部门编号以及工资和:
select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;
27、查询5行记录,起始行从0开始
select * from emp limit 0,5;
28、查询10行记录,起始行从3开始
select * from emp limit 3,10;
这些题是基础题,提升可以去力扣或者牛客上刷题
以上是关于SQL基础语句练习的主要内容,如果未能解决你的问题,请参考以下文章