当前位置:网站首页>SQL 实现 Excel 的10个常用功能,附面试原题
SQL 实现 Excel 的10个常用功能,附面试原题
2022-07-25 18:45:00 【Python数据挖掘】
SQL难吗?说实话,要写好,很难很难。但要通过SQL笔试这关,并不难。相信大伙都使用过Excel,用SQL实现excel 常用操作去学,感觉会比较具体。我自身也刚入数据岗不久,本文也是为自己巩固一下SQL。
SQL面试题、技术交流,文末找我获取
数据是网上找到的销售数据,命名为sale,长这样:

01. 关联公式:Vlookup
vlookup是excel几乎最常用的公式,一般用于两个表的关联查询等。所以我先创建一个新表:复制sale表并筛选出地区仅为广州的,命名为sale_guang。
create table sale_guang
SELECT * from sale where city="广州";
需求:根据订单明细号关联两表,并且sale_guang只有订单明细号与利润两列
SELECT * from sale a
inner JOIN
(SELECT ordernum,profit from sale_guang) b
on a.ordernum=b.ordernum
02. 对比两列差异
需求:对比sale的订单明细号与sale_guang订单明细号的差异;
SELECT * from sale a
WHERE a.ordernum not in
(SELECT b.ordernum from sale_guang b);
03. 去除重复值
需求:去除业务员编码的重复值
SELECT * FROM sale
where salesnum not in
(SELECT salesnum from sale
GROUP BY salesman
HAVING COUNT(salesnum)>1)
04. 缺失值处理
需求:用0填充缺失值或则删除有地区名称缺失值的行。
--用0填充:
update sale set city = 0 where city = NULL
--删除有缺失值的行:
delete from sale where city = NULL;
05. 多条件筛选
需求:想知道业务员张爱,在北京区域卖的商品订单金额大于等于6000的信息。
SELECT * from sale
where salesman = "张爱"
and city = "北京"
and orderaccount >=6000;
06. 模糊筛选数据
需求:筛选存货名称含有"三星"或则含有"索尼"的信息。
SELECT * from sale
where inventoryname like "%三星%"
or 存货名称 like "%索尼%";
07. 分类汇总
需求:北京区域各业务员的利润总额。
SELECT city,sum(`profit`)
from sale
WHERE city = "北京"
GROUP BY `city`;
08. 条件计算
需求:存货名称含“三星字眼”并且税费高于1000的订单有几个?这些订单的利润总和和平均利润是多少?
--有多少个?
SELECT COUNT(*) from sale
where inventoryname like "%三星%"
and `tax` > 1000 ;
--这些订单的利润总和和平均利润是多少?
SELECT `ordernum`,SUM(profit),AVG(`profit`)
from sale
where inventoryname like "%三星%"
and `tax` > 1000
GROUP BY `ordernum`;
09. 删除数据间的空格
需求:删除存货名称两边的空格。
SELECT trim(inventoryname) from sale;
10. 合并与排序列
需求:计算每个订单号的成本并从高到低排序(成本 = 不含税金额 - 利润)
SELECT city,ordernum,
(Nontaxamount - profit) as cost
from sale
order by cost DESC;
总结:结构化查询语言(Structured Query Language)简称SQL,果然和它名字一样,查询起来得心应手,但做想做数据处理方面,能明细感受到比Python和excel吃力(也可能是我还没学好orz)。
SQL笔试题原题
贴一些我在面试时遇到过的SQL笔试题吧:
某数据服务公司:

Student表

Score表
(1)查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student;
(2)查询Score表中成绩在60到80之间的所有记录。
select * from score between 60 and 80;
(3)查询95033班和95031班的平均分。
select class,avg(degree) from Score a
join student b
on a.sno = b.sno
GROUP BY CLASS;
总之是比较简单的SQL笔试题了,当时很快就写完了。实际上这不是原题,不过我有印象就是考察这几个知识点,并且蛮简单的。
某手游公司的SQL笔试题(原题)

(1)建立表Student的语句写下来,表Student是由学号Sno,姓名Sname,性别Ssex,年龄Sage,所在系Sdept五个属性组成,其中学号属性不能为空,并且其值是唯一的。
create table Student_new
(sno varchar(20) PRIMARY KEY,
sname varchar(10),ssex char(2),
sage int,sdept varchar(25));
(2)在student 表中查询Sdept是“计算机”的学生所有信息并按SNO列排序。
select * from student
where sdept = "计算机"
order by sno ;
(3)在以上三个表中查询Ccredit为5并且Grade大于60的学生的学号、姓名和性别。
select a.sno,a.sname,a.ssex from student a
join (Course b ,SC c)
on a.sno=c.sno and b.cno =c.cno
where Ccredit = 5 and Grade > 60;
部分互联网金融公司SQL笔试题(原题)
完整版,找我获取

(1)表A和表B的交集:
SELECT a.cus_id from `表a` as a
INNER JOIN `表b` as b
on a.cus_id=b.cus_id;
(2)表A和表B的并集:
SELECT * from `表a`
UNION
SELECT * from `表b`;
(3)表A和表B的对称差:
SELECT * from `表a`
where cus_id not in (SELECT * from `表b`)
UNION
SELECT * from `表b`
where cus_id not in (SELECT * from `表a`);
(4)表A中存在但表B中不存在:
SELECT * from `表a`
WHERE cus_id not in (SELECT cus_id from `表b`);
联系方式
目前开通了技术交流群,群友已超过3000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友,资料获取也可以加入
方式1、添加微信号:dkl88191,备注:来自CSDN
方式2、微信搜索公众号:Python学习与数据挖掘,后台回复:加群
边栏推荐
- Yarn 安装与使用教程[通俗易懂]
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- Interpretation of "cross chain interconnection smart contract"
- 拍卖行作VC,第一次出手就投了个Web3
- Process communication (Systemv communication mode: shared memory, message queue, semaphore)
- CS615 System Administration, Week 02, Segment 5 - Partitions
- 大厂云业务调整,新一轮战争转向
- ThreadLocal夺命11连问
- MySQL子查询篇(精选20道子查询练习题)
- F5: Six capabilities required for enterprise digital transformation
猜你喜欢

市值300亿,欧洲十年来最大IPO再冲纽交所

Ceres curve fitting

How high are the young people in this class for "ugly things"?

年轻时代,噢,年轻时代

Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%

Project: serial port receiving RAM storage TFT display (complete design)

拍卖行作VC,第一次出手就投了个Web3

Optimistic lock resolution

What is hpapaas platform?

华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条
随机推荐
[Huawei machine test real question] string matching
如何创建一个有效的帮助文档?
Share six practical applet plug-ins
11.1-cm24 nearest common ancestor
Regex 正则表达式
[applet development] page navigation details
F5: Six capabilities required for enterprise digital transformation
浏览器内核有几种,浏览器版本过低怎么升级
Esp32 S3 vscode+idf setup
Chapter 5 Basic Scripting: Shell Variables
What is hpapaas platform?
With a market value of 30billion yuan, the largest IPO in Europe in the past decade was re launched on the New York Stock Exchange
APP测试点(思维导图)
怎么禁止使用360浏览器(怎么才能把自带的浏览器停用)
JVM基础和问题分析入门笔记
Jz32 print binary tree from top to bottom
Ping 命令详解[通俗易懂]
Project: serial port receiving RAM storage TFT display (complete design)
What is the difference between GB and gib disk space units?
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%