100道MySQL练习题,帮你熟悉sql语法【后50道】

1、查询id、货品名称、分类编号、零售价,并且零售价(salePrice)按降序排列:

 select id,productName,dir_id,salePrice from product order by salePrice desc;

[图片上传失败...(image-1549f3-1649149727008)]

2、查询id、货品名称、分类编号、零售价,并且零售价按升序排列:

 select id,productName,dir_id,salePrice from product order by salePrice asc;

[图片上传失败...(image-176a31-1649149727008)]

3、查询id、货品名称、分类编号、零售价,先按分类编号升序排列,再零售价按降序排列:

 select id,productName,dir_id,salePrice from product order by dir_Id asc,salePrice desc;

[图片上传失败...(image-4066d8-1649149727008)]

4、查询M系列的id、货品名称、并按照批发价(salePrice*cutoff)升序排列,批发价使用别名:

select
    id,
    productName,
    salePrice * cutoff as trade_price
from
    product
where
    productName like '%M%'
order by
    trade_price asc;

[图片上传失败...(image-605554-1649149727008)]

5、查询id、货品名称、分类编号,按分类编号为2的商品按照批发价(salePrice*cutoff)升序排列:

select
    id,
    productName,
    dir_id,
    salePrice * cutoff trade_price
from
    product
where
    dir_id = 2
order by
    trade_price asc;

[图片上传失败...(image-e87ea9-1649149727008)]

6、分页查询所有商品信息,每页五条,第一页:

  select * from product limit 0,5;

[图片上传失败...(image-d5fecb-1649149727008)]

7、分页查询所有商品信息,每页五条,第三页:

select * from product limit 10,5;

[图片上传失败...(image-6ee356-1649149727008)]

8、分页查询所有商品信息,每页五条,第五页:

 select * from product limit 20,5;

[图片上传失败...(image-f0417c-1649149727008)]

9、查询所有商品的平均零售价(salePrice):

  select avg(salePrice) from product;

[图片上传失败...(image-75370b-1649149727008)]

10、查询商品总的条数:

select count(id) from product;

[图片上传失败...(image-260fbf-1649149727008)]

11、查询分类编号为2的商品总数:

 select COUNT(id) from product where dir_id=2;

[图片上传失败...(image-b2690f-1649149727008)]

12、查询商品的最低零售价(salePrice),最高零售价(salePrice),以及所有商品零售价的总和:

 select min(salePrice),max(salePrice),sum(salePrice) from product;

[图片上传失败...(image-60b0a5-1649149727008)]

13、按照零售价(salePrice)升序排列,并设置每页显示5条数据:

select * from product order by salePrice limit 0,5;

[图片上传失败...(image-1b78d-1649149727008)]

14、查询id、货品名称(productName),以及货品所属分类名称(dirName):

建表语句:

CREATE TABLE productdir(
  id bigint(20) PRIMARY KEY AUTO_INCREMENT,
  dirName varchar(20),
  parent_id bigint(20)
);
INSERT INTO productdir VALUES (1, '鼠标', NULL);
INSERT INTO productdir VALUES (2, '无线鼠标', 1);
INSERT INTO productdir VALUES (3, '有线鼠标', 1);
INSERT INTO productdir VALUES (4, '游戏鼠标', 1);

答案:

select
    product.id,
    product.productName,
    productdir.dirName
from
    product,
    productdir
where
    product.dir_id = productdir.id;

[图片上传失败...(image-8a944b-1649149727008)]

15、查询商品零售价(salePrice)大于200的无线鼠标的所有信息:

select
    *
from
    product,
    productdir
where
    product.dir_id = productdir.id
    and salePrice > 200
    and dirName = '无线鼠标';

[图片上传失败...(image-759367-1649149727008)]

16、查询商品名称(productName)、每个货品对应的分类名称(dirName)以及对应的库存量(storeNum):

建表语句:

CREATE TABLE productstock(
   id bigint(20) PRIMARY KEY AUTO_INCREMENT,
   product_id bigint(20),
   storeNum bigint(20),
   lastIncomeDate datetime,
   lastOutcomeDate datetime,
   warningNum bigint(20)
);

INSERT INTO productstock VALUES (1, 1, 182, '2020-04-08 09:22:21', '2020-04-09 09:22:26', 20);
INSERT INTO productstock VALUES (2, 2, 27, '2020-04-10 09:22:46', '2020-04-11 09:22:40', 20);
INSERT INTO productstock VALUES (3, 3, 89, '2020-03-03 09:22:57', '2020-03-12 09:23:05', 20);
INSERT INTO productstock VALUES (4, 5, 19, '2020-04-09 09:23:30', '2020-04-17 09:23:25', 20);
INSERT INTO productstock VALUES (5, 6, 3, '2020-04-07 09:25:05', '2020-04-09 09:25:11', 5);
INSERT INTO productstock VALUES (6, 7, 2, '2020-04-08 09:25:28', '2020-04-10 09:25:24', 3);
INSERT INTO productstock VALUES (7, 8, 120, '2020-04-09 09:25:44', '2020-04-10 09:25:47', 20);
INSERT INTO productstock VALUES (8, 9, 58, '2020-04-07 09:26:03', '2020-04-09 09:25:59', 20);
INSERT INTO productstock VALUES (9, 11, 28, '2020-04-01 09:26:21', '2020-04-15 09:26:26', 20);
INSERT INTO productstock VALUES (10, 12, 8, '2020-04-02 09:26:38', '2020-04-14 09:26:35', 5);
INSERT INTO productstock VALUES (11, 13, 3, '2020-04-03 09:26:53', '2020-04-13 09:26:57', 5);
INSERT INTO productstock VALUES (12, 14, 6, '2020-04-04 09:27:11', '2020-04-12 09:27:07', 5);
INSERT INTO productstock VALUES (13, 15, 2, '2020-04-05 09:27:26', '2020-04-11 09:27:39', 5);
INSERT INTO productstock VALUES (14, 16, 3, '2020-04-06 09:28:04', '2020-04-10 09:28:00', 3);
INSERT INTO productstock VALUES (15, 17, 49, '2020-04-07 09:28:20', '2020-04-09 09:28:25', 20);
INSERT INTO productstock VALUES (16, 18, 14, '2020-04-07 09:28:49', '2020-04-14 09:28:37', 10);
INSERT INTO productstock VALUES (17, 20, 7, '2020-04-06 09:29:09', '2020-04-13 09:29:13', 5);

