sql in的用法 sql in的用法的意思ig decimal(sql in的用法的意思)

1.编写查询,查找表中的行总数。答:selectcount(*)fromtblsample;2.编写查询,消除表结果中的重复记录。答:Selectdistinct*fromtblSample;3.编写查询,获取t_employee表中designation字段前3个字符。答:Selectsubstr(designation,1,3)fromt_employee;4.

1.编写查询,查找表中的行总数。

答:select count(*) from tblsample;

2.编写查询,消除表结果中的重复记录。

答:Select distinct * from tblSample;

3.编写查询,获取t_employee表中designation字段前3个字符。

答:Select substr(designation,1,3) from t_employee;

4.查询t_employee表,合并输出Designation和Department两个字段的内容。

答:Select Designation + ‘ ‘ + Department from t_employee;

5.如果使用union和union all合并4条SQL子查询,union会有多少次被用来去除重复行?

答:1次。

6.IN和BETWEEN之间的区别是什么,在WHERE子句中如何使用?

答:BETWEEN子句是用来获取一个范围值,而IN子句是从指定值列表中获取对应数据。

7.解释“LIKE”关键字在WHERE子句中如何使用?sql有哪些通配符?

答:LIKE是用于部分字符串的匹配。SQL有两个通配符,“%”(匹配字符串的任何字符)和“_”(匹配任意单个字符)。

8.怎样使用“LIKE”语句?

答:在局部搜索中使用。例如,你需要找到lastname包含“gat”字母的所有员工,那么你可以使用下面的查询、匹配搜索条件:Select empid, firstname, lastname from t_employee where lastname like ‘%gat%’;这可能会搜索到lastname包含字符“gat”的所有雇员,像Gates、Gatsby、Gatsburg、Sogatsky…"%"用于表示名称中剩余的所有字符。这个查询获取在字符串中包含“gat”的所有记录。

9.解释 GROUP BY 和 HAVING 子句的用法。

答:利用group by子句分组数据,当select语句中使用到组函数和字段一起连用时会用到group by,否则会出现错误。Group by 把select查询的结果集分成几个小组,这个group by子句可以跟在where后面且在having前面。Group by子句也会触发排序操作,会按分组字段排序。 b

10.在t_employee表中,department字段可为空。编写查询,获取尚未分配部门的员工。

答:Select empid, firstname, lastname from t_employee where department is null;

获取更详细的私信博主(学习)

一、 SQL分类:

DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)

DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)

DCL数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

二、基本语法

1、创建数据库

create database database-name

2、删除数据库

drop database dbname

3、创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表:

A:create table tab_new like tab_old (使用旧表创建新表)(在orcale中不能用)

B:create table tab_new as select col1,col2… from tab_old definition only

4、删除新表

drop table tabname

5、增加一个列

Alter table tabname add column col type

注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

6、添加主键

Alter table tabname add primary key(col)

7、 创建索引

create [unique] index idxname on tabname(col….)

删除索引

drop index idxname

注:索引是不可更改的,想更改必须删除重新建。

8、 创建视图

create view viewname as select statement

删除视图:

drop view viewname

9、 几个简单的基本的SQL语句

选择:select * from table1 where 范围

插入:insert into table1(field1,field2) values(value1,value2)

Insert into table1 values(‘001’,’sll’)

删除:delete from table1 where 范围

更新:update table1 set field1=value1 where 范围

查找:select * from table1 where field1 like ’%value1%’

表示模糊查询(匹配字符串)

排序:select * from table1 order by field1,field2 [desc]

总数:select count(*) as totalcount from table1

求和:select sum(field1) as sumvalue from table1

平均:select avg(field1) as avgvalue from table1

最大:select max(field1) as maxvalue from table1

最小:select min(field1) as minvalue from table1

10、 使用外连接

A、left outer join:

左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。

sql: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a =b.c

B:right outer join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。

C:full outer join: 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

D:等值连接 无条件连接,取两个表的笛卡尔积

11、 in 的使用方法

select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

13、两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

三.MySQL索引技巧

举例,业务场景,用户表,表结构为:

t_user(

uid primary key,

login_name unique,

passwd,

login_time,

age,

);

聚集索引(clustered index):聚集索引决定数据在磁盘上的物理排序,一个表只能有一个聚集索引,一般用primary key来约束。

举例:t_user场景中,uid上的索引。

非聚集索引(non-clustered index):它并不决定数据在磁盘上的物理排序,索引上只包含被建立索引的数据,以及一个行定位符row-locator,这个行定位符,可以理解为一个聚集索引物理排序的指针,通过这个指针,可以找到行数据。

举例,查找年轻MM的业务需求:

select uid from t_user where age > 18 and age < 26;

age上建立的索引,就是非聚集索引。

联合索引:多个字段上建立的索引,能够加速复核查询条件的检索

举例,登录业务需求:

select uid, login_time from t_user where

login_name=? and passwd=?

可以建立(login_name, passwd)的联合索引。

联合索引能够满足最左侧查询需求,例如(a, b, c)三列的联合索引,能够加速a | (a, b) | (a, b, c) 三组查询需求。

这也就是为何不建立(passwd, login_name)这样联合索引的原因,业务上几乎没有passwd的单条件查询需求,而有很多login_name的单条件查询需求。

提问:

select uid, login_time from t_user where

passwd=? and login_name=?

能否命中(login_name, passwd)这个联合索引?

回答:可以,最左侧查询需求,并不是指SQL语句的写法必须满足索引的顺序(这是很多朋友的误解)

索引覆盖:被查询的列,数据能从索引中取得,而不用通过行定位符row-locator再到row上获取,即“被查询列要被所建的索引覆盖”,这能够加速查询速度。

举例,登录业务需求:

select uid, login_time from t_user where

login_name=? and passwd=?

可以建立(login_name, passwd, login_time)的联合索引,由于login_time已经建立在索引中了,被查询的uid和login_time就不用去row上获取数据了,从而加速查询。

末了多说一句,登录这个业务场景,login_name具备唯一性,建这个单列索引就好。

四.MySQL的or/in/union与索引优化

假设订单业务表结构为:

order(oid, date, uid, status, money, time, …)

其中:

oid,订单ID,主键

date,下单日期,有普通索引,管理后台经常按照date查询

uid,用户ID,有普通索引,用户查询自己订单

status,订单状态,有普通索引,管理后台经常按照status查询

money/time,订单金额/时间,被查询字段,无索引

假设订单有三种状态:0已下单,1已支付,2已完成

业务需求,查询未完成的订单,哪个SQL更快呢?

select * from order where status!=2

select * from order where status=0 or status=1

select * from order where status IN (0,1)

select * from order where status=0

union all

select * from order where status=1

结论:方案1最慢,方案2,3,4都能命中索引

但是…

一:union all 肯定是能够命中索引的

select * from order where status=0

union all

select * from order where status=1

说明:

直接告诉MySQL怎么做,MySQL耗费的CPU最少

程序员并不经常这么写SQL(union all)

二:简单的in能够命中索引

select * from order where status in (0,1)

说明:

让MySQL思考,查询优化耗费的cpu比union all多,但可以忽略不计

程序员最常这么写SQL(in),这个例子,最建议这么写

三:对于or,新版的MySQL能够命中索引

select * from order where status=0 or status=1

说明:

让MySQL思考,查询优化耗费的cpu比in多,别把负担交给MySQL

不建议程序员频繁用or,不是所有的or都命中索引

对于老版本的MySQL,建议查询分析下

四、对于!=,负向查询肯定不能命中索引

select * from order where status!=2

说明:

全表扫描,效率最低,所有方案中最慢

禁止使用负向查询

五、其他方案

select * from order where status < 2

这个具体的例子中,确实快,但是:

这个例子只举了3个状态,实际业务不止这3个状态,并且状态的“值”正好满足偏序关系,万一是查其他状态呢,SQL不宜依赖于枚举的值,方案不通用

这个SQL可读性差,可理解性差,可维护性差,强烈不推荐

sql in的用法 sql in的用法的意思ig decimal(sql in的用法的意思)

说明:本文限于篇幅,故而只展示部分的面试内容,完整的Java面试学习文档小编已经帮你整理好了,有需要的朋友点赞+关注私信我777免费领取Java、大厂面试学习资料哦!

sql in的用法 sql in的用法的意思ig decimal(sql in的用法的意思)

原创文章,作者:admin,如若转载,请注明出处:https://www.cshaobangshou.cn/126925.html