1邀层、查詢id返敬、貨品名稱、分類編號(hào)寥院、零售價(jià)劲赠,并且零售價(jià)(salePrice)按降序排列:
select id,productName,dir_id,salePrice from product order by salePrice desc;
[圖片上傳失敗...(image-1549f3-1649149727008)]
2、查詢id秸谢、貨品名稱凛澎、分類編號(hào)、零售價(jià)估蹄,并且零售價(jià)按升序排列:
select id,productName,dir_id,salePrice from product order by salePrice asc;
[圖片上傳失敗...(image-176a31-1649149727008)]
3塑煎、查詢id、貨品名稱臭蚁、分類編號(hào)最铁、零售價(jià)讯赏,先按分類編號(hào)升序排列,再零售價(jià)按降序排列:
select id,productName,dir_id,salePrice from product order by dir_Id asc,salePrice desc;
[圖片上傳失敗...(image-4066d8-1649149727008)]
4冷尉、查詢M系列的id漱挎、貨品名稱、并按照批發(fā)價(jià)(salePrice*cutoff)升序排列雀哨,批發(fā)價(jià)使用別名:
select
id,
productName,
salePrice * cutoff as trade_price
from
product
where
productName like '%M%'
order by
trade_price asc;
[圖片上傳失敗...(image-605554-1649149727008)]
5磕谅、查詢id、貨品名稱雾棺、分類編號(hào)膊夹,按分類編號(hào)為2的商品按照批發(fā)價(jià)(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、分頁(yè)查詢所有商品信息捌浩,每頁(yè)五條放刨,第一頁(yè):
select * from product limit 0,5;
[圖片上傳失敗...(image-d5fecb-1649149727008)]
7、分頁(yè)查詢所有商品信息嘉栓,每頁(yè)五條宏榕,第三頁(yè):
select * from product limit 10,5;
[圖片上傳失敗...(image-6ee356-1649149727008)]
8、分頁(yè)查詢所有商品信息侵佃,每頁(yè)五條麻昼,第五頁(yè):
select * from product limit 20,5;
[圖片上傳失敗...(image-f0417c-1649149727008)]
9、查詢所有商品的平均零售價(jià)(salePrice):
select avg(salePrice) from product;
[圖片上傳失敗...(image-75370b-1649149727008)]
10馋辈、查詢商品總的條數(shù):
select count(id) from product;
[圖片上傳失敗...(image-260fbf-1649149727008)]
11抚芦、查詢分類編號(hào)為2的商品總數(shù):
select COUNT(id) from product where dir_id=2;
[圖片上傳失敗...(image-b2690f-1649149727008)]
12、查詢商品的最低零售價(jià)(salePrice)迈螟,最高零售價(jià)(salePrice)叉抡,以及所有商品零售價(jià)的總和:
select min(salePrice),max(salePrice),sum(salePrice) from product;
[圖片上傳失敗...(image-60b0a5-1649149727008)]
13、按照零售價(jià)(salePrice)升序排列答毫,并設(shè)置每頁(yè)顯示5條數(shù)據(jù):
select * from product order by salePrice limit 0,5;
[圖片上傳失敗...(image-1b78d-1649149727008)]
14褥民、查詢id、貨品名稱(productName)洗搂,以及貨品所屬分類名稱(dirName):
建表語(yǔ)句:
CREATE TABLE productdir(
id bigint(20) PRIMARY KEY AUTO_INCREMENT,
dirName varchar(20),
parent_id bigint(20)
);
INSERT INTO productdir VALUES (1, '鼠標(biāo)', NULL);
INSERT INTO productdir VALUES (2, '無(wú)線鼠標(biāo)', 1);
INSERT INTO productdir VALUES (3, '有線鼠標(biāo)', 1);
INSERT INTO productdir VALUES (4, '游戲鼠標(biāo)', 1);
答案:
select
product.id,
product.productName,
productdir.dirName
from
product,
productdir
where
product.dir_id = productdir.id;
[圖片上傳失敗...(image-8a944b-1649149727008)]
15消返、查詢商品零售價(jià)(salePrice)大于200的無(wú)線鼠標(biāo)的所有信息:
select
*
from
product,
productdir
where
product.dir_id = productdir.id
and salePrice > 200
and dirName = '無(wú)線鼠標(biāo)';
[圖片上傳失敗...(image-759367-1649149727008)]
16、查詢商品名稱(productName)耘拇、每個(gè)貨品對(duì)應(yīng)的分類名稱(dirName)以及對(duì)應(yīng)的庫(kù)存量(storeNum):
建表語(yǔ)句:
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撵颊、如果庫(kù)存商品銷售完成,按照利潤(rùn)(salePrice-costPrice)從高到底查詢貨品名稱(productName)惫叛,零售價(jià)(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)]
注:因?yàn)橄旅娴念}目沒給創(chuàng)建emp表的代碼,而且操作也簡(jiǎn)單嘉涌,所以底下的sql操作并沒有得到驗(yàn)證妻熊,如發(fā)現(xiàn)問題請(qǐng)不吝指正??
18夸浅、查詢所有的員工信息:
select * from emp;
19、查詢每個(gè)員工的編號(hào)(dmpno)固耘、姓名(ename)题篷、職位(job):
select dmpno,ename,job from emp;
20词身、查詢所有部門信息:
建表語(yǔ)句:
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厅目、查詢員工的部門編號(hào)(deptno)并去除重復(fù):
select distinct deptno from emp;
22、查詢所有員工的姓名以及對(duì)應(yīng)的年薪:
select ename,sal*12 from emp;
23法严、查詢所有員工的姓名以及對(duì)應(yīng)的年薪(使用別名):
select ename,sal*12 as annual_salary from emp;
24损敷、查詢每月都有500元的餐補(bǔ)和200元的交通補(bǔ)貼并且年底多發(fā)一個(gè)月的工資的年薪:
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萬(wàn)的員工的所有信息:
select * from emp where sal*12 < 30000;
28、查詢所有不是銷售人員(salesman)的所有信息:
select * from emp where job!='salesman';
29溯街、查詢工資在2000-3000之間的所有員工信息(使用區(qū)間查詢):
select * from emp where sal between 2000 and 3000;
30诱桂、查詢工資不在2000-3000之間的所有員工信息(使用區(qū)間查詢):
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)第二個(gè)字母是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犬缨、把所有員工分頁(yè),每頁(yè)5條信息棉浸,第一頁(yè)SQL:
select * from emp limit 0,5;
42怀薛、把所有員工分頁(yè),每頁(yè)5條信息迷郑,第三頁(yè)SQL:
select * from emp limit 10,5;
43枝恋、查詢所有員工每個(gè)月支付工資的平均工資和總工資:
select avg(sal),sum(sal) from emp;
44创倔、查詢?cè)滦皆?000以上的員工人數(shù):
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、統(tǒng)計(jì)30部門的總?cè)藬?shù):
select count(dmpno) from emp where deptno = 30;
49十电、查詢獎(jiǎng)金(comm)為空的所有員工信息:
select * from emp where comm is null;
50知押、統(tǒng)計(jì)沒有獎(jiǎng)金的員工人數(shù):
select count(dmpno) from emp where comm is null;