答案:

select
    product.productName,
    productdir.dirName,
    productstock.storeNum
from
    product,
    productdir,
    productstock
where
    product.dir_id = productdir.id
    and product.id = productstock.product_id;

[图片上传失败...(image-59fde5-1649149727008)]

17、如果库存商品销售完成,按照利润(salePrice-costPrice)从高到底查询货品名称(productName),零售价(salePrice),货品分类名称(dirName):

 select
    product.productName,
    product.salePrice,
    product.costPrice,
    productstock.storeNum,
    productdir.dirName,
(salePrice - costPrice) * storeNum profit 
from
    product,
    productdir,
    productstock
where
    product.dir_id = productdir.id
    and product.id = productstock.product_id
order by
    profit desc;

[图片上传失败...(image-67139e-1649149727008)]

注:因为下面的题目没给创建emp表的代码,而且操作也简单,所以底下的sql操作并没有得到验证,如发现问题请不吝指正

18、查询所有的员工信息:

 select * from emp;

19、查询每个员工的编号(dmpno)、姓名(ename)、职位(job):

 select dmpno,ename,job from emp;

20、查询所有部门信息:

建表语句:

CREATE TABLE dept  (
  DEPTNO bigint(20) PRIMARY KEY ,
  DNAME varchar(30),
  LOC varchar(30)   
)

INSERT INTO dept VALUES (10, 'ACCOUNTING', 'NEWTORK');
INSERT INTO dept VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO dept VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES (40, 'OPERATIONS', 'BOSTON');

答案:

 select * from dept;

[图片上传失败...(image-4d57c6-1649149727008)]

21、查询员工的部门编号(deptno)并去除重复:

 select distinct deptno from emp;

22、查询所有员工的姓名以及对应的年薪:

select ename,sal*12 from emp;

23、查询所有员工的姓名以及对应的年薪(使用别名):

 select ename,sal*12 as annual_salary from emp;

24、查询每月都有500元的餐补和200元的交通补贴并且年底多发一个月的工资的年薪:

 select ename,(sal+500+200)*12+sal from emp;

25、查询基本工资(sal)高于1500的员工所有信息:

 select * from emp where sal>1500;

26、查询名为SCOTT员工的岗位(job)是什么:

 select job from emp where ename='SCOTT';

27、查询年薪小于3万的员工的所有信息:

 select * from emp where sal*12 < 30000;

28、查询所有不是销售人员(salesman)的所有信息:

 select * from emp where job!='salesman';

29、查询工资在2000-3000之间的所有员工信息(使用区间查询):

 select * from emp where sal between 2000 and 3000;

30、查询工资不在2000-3000之间的所有员工信息(使用区间查询):

   select * from emp where sal not between 2000 and 3000;

31、查询工资为800、1600、3000的所有员工信息(使用IN):

 select * from emp where sal in(800,1600,3000);

32、查询工资不为800、1600、3000的所有员工信息(使用IN):

 select * from emp where sal not in(800,1600,3000);

33、查询所有员工名字(ename)以A字母开头的员工信息:

 select * from emp where ename like 'A%';

34、查询所有员工名字(ename)第二个字母是M的员工信息:

select * from emp where ename like '_M%';

35、查询所有员工名字(ename)中有A字母的员工信息:

 select * from emp where ename like '%A%';

36、查询员工名字(ename)中有E或者A的员工信息:

  select * from emp where ename like '%E%' or '%A%';

37、查询工资在1500-3000之间的所有员工信息:

 select * from emp where sal >=1500 and sal <=3000;

38、查询所有员工信息,按工资(sal)升序排列:

select * from emp order by sal asc;

39、查询所有员工信息,按年薪降序排列:

select * from emp order by sal*12 desc;

40、查询所有员工信息,按照部门(deptno)和年薪降序排列:

select * from emp order by deptno desc ,sal*12 desc;

41、把所有员工分页,每页5条信息,第一页SQL:

 select * from emp limit 0,5;

42、把所有员工分页,每页5条信息,第三页SQL:

 select * from emp limit 10,5;

43、查询所有员工每个月支付工资的平均工资和总工资:

 select avg(sal),sum(sal) from emp;

44、查询月薪在2000以上的员工人数:

 select count(dmpno) from emp where sal>2000;

45、查询员工最高工资:

 select max(sal) from emp;

46、查询员工最低工资:

 select min(sal) from emp;

47、查询员工最高工资和最低工资的差额:

 select max(sal)-min(sal) from emp;

48、统计30部门的总人数:

select count(dmpno) from emp where deptno = 30;

49、查询奖金(comm)为空的所有员工信息:

 select * from emp where comm is null;

50、统计没有奖金的员工人数:

  select count(dmpno) from emp where comm is null;

你可能感兴趣的:(100道MySQL练习题,帮你熟悉sql语法【后50道】)