2023年6月27日发(作者:)
【SQL】SQL数据库基础语句学习(⼀)前⾔针对平安⾦融机器学习⽅向的实习⽣⾯试要求复习了数据库数据库的⼀基础知识,⾯试问到的是表的内连接、外连接和全连接的问题,还有⼀道智⼒题⽬,如何将两个亿万级别的两个表进⾏连接。前⾯的问题回答出,后⾯的问题天马星空⼀番。⾯试官很和蔼,最后⼀道可以将两个表按照特征分割成⼩表,⽤⼩表进⾏连接。SQL连接的问题连接:左连接&右连接
左连接和右链接的区别?
左连接只影响右表,右链接只影响左表。左连接 (left join)
select * from table1 left join tbale2 on =
这条sql语句返回结果 table1表中的数据全部返回 table2表中的数据只返回满⾜where条件的右链接 (right join)
select * from table1 right join table2 on =
这条sql语句返回结果 table2表中的数据全部返回 table1表中的数据只返回满⾜where条件的全连接 (inner join)
select * from table1 inner join table2 on =
这条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 定价 descselect * 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 book1select 书名 书⼤名 from book1--起别名select 编号,书名 as 书⼤名,定价 as 价格 from book1--多表联合查询select * from book1
select * from author1--左连接select * from book1 aleft join author1 b on a.作者ID=e ='张三'where ='张三'select a.编号,a.书名,a.出版社,a.定价, as 作者 from book1 aleft join author1 b on a.作者ID=e ='张三'--左连接,右连接的区别select * from book1 aleft join author1 b on a.作者ID=e ='张三'--全连接select * from book1 ainner join author1 b on a.作者ID=e ='张三'----------------------------------------更新(修改)数据
--uppdate 表名称 set [] where []update 1 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温故知新:
排序
- select [] from 表名称 order by 字段多表查询
select * from book1
select * from author1
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687817485a47737.html
评论列表(0条)