【SQL】SQL数据库基础语句学习(一)

前言

针对平安金融机器学习方向的实习生面试要求复习了数据库数据库的一基础知识,面试问到的是表的内连接、外连接和全连接的问题,还有一道智力题目,如何将两个亿万级别的两个表进行连接。前面的问题回答出,后面的问题天马星空一番。面试官很和蔼,最后一道可以将两个表按照特征分割成小表,用小表进行连接。


SQL连接的问题

连接:左连接&右连接
左连接和右链接的区别?
左连接只影响右表,右链接只影响左表。

  • 左连接 (left join)
    select * from table1 left join tbale2 on table1.id=table2.id
    这条sql语句返回结果 table1表中的数据全部返回 table2表中的数据只返回满足where条件的

  • 右链接 (right join)
    select * from table1 right join table2 on table1.id=table2.id
    这条sql语句返回结果 table2表中的数据全部返回 table1表中的数据只返回满足where条件的

  • 全连接 (inner join)
    select * from table1 inner join table2 on table1.id = table2.id
    这条sql语句返回结果 显示满足条件的数据 并不以谁为主表
    看到下面这张图,讲的非常形象:
    这里写图片描述
    内连接 交
    外连接 分为左连接和右连接
    外连接 并

基础语句练习

感谢男朋友的三个晚上的远程辅导~
我感觉难点在于子查询的应用以及group by 和having的应用
查找重复的信息(使用where和count()函数)

--2018/09/06——2018/09/09

--对表table进行操作
--创建:create   --修改: alert    --删除:drop   --增加:add
--create table 表名(数据段1,数据段2,……)
--alert table 表名
--drop table表名 删除数据结构
--add 

--对表中的数据进行操作
--增加 insert into 表名(字段1,字段2,……) values(value1,value2,……)、
--修改(更新) update
--删除 delete
--查找 select


--增加一条数据
--insert into book1(编号,书名,定价) values(3,'白与黑',38.9)

----------------查询语句 select [] from 表名称----------------------------
--查询所有数据
select * from book1 

--查询排序 select [] from 表名称 order by 一个字段 [asc/desc]
--根据顺序查询数据(asc升序,desc降序)
select * from book1 order by 编号 desc

--按照编号升序,查询前5条数据
select top 5 * from book1 order by 编号 asc

--按照编号查询后几条数据
select top 5 * from book1 order by 编号 desc

--查询特定几条数据(子查询)
select * from book1 where 编号 in (6,7,8)
-- in (某列字段,不是表数据,在最里面的select语句写的是select 编号,而不是select *)
select top 3 * from book1 where 编号 in 
(
 select top 5 编号 from book1 order by 编号 desc
) order by 编号 asc

--查询定价最高的,排序
select top 1 * from book1 where 书名='悲惨世界' order by 定价 desc

select * from book1 where 书名='悲惨世界' and 出版社='上海出版社'

--查询计数
select COUNT(*) as 行数 from book1 
select COUNT(编号) as 编号个数 from book1 
--分组查询计数
select 出版社,COUNT(*) from book1 where 书名='悲惨世界' group by 出版社 


--查询书名,select 字段 (as) 新的字段名称(别名)
select 书名 as 书大名 from book1
select 书名  书大名 from book1
--起别名
select 编号,书名 as 书大名,定价 as 价格 from book1

--多表联合查询
select * from book1 
select * from author1

--左连接
select * from book1 a
left join author1 b on a.作者ID=b.id
where b.name='张三'

select a.编号,a.书名,a.出版社,a.定价,b.name  as 作者 from book1 a
left join author1 b on a.作者ID=b.id
where b.name='张三'

--左连接,右连接的区别
select * from book1 a
left join author1 b on a.作者ID=b.id
where b.name='张三'

--全连接
select * from book1 a
inner join author1 b on a.作者ID=b.id
where b.name='张三'


----------------------------------------更新(修改)数据 
--uppdate 表名称 set [] where []
update TestDB.dbo.book1 set 书名='小王子4' where 编号='2'

-------------子查询
--删除重复的姓名信息并保留编号最小的那一条
delete from emp where ename in (select ename from emp group by ename  having COUNT(ename)>1) and deptno not in ( select MIN(deptno) from emp where ename in(select ename from emp group by ename  having COUNT(ename)>1) )


温故知新:
排序
- select [] from 表名称 order by 字段

多表查询
select * from book1
select * from author1

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值