50道MYSQL基础练习题

/1.ascii(str):返回指定字符串的第一个字符的ascii码 dual 虚表/
select ascii(‘Aello’) from dual
select ASCII(‘null’)
select ASCII(null)

/2.conv(n,from_base,to_base),将from_base进制中的数n转成to_base进制下的表示方式/
select CONV(‘90’,10,2) --90的十进制转二进制
select CONV(101011101,2,10) --二进制转十进制

/3.bin(n),oct(n),hex(n)
将数值n转成对应的二进制,八进制,十六进制
/
select bin(13),oct(100),hex(128);

/4.char(n…):返回多个ascii码组成的字符串/
select char(97,65,110)

/5.concat(str1,str2,…):将多个字符串
拼接成一个字符串
/
select CONCAT(‘hello’,‘hi’,‘enheng’,null)

#concat如果拼接NULL那么整个值就是NULl,需要校验
select concat(e.deptno,e.ename,IFNULL(e.comm,’’)),job from emp e where deptno=20

/7.length(str)/octet_length(str)/
一个中文占3字节,一个英文占1个字节
select LENGTH(‘hello’)
select LENGTH(‘我非常喜欢学习java’)

select octet_length(‘hello’)
select octet_length(‘我非常喜欢学习java’)

返回字符串长度
select character_length(‘hello’)
select character_length(‘我非常喜欢学习java’)

/8.locate(substr,str)/position(substr in str)/instr(str,substr)
它们三个意义是一样的,返回substr在str第一次出现的位置。如果没有,返回0;
/

select LOCATE(‘in’,‘Thinking in java’)
select position(‘in’ in ‘Thinking in java’)
select instr(‘Thinking in java’,‘in’)

/9.locate(substr,str,pos)
从str的下标pos开始向后查找,substr第一次出现的位置。找不到返回0
/
select LOCATE(‘in’,‘Thinking in java’,7)

/10.lpad(str,len,padstr)/rpad(str,len,padstr);
在字符串str的左边/右边添加padstr,添加后的总长度为len。
/
select e.first_name,lpad(e.first_name,10,’KaTeX parse error: Expected 'EOF', got '#' at position 23: … employees e #̲大于是添加 select e.…’) from employees e #少于是截取
select e.first_name,rpad(e.first_name,10,’KaTeX parse error: Expected 'EOF', got '#' at position 23: … employees e #̲大于是添加 select e.…’) from employees e #少于是截取

/11.left(str,len)/right(str,len)
返回字符串左端/右端的len个字符
/
select right(‘helloworld’,6)

select first_name,left(first_name,5) from employees

/12.substring(str,pos)/substring(str from pos)
截取str的子串,从pos下标开始截取到最后。
/
select substring(‘helloworld’, 3);
select substring(‘helloworld’ from 3);

/13.substring(str,pos,len)/
substring(str from pos for len)/
对str进行截取操作,从pos下标开始,截取len长度个。
/
select substring(‘helloworld’,5,3)
select substring(‘helloworld’ from 5 for 3)

/14.substring_index(str,delim,count)
截取str,截取到第count个分隔符delim
/
select SUBSTRING_INDEX(‘www.sohu.com’,’.’,2) --www.sohu
select SUBSTRING_INDEX(‘www.sohu.com’,’.’,1) --www
select SUBSTRING_INDEX(‘www.sohu.com’,’.’,-1) --com

/15.ltrim(str)/rtrim(str)
删除str的左端/右端的空格
/
select first_name,ltrim(first_name) from employees

trim去除左右两边空格

/*17.replace(str,from_str,to_str)
使用to_str替换掉str中的from_str /
select REPLACE(‘helloworld’,‘o’,’
’)

/18.insert(str,pos,len,newstr)
使用newstr替换str中的一部分,这部分从pos下标开始到len个替换成newstr
/
select insert(‘helloworld’,3,5,’#’) --he#rld

/19.lcase(str)/lower(str)/ucase(str)/upper(str)
将字符串中的英文字母全变成大/小写。 mysql中是不区分大小写的
/
select lower(‘HELLO WORLD’)
select lcase(‘HELLO WORLD’);
select upper(lower(‘HELLO WORLD’))

/*20.mod取余函数 同java中计算% */
select mod(10,4),10%4;
select mod(-10,4),-10%4;
select mod(10,-4),10%-4;
select mod(-10,-4),-10%-4;

/21.round(n,d) 保留小数点前几位/
select round(12365.6789,-2), – -2保留小数点左边2位 进位后12400 | 12335.6789 不进位 12300
round(12345.6789,0), – 保留整数
round(12345.6789,2); – 保留小数点后2位

pow(x,y)/power(x,y)
–作用:计算x的y次方
–练习 计算10的3次方
select pow(10,3);

sqrt(n)
– 作用:对n进行开平方。
–练习: 计算64的平方根。
select sqrt(64);

rand()
–作用:返回0~1.0的一个随机小数。
–练习: 获取一个随机数
select rand();

truncate一般和round比较,截断不进位
select truncate(12365.6789,-2), --12300
truncate(12345.6789,0), --12345 ***与round区别0不能省略
truncate(12345.6789,2); --12345.67

/22.dayofweek(date)
查看指定日期date是所在周的第几天,
返回的是数字 周日-1,周一-2,周六-7
/
select DAYOFWEEK(‘2018-12-9’)

/23.weekday(date)
返回指定date是所在周的周几。返回的也是数字
0-周一,1-周二,6-周日
/
select WEEKDAY(‘2018-12-9’)

/24.dayname(date)
返回指定日期是周几,注意:返回的是英文描述。
/
select DAYNAME(‘2018-12-12’)

/25.dayofmonth(date)/dayofyear(date)
返回指定日期是当月/当年的第几天
/
select DAYOFMONTH(‘2018-12-12’),DAYOFYEAR(‘2018-12-12’)

/26.monthname(date)
回指定日期所在月的英文描述。
/
select MONTHNAME(‘2018-10-12’)

/27.week(date,first)
返回指定日期是当年的第几周。first可以取值0或1.
– 0表示周日是一周的第一天,1表示周一是一周的第一天。
/
select WEEK(‘2018-02-02’,1) – 5
select WEEK(‘2018-02-02’,0) – 4

/28.获取当前系统时间的各个分量值/
select now() --获得当前系统时间
select year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())

/29.extract(part from date)截取指定日期的一部分,/
select EXTRACT(year FROM now())
select EXTRACT(MONTH FROM now())
select EXTRACT(minute FROM now())

/30.date_format(date,format)
–作用:将指定日期date,按照自定义格式,转成字符串显示。
–格式: 年: %y
月: %M或%m
日: %d
时: %H
分: %i
秒: %s
–练习: 将当前系统时间格式化 yyyy年mm月dd日 mm分ss秒HH时。
/
select now(),DATE_FORMAT(now(),’%y年%M月%d日 %H时%i分%s秒’) ymd;

/31.curdate()/curtime();
–作用:获取当前系统的日期/时间
/
select curdate(),curtime()

/32.获取当前系统时间/
select now(),sysdate(),current_timestamp();

/33.sec_to_time(n)
将指定数值n转成时间格式
/
select sec_to_time(50216);

/34.time_to_sec(time)
–作用:将指定时间转成秒的总数。
/
select time_to_sec(‘13:56:56’);

select time_to_sec(now())

/35.查询emp表中的所有信息/
select * from employees;

/36.查询emp表中所有员工的员工姓名,职位,以及工资/
select CONCAT(e.first_name,e.last_name),e.email from employees e;

/37.查询emp表中工资大于3000或者部门号为20部门的员工姓名以及工资和部门号/
select CONCAT(e.first_name,e.last_name) name,e.salary,e.department_id
from employees e
where e.salary > 3000 or e.department_id = 20 ;

/38.查询emp表中20部门的员工的月薪以及年薪/
select e.salary as “月 薪”,e.salary*12 ‘年 薪’ from employees e
where e.department_id = 20

/39.查询emp表中没有奖金的员工的员工姓名,以及工资以及奖金/
select CONCAT(e.first_name,e.last_name),e.salary,e.commission_pct from employees e
where e.commission_pct IS null

/40.查询emp表中工资在10000到20000之间的员工姓名,月薪,年薪,奖金/

select e.first_name 姓名,e.salary 月薪,e.salary *12 年薪,e.commission_pct 奖金
from employees e
where e.salary > 10000 and e.salary < 20000;

select ename 姓名,sal 月薪,sal*12 年薪,comm 奖金
from emp
where sal [NOT] BETWEEN 1000 AND 2000 --where e.salary >= 10000 and e.salary <= 20000;

/41.查询职位为’ST_CLERK’,‘SA_MAN’,'AC_MGR’的员工姓名,职位以及工资/
select e.first_name,e.job_id,e.salary from employees e
where e.job_id=‘ST_CLERK’ or e.job_id=‘SA_MAN’ or e.job_id=‘AC_MGR’;

select e.first_name,e.job_id,e.salary from employees e
where e.job_id IN (‘ST_CLERK’,‘SA_MAN’,‘AC_MGR’)

/42.查询10部门或20部门的员工信息/
select * from employees e
where e.department_id=10 or e.department_id=20;

select * from employees e
where e.department_id in (10,20)

/43.查询入职日期在’1999-12-03’之后入职的30部门的员工/
select * from employees e
where e.hiredate>‘1999-12-03’ and e.department_id=30;

/*44.查询工资大于王贺宇员工编号7499,张秋红员工编号7521 他们中最大的工资员工信息

#通过员工编号去查询工资
select e.sal from emp e where e.empno in (7499,7521)

#工资大于他们俩中工资最大的, 不允许直接写>all(1600,1250)
select e.ename.e.sal from emp where salary >all(1600,1250)

#子查询替换
select e.ename,e.sal from emp e where e.sal >all(
select e.sal from emp e where e.empno in (7499,7521))

/45.查询emp表中名字带有s的员工信息/
select * from employees e where e.first_name like ‘%s%’

/46.查询emp表中email中第三个字符为’e’的员工信息/
select * from employees e where e.email like ‘__e%’

/47.查询emp表中所有的数据,按照工资降序排列/
select * from employees order by salary Desc

/48.查询emp表中20部门员工的工资降序排列/
select * from employees
where department_id = 20
order by salary desc

/49.按照部门升序,同部门的按照工资降序/
select * from employees
order by department_id,salary desc

/创建一张表x,order,desc/
create table x(order int,desc varchar(20));
insert into x values(1,‘qaq’)
select order,desc from x

/50.查询emp表中有哪些职位/
select DISTINCT job_id from employees

猜你喜欢

转载自blog.csdn.net/weixin_40645193/article/details/107346023