这个是我学习的所有sql语句的写法

create table reder(
card_id int(18) primary key not null auto_increment,
name varchar(20) not null,
sex enum(‘男’,‘女’,‘保密’) not null,
age int(30) not null,
tel int(30) not null,
balance decimal(7,3) not null
)
alter table reder auto_increment=15
alter table reder add people set(‘小丽’,‘小王’,‘笑’) not null
alter table reder modify people set(‘小丽’,‘小王’,‘笑’) after tel
alter table reder modify age int(18) not null
alter table reder modify age int(18)
alter table reder alter column age set default’18’
alter table reder alter column age drop default

create table bookxx(
book_id int(10) not null primary key auto_increment,
book_category_id int(10) not null,
book_name varchar(10) not null,
author varchar(10) not null,
price decimal(7,3) not null,
press varchar(20) not null default’机械出版社’,
pubdate timestamp not null,
store int(10) not null
)
alter table bookxx add constraint bookxx_wy unique(author)
alter table bookxx drop key bookxx_wy
alter table bookxx drop store
create table booklb(
category_id int(10) not null primary key auto_increment,
category varchar(10) not null,
parent_id int(10) not null
)
alter table booklb add constraint booklb_wj foreign key booklb(category) references bookxx(author)
alter table booklb drop key booklb_wj
create table borrow(
book_id int(10) not null primary key auto_increment,
card_id int(18) not null,
borrow_date timestamp not null,
return_date timestamp not null,
status enum(‘是’,‘否’)
)
insert into borrow(book_id,card_id,borrow_date) values(9,93,sysdate(‘1991-03-05 23:07:23’)) 插入时间不会

update borrow set card_id=card_id-1 where booklb=’’
delete from borrow where book_id=8
truncate table borrow where
selectfrom booklb
select
from emp
select ename 员工姓名,e.deptno 部门编号,d.dname 部门名称
from emp e
join dept d on e.deptno=d.deptno

select ename,d.loc,sal
from emp e
join dept d on e.deptno=d.deptno where d.loc=‘CHICAGO’ and sal is not null

SELECT ENAME,LOC
from emp e
join dept d on e.deptno=d.deptno
where e.ename like’%A%’

select*from salgrade
SELECT e.empno,ename,sal,grade,loc
from emp e
join dept d on e.deptno=d.deptno
join salgrade s on e.sal BETWEEN losal and hisal
ORDER BY sal desc
SELECT e.ename,e.empno,e1.ename,e1.mgr
from emp e
join emp e1 on e.empno=e1.mgr
join dept d on e.deptno=d.deptno
where d.loc in(‘CHICAGO’,‘NEW YORK’)

select e.ename,e1.ename
from emp e
left join emp e1 on e.empno=e1.mgr

select count(ename),max(sal),min(sal)
from emp
join dept on emp.deptno=dept.deptno
where dept.loc=‘CHICAGO’
select count( distinct job) from emp
select*from emp
select deptno,job,count(job),max(sal),min(sal),sum(sal),avg(sal) from emp
select deptno,job,count(job),max(sal),min(sal),sum(sal),avg(sal) from emp GROUP BY deptno

select count(e1.ename),e1.deptno,e1.ename
from emp e
join emp e1 on e.empno=e1.mgr

select count(ename),deptno
from emp GROUP BY deptno>2

where
1.查询部门平均工资在2500元以上的部门名称及平均工资

select d.dname,avg(e.sal),e.deptno
from emp e,dept d where e.deptno=d.deptno
GROUP BY e.deptno having avg(sal)>2500
2.查询员工岗位中不是以“SA”开头并且平均工资在2500元以上的岗位及平均工资,并按平均工资降序排序。
SELECT d.dname,avg(e.sal),e.deptno
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno having d.dname not like’SA%’ AND avg(e.sal)>2500
ORDER BY avg(e.sal) desc
3.查询部门人数在2人以上的部门名称、最低工资、最高工资,并对求得的工资进行四舍五入到整数位。
SELECT d.dname,min(sal),max(sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno having count(e.empno)>2
4.查询岗位不为SALESMAN,工资和大于等于2500的岗位及每种岗位的工资和。

select d.dname,sum(sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno
having d.dname<>‘salesman’ and sum(sal)>=2500

5.显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于3000的,按最低工资由高到低排序。 不会
select e.ename, e1.mgr,e1.ename from emp e
LEFT JOIN emp e1 on(e.empno=e1.mgr)
where e.sal>3000 ORDER BY e.sal desc

(select e1.mgr,e1.ename
from emp e,emp e1
where e.empno=e1.mgr) s

6.查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
select empno,ename,sal from emp where
sal>(select sal from emp where empno=7782) and
job=(select job from emp where empno=7369)
7.查询工资最高的员工姓名和工资。
select max(sal),ename from emp
8.查询部门最低工资高于20号部门最低工资的部门的编号、名称及部门最低工资。 不会

SELECT e.deptno,e.ename,d.dname,min(e.sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno HAVING min(e.sal)>(select min(sal)from emp where deptno=20)

9.查询员工工资为其部门最低工资的员工的编号和姓名及工资。 不会
select empno,ename,sal
from emp
where sal=ANY
(SELECT min(sal)
from emp)
GROUP BY deptno

10.显示经理是KING的员工姓名,工资 不会

select e.ename,e.sal
from emp e LEFT JOIN emp e1 on(e.empno=e1.mgr)
where e1.ename=‘KING’

11.显示比员工SMITH参加工作时间晚的员工姓名,工资,参加工作时间。
select ename,sal,hiredate from emp where
hiredate>(select hiredate from emp where ename=‘SMITH’)

12.使用子查询的方式查询哪些职员在NEW YORK工作。

select e.ename,e.deptno
from emp e,dept d
where e.deptno=d.deptno and d.loc=‘NEW YORK’

select e.ename,d.deptno from emp e,dept d
where e.deptno=d.deptno and d.loc=(select loc from dept where loc=‘NEW YORK’)

13.写一个查询显示和员工SMITH工作在同一个部门的员工姓名,雇用日期,查询结果中排除SMITH。
select e.ename,e.hiredate
from emp e,dept d where e.deptno=d.deptno and d.dname=(select d.dname from emp e,dept d
where e.deptno=d.deptno and e.ename=‘SMITH’)and e.ename<>‘SMITH’

select d.dname
from emp e,dept d
where e.deptno=d.deptno and e.ename=‘SMITH’

14.写一个查询显示其工资比全体职员平均工资高的员工编号、姓名。
select empno,ename
from emp where sal>(select avg(sal)from emp where IFNULL(sal,0))
select avg(sal)from emp where IFNULL(sal,0)

  1. 显示部门名称和人数

select count(e.ename),d.dname
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno

  1. 显示每个部门的最高工资的员工
    select ename,sal from emp where sal in
    (select max(sal) from emp
    GROUP BY deptno)

  2. 显示出和员工号7369部门相同的员工姓名,工资

select ename,sal from emp where deptno=
(select deptno from emp where empno=‘7369’)

  1. 显示出和姓名中包含“W”的员工相同部门的员工姓名
    select ename,sal from emp where deptno=
    (select deptno from emp where ename like’%W%’)

                                                                   CREATE TABLE person(
    

id INT PRIMARY KEY NOT NULL auto_increment,
driver_id INT UNIQUE NOT NULL,
NAME VARCHAR(10)NOT NULL,
sex enum(‘男’,‘女’) NOT NULL,
hobby_id INT NOT NULL,
FOREIGN KEY person(hobby_id) REFERENCES hobbies(hobby_id)
)
CREATE TABLE hobbies(
hobby_id INT PRIMARY KEY NOT NULL auto_increment,
hobby VARCHAR(10) NOT NULL
)
CREATE TABLE student1(
学生学号 char(10) primary key not null,
学生姓名 varchar(10) not null,
学生性别 enum(‘男’,‘女’)not null,
学生生日 date not null,
学生班级 int not NULL,
FOREIGN KEY student(学生姓名) REFERENCES class(班级名称)
)
create table class1(
班级编号 int(10)unique PRIMARY key not null AUTO_INCREMENT,
班级名称 VARCHAR(10) unique not NULL,
系 VARCHAR(10) not null,
foreign key class(班级名称) REFERENCES department(系名称)
)

create table department1(
系编码 int primary key,
系名称 VARCHAR(10) UNIQUE not null,
系主任 VARCHAR(10) not NULL,
主任地址 enum(‘南湖校区’,‘浑南校区’)not null
)
create table depatrment2 like department1
create table department3 as select from 系编码,系名称 depatment1,WHERE 1=0
delete from emp where mgr>200
delete from emp where hiredate BETWEEN ‘1950-1-1’ AND ‘1970-1-1’
delete from emp where sal>SELECT avg(sal) from emp
SELECT avg(sal) from emp

create table animal(
animal_id int(10) primary key not null auto_increment COMMENT’动物编号’,
animal_type varchar(20) not null COMMENT’动物类型’,
age int(10) not null DEFAULT’18’,
sex enum(‘雄性’,‘雌性’,‘保密’),
weight float(8,2) not null,
type_id int(10) not NULL COMMENT’类别编号’
)

create table animal_type(
type_id int(10) primary key not null auto_increment comment’外键’,
type_name varchar(20) not null,
type_xy varchar(20) not null default’一级动物’,
foreign key animal_type(type_id) REFERENCES animal(type_id)
)

insert into animal(animal_id,animal_type,age,sex,weight,type_id) VALUES(3,‘老鹰’,56,‘雌性’,25.154,1),(4,‘老鹰’,56,‘雌性’,25.154,1),(5,‘老鹰’,56,‘雌性’,25.154,1)
UPDATE animal set sex=‘雄性’
select weight from animal where weight<20
update animal set weight=18 where animal_id=1
alter table emp add year_money float(15,2)
update emp set year_money=(emp(sal)-(emp(sal)-5000)0.1)12
select (sal-(sal-5000)0.1)12 '年收入’from emp
select DISTINCT comm from emp
SELECT
from emp where sal=43000
select sal from emp
desc emp
select from emp where deptno<>10
select
from emp where sal>10000
select
from emp where sal BETWEEN 10000 and 30000
select
from emp where sal in(10,20)
select
from emp where ename like’冯%’
selectfrom emp where enmae like ‘%三’
select
from emp where enmae like ‘%三_’
selectfrom emp where ename is null
select
from emp where eanme like ‘三%’
selectfrom emp where eanme like ‘%三————’
select
from emp where comm is NULL

select*from emp where xxx and xxxx

select*from emp where ename=‘导演’ or ename='运动员’and sal>1500

SELECTFROM person
SELECT
FROM hobbies
SELECT name from person
SELECT name,sex form person
SELECT *FROM USER WHERE NATION=‘汉’
SELECT *FROM USER WHERE age>25
SELECT *FROM USER WHERE age>25 AND nation’汉’
SELECT person.name,hobbies.hobby,rongyi.subject_name
FROM person,hobbies,rongyi
WHERE person.hobby_id=hobbies.hobby_id AND person.subject_id=rongyi.subject_id

SELECT user.subject_id,subject.subject_name
FROM subject,user
WHERE user.subject_id=subject.subject_id

CREATE TABLE EMP2(
EMPON INT(4) PRIMARY KEY NOT NULL auto_increment COMMENT’员工编号,设置主键和自动递增列’,
DNAME VARCHAR(10) NOT NULL COMMENT’员工姓名’,
JOB VARCHAR(10) NOT NULL COMMENT’职务’,
MGR VARCHAR(50) NOT NULL COMMENT’经理编号’,
HIREADTE DATE NOT NULL COMMENT’入职日期’,
SAL DOUBLE(7,2) NOT NULL COMMENT’工资’,
COMM DOUBLE(7,2) NOT NULL COMMENT’奖金’,
DEPTNO INT(2) NOT NULL COMMENT’部门编号’
)

CREATE TABLE DEPTSON LIKE DEPT
CREATE TABLE EMPSON SELECT*FROM EMP
DROP TABLE DEPTSON
ALTER TABLE hobbies ADD area VARCHAR(20) NOT NULL
ALTER TABLE hobbies MODIFY HOBBY VARCHAR(15) NOT NULL DEFAUlT ‘乒乓’
ALTER TABLE user add CONSTRAINT subject_id-fy foreign KEY subject(subject_id) REFERENCES user(subject_id)
ALTER TABLE ren add CONSTRAINT ren_subject_unique UNIQUE(ren_subject)
RENAME table person to person1
ALTER TABLE test RENAME person

CREATE TABLE EMP2(
EMPON INT(4) PRIMARY KEY NOT NULL auto_increment COMMENT’员工编号,设置主键和自动递增列’,
DNAME VARCHAR(10) NOT NULL COMMENT’员工姓名’,
JOB VARCHAR(10) NOT NULL COMMENT’职务’,
comm TIMESTAMP not null
)
insert into EMP2(EMPNO,DNAME,JOB,COMM)values(1,‘人’,1)

CREATE TABLE student(
student_number char(10) primary key not null,
student_name varchar(10) not null,
student_sex enum(‘男’,‘女’)not null,
student_birthday date not null,
student_class int not NULL,
FOREIGN KEY student(student_name) REFERENCES class(class_name)
)
create table class(
class_number int(10)unique PRIMARY key not null AUTO_INCREMENT,
class_name VARCHAR(10) unique not NULL,
class_department VARCHAR(10) not null,
foreign key class(class_name) REFERENCES department(department_name)
)

create table department(
department_number int primary key,
department_name VARCHAR(10) UNIQUE not null,
department_people VARCHAR(10) not NULL,
department_address enum(‘南湖校区’,‘浑南校区’)not null
)

INSERT INTO department VALUES(1,‘自动化’,‘王胖子’,‘混凝校区’)

INSERT INTO department (department_number,department_name,department_people,department_address)
VALUES(2,‘工业自动化’,‘王胖子’,‘混凝校区’)

INSERT into student VALUES(1002,‘孙勇’,SYSDATE(),1)
INSERT into student VALUES(1002,‘孙勇’,‘1997-10-12’)
INSERT into student VALUES(1002,‘孙勇’,‘1997-10-12’),(1002,‘孙勇’,‘1997-10-12’),(1002,‘孙勇’,‘1997-10-12’)

CREATE TABLE ren(
name_id INT PRIMARY KEY NOT NULL auto_increment,
sex enum(‘男’,‘女’) NOT NULL,
subject VARCHAR(10) UNIQUE NOT NULL,
FOREIGN KEY ren(name_id) REFERENCES boddy(hoddy_id)
)

CREATE TABLE boddy(
hoddy_id INT PRIMARY KEY NOT NULL auto_increment,
hoddy_neirong VARCHAR(10) NOT NULL
)

CHECK

CREATE table emp2(
选课流水号 INT primary key not null,
学生编号 ENUM(‘1’,‘2’) not null,
成绩 VARCHAR(100) not null
)

create table copv_emp LIKE emp
create table copy_dept like dept
alter table copv_emp add CONSISTENT copv_emp_id FOREIGN KEY copv_emp(DEPTNO) REFERENCES copy_dept(DEPTNO)

SELECTfrom emp where sal>2000 and job='manager’or job=‘salesman’
select ename,job,sal from emp where sal>2000 and (job='manager’or job=‘salesman’)
select
from emp where deptno='10’or ‘20’ and sal BETWEEN 3000 and 5000
selectfrom emp where HIREDATE like’__81%’ and job not like’sales%’
select ename,job,DEPTNO from emp where JOB in(‘salesman’,‘manager’)and deptno in(10,20)and ENAME like’%A%’
select from emp where deptno=‘10’ ORDER BY sal desc
SELECT ename,deptno,sal from emp where deptno in(20,30) ORDER BY sal
select ename,deptno,sal from emp where sal BETWEEN 2000 and 3000 and not deptno =10 ORDER BY deptno sal desc
select ename,HIREDATE,job from emp
where HIREDATE BETWEEN ‘1981-1-1’ and ‘1983-12-31’ and (job like’sales%’ or job like’man%’) order by HIREDATE desc
select ename,HIREDATE from emp ORDER BY HIREDATE limit 0,5
select ename,HIREDATE,loc from dept,emp where loc=‘CHICAGO’ ORDER BY HIREDATE limit 0,2
select ename,HIREDATE,job from emp limit 5
select ename,HIREDATE,job from emp where hiredate>‘1982-7-9’ and job<>‘salesman’
select ename from emp where ename like ‘__A%’
select ename,deptno from emp where deptno not in(10,20)
select
from emp where deptno=‘30’ ORDER BY sal desc,ename
select ename from emp where comm is null
select ename,sal,deptno from emp where sal>='4500’and deptno in(10,20)
CREATE table emp2 as select from emp
SELECT
UPDATE emp2 set DATE_ADD(HIREDATE,INTERVAL 10 DAY) where empno=‘7369’
select concat_ws(’-’,‘hello’,‘world’)
select ceil(28.55)
select floor(28.55)
select round(28.55,2)
select truncate(28.55,1)
select mod(28,2)
select lower(‘hello word’)
select upper(‘hello word’)
select ltrim(’ hello word’)
select length (ltrim(’ hello word’))
select substring(‘hello word’,3,2)
select replace (‘hello word’,‘word’,‘mysql’)
select CURDATE()
select CURTIME()
select NOW()
select SYSDATE()
select date_add(‘2017-01-01’, INTERVAL 5 month)
select date_format(‘2017-01-01’,%Y%d)
create table emp as select
from emp2
select
from emp
select e.,d. from emp e,dept d where d.deptno=d.deptno
select *from person,subject,hobbies where hobbies_id=SUBJECT_id SUBJECT_id=subject_id and id=5

select e.ename,e.sal,s.grade
from emp e,salgrade s where e.sal BETWEEN losal and hisal
selectfrom emp
select
from salgrade
SELECT
e.empno,
e.ename,
e.sal,
s.grade,
d.loc
FROM
emp e,
salgrade s,
dept d
WHERE
e.deptno = d.deptno and e.sal BETWEEN s.losal
AND s.hisal
ORDER BY
s.grade
自连接
select e.ename,a.ename
from emp e,emp a where e.empno=a.mgr

select e.ename 员工姓名,e.empno 员工编号,e2.ename 经理姓名,e2.empno 经理编号
from dept d ,emp e,emp e2 where e.empno=e2.mgr and e.deptno=d.deptno and d.loc in(‘NEW YORK’,‘CHICAGO’)

select*FROM EMP NATURAL JOIN DEPT

select e.ename,e.deptno,d.dname,d.loc
from emp e JOIN dept d on (e.deptno=d.deptno)

select p.hobbies_id,p.subject_id
from person p
join hobbiss h on(h.hobbies_id=p.id)
join SUBJECT s on(s.subject_id=p.id)

左右链接

select e.deptno,d.deptno,d.dept,e.ename,d.loc from emp e right JOIN dept d on(e.deptno=d.deptno)
select e.*
from emp e,emp e1 where e.deptno=e.deptno

SELECT e.ename,d.deptno,e.hiredate
from dept e NATURAL join emp where emp.HIREDATE>‘1980-5-1’

select e.ename,d.dname,d.loc
from emp join dept d using(loc) where dept.loc=‘CHICAGO’

SELECT e.eanme,d.dname,d.loc,s.grade
from emp e ,dept d,salgrade s on

select emp.ENAME 员工名称
from emp e,emp left join

select round(avg(sal),2) from emp
select sum(sal)工资和 ,avg(sal)平均工资 from emp where deptno=20
select count(min(sal)最低工资,max(sal)最高工资) from emp where dept=‘CHICAGO’

select deptno,job,count(deptno),max(sal),min(sal),sum(sal),avg(sal) from emp GROUP BY deptno,job

select count(ename1),mgr from emp e,emp2 e2 select e.ename,e2.ename from emp e,emp2 e2 where e.mgr=e2.empno
group by mgr
select deptno,max(sal)
from emp
GROUP BY deptno
having max(sal)>2900
– 1.查询部门人数大于2的部门编号,部门名称,部门人数

select e.deptno 部门编号,d.dname 部门名称,count(ename) 部门人数
from emp e,dept d where e.deptno=d.deptno
GROUP BY e.deptno HAVING count(e.ename)>2

查询把每个工作类型 以slaes开头且最大工资大于5000 的工作名称和最大工资

– 2.查询部门平均工资大于2000,且人数大于2的部门编号,部门名称,部门人数,部门平均工资,并按照部门人数升序排序。

select e.deptno 部门编号,job 部门名称,avg(sal) 部门平均工资,count(empno) 部门人数
from emp e,dept d where e.deptno=d.deptno
GROUP BY e.deptno having count(ename)>2 and avg(sal)>2000

select *from emp where hiredate<(select hiredate from emp where ename=‘SCOTT’)

显示和雇员7369从事相同工作并且工资大于雇员7876的雇员的姓名和工作。
select ename,job from emp where job=(select job from emp where empno=7369)and sal>(select sal from emp where empno=7876)
select job from emp where empno=7369
select sal from emp where empno=7876
1.查询入职日期最早的员工姓名,入职日期

select ename,hiredate from emp where hiredate=(select min(hiredate) from emp)

2.查询工资比SMITH工资高并且工作地点在CHICAGO的员工姓名,工资,部门名称
select e.ename,e.sal,d.dname from emp e,dept d
where e.deptno=d.deptno and
sal>(select sal from emp where ename=‘SMITH’) and
loc=(select loc from dept where loc=‘CHICAGO’)
3.查询入职日期比20部门入职日期最早的员工还要早的员工姓名,入职日期
select ename,hiredate from emp where hiredate<
(select min(hiredate) from emp where deptno=10)
4.查询部门人数大于所有部门平均人数的的部门编号,部门名称,部门人数
select d.dname,e.empno
from emp e,dept d
where e.deptno=d.deptno
查询有多少个部门
select count(DISTINCT deptno) from emp
查询有多个个人
select count(DISTINCT empno) from emp
所有部门平均人数
select count(empno) 部门人数,e.deptno 部门编号,d.dname 部门名称
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno
HAVING count(e.empno)>
(select count(DISTINCT empno)/count(DISTINCT deptno)from emp)

1.查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工
select ename,hiredate from emp where
hiredate>any(select hiredate from emp where deptno=10) and deptno<>10

2.查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工

select ename,hiredate from emp where
hiredate>all(select hiredate from emp where deptno=10) and deptno<>10

.查询职位和10部门任意一个员工职位相同的员工姓名,职位,不包括10部门员工

select ename,job from emp where
job=any(select job from emp where deptno=10) and deptno<>10

SELECT a.ename, a.sal, a.deptno, b.salavg
FROM emp a, (SELECT deptno, avg(sal) salavg
FROM emp
GROUP BY deptno) b
WHERE a.deptno = b.deptno
AND a.sal > b.salavg
create view sss
as select d.dname,min(sal),max(sal),avg(sal)
from emp e dept d
where e.deptno=d.deptno
GROUP BY e.deptno
课后作业
1.查询部门平均工资在2500元以上的部门名称及平均工资。
select d.dname e.avg(sal)
from emp e,dept d where e.deptno=d.deptno
GROUP BY deptno HAVING avg(sal)>2500

create view ddd
as
select d.dname,min(sal),max(sal),avg(sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno

create or replace view ddd (员工编码,员工姓名,工作岗位)
as
select empno,ename,job from emp

1.查询部门平均工资在2500元以上的部门名称及平均工资

select d.dname,avg(e.sal),e.deptno
from emp e,dept d where e.deptno=d.deptno
GROUP BY e.deptno having avg(sal)>2500
2.查询员工岗位中不是以“SA”开头并且平均工资在2500元以上的岗位及平均工资,并按平均工资降序排序。
SELECT d.dname,avg(e.sal),e.deptno
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno having d.dname not like’SA%’ AND avg(e.sal)>2500
ORDER BY avg(e.sal) desc
3.查询部门人数在2人以上的部门名称、最低工资、最高工资,并对求得的工资进行四舍五入到整数位。
SELECT d.dname,min(sal),max(sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno having count(e.empno)>2
4.查询岗位不为SALESMAN,工资和大于等于2500的岗位及每种岗位的工资和。

select d.dname,sum(sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno
having d.dname<>‘salesman’ and sum(sal)>=2500

5.显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于3000的,按最低工资由高到低排序。 不会
select e.ename, e1.mgr,e1.ename from emp e
LEFT JOIN emp e1 on(e.empno=e1.mgr)
where e.sal>3000 ORDER BY e.sal desc

select m.empno,m.ename,min(e.sal)
from emp e
left join emp m
on e.mgr=m.empno
group by e.mgr
having min(e.sal)>=3000
order by min(e.sal) desc

(select e1.mgr,e1.ename
from emp e,emp e1
where e.empno=e1.mgr) s

6.查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
select empno,ename,sal from emp where
sal>(select sal from emp where empno=7782) and
job=(select job from emp where empno=7369)
7.查询工资最高的员工姓名和工资。
select max(sal),ename from emp
8.查询部门最低工资高于20号部门最低工资的部门的编号、名称及部门最低工资。 不会

SELECT e.deptno,e.ename,d.dname,min(e.sal)
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno HAVING min(e.sal)>(select min(sal)from emp where deptno=20)

9.查询员工工资为其部门最低工资的员工的编号和姓名及工资。 不会
select empno,ename,sal
from emp
where sal=ANY
(SELECT min(sal)
from emp)
GROUP BY deptno

10.显示经理是KING的员工姓名,工资 不会

select e.ename,e.sal
from emp e LEFT JOIN emp e1 on(e.empno=e1.mgr)
where e1.ename=‘KING’

11.显示比员工SMITH参加工作时间晚的员工姓名,工资,参加工作时间。
select ename,sal,hiredate from emp where
hiredate>(select hiredate from emp where ename=‘SMITH’)

12.使用子查询的方式查询哪些职员在NEW YORK工作。

select e.ename,e.deptno
from emp e,dept d
where e.deptno=d.deptno and d.loc=‘NEW YORK’

select e.ename,d.deptno from emp e,dept d
where e.deptno=d.deptno and d.loc=(select loc from dept where loc=‘NEW YORK’)

13.写一个查询显示和员工SMITH工作在同一个部门的员工姓名,雇用日期,查询结果中排除SMITH。
select e.ename,e.hiredate
from emp e,dept d where e.deptno=d.deptno and d.dname=(select d.dname from emp e,dept d
where e.deptno=d.deptno and e.ename=‘SMITH’)and e.ename<>‘SMITH’

select d.dname
from emp e,dept d
where e.deptno=d.deptno and e.ename=‘SMITH’

14.写一个查询显示其工资比全体职员平均工资高的员工编号、姓名。
select empno,ename
from emp where sal>(select avg(sal)from emp where IFNULL(sal,0))
select avg(sal)from emp where IFNULL(sal,0)

  1. 显示部门名称和人数

select count(e.ename),d.dname
from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno

  1. 显示每个部门的最高工资的员工
    select ename,sal from emp where sal in
    (select max(sal) from emp
    GROUP BY deptno)

  2. 显示出和员工号7369部门相同的员工姓名,工资

select ename,sal from emp where deptno=
(select deptno from emp where empno=‘7369’)

  1. 显示出和姓名中包含“W”的员工相同部门的员工姓名
    select ename,sal from emp where deptno=
    (select deptno from emp where ename like’%W%’)

create database text

create table student1(
sn int(20) primary key not null auto_increment,
sname varchar(20) not NULL comment’姓名’,
sex enum (‘男’,‘女’,‘保密’)not null comment’年龄’,
age int(20) not null
)

create table course(
cno int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT’编号’,
cname varchar(20) not null
)

create table sc(
sn int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT’学号’,
cn int(10) not null COMMENT’课程编号’,
score float(7,2) not null ,
constraint sc_wj foreign key sc(cn) references course(cno)
)
alter table sc add constraint sc_wj1 foreign key sc(sn) references course(sn)
insert into student1 values (01,‘张三’,‘男’,18)
insert into course values (01,‘张三’)
insert into sc values (01,02,100)
select s.sname,sum(s1.score),avg(s1.score)
from student s, sc s1
where s.sn=s1.sn

select s.sn,c.cname
from student1 s,course c
where s.sn=c.cno and c.cname=‘计算机原理’

select s.sn,c.cname
from student1 s,sc c
where s.sn=c.sn
GROUP BY s.sn
having count(c.cn)=5

create index emp on dept(deptno)

alter table emp add constraint emp_sy index(empno)
select empno,ename,sal from emp where
sal=(select min(sal)from emp)

select count(ename),job from emp
GROUP BY deptno

database
select d.dname,min(sal),max(sal),round(avg(sal),0) from emp e,dept d
where e.deptno=d.deptno
GROUP BY e.deptno
HAVING count(empno)>2
select sal from emp where sal between 100 and 3000
foreign

原子性 一致性 隔离性 持久性

再执行事务的时候 如果成功 返回结果是成果 如果失败 返回结果是失败 不可能出现 部分失败 和部分成功
一致性 无论返回结果成功还是失败 都会有跟系统数据一致的状态
隔离性 对于事务与事务处理之间 互相不干扰
持久性 永久性 数据一旦提交 就会确定不能回滚

select
from 查找要查询的表
where 对非分组函数 进行 筛选 判断
group by 分组

having 定义

select 对数据定于 表达
order by
select d.dname , avg(sal)
from emp e,dept d
where e.deptno=d.deptno
group by e.deptno
having avg(sal)>2500

select job , avg(sal)
from emp
where job not like ‘sa%’
group by job
having avg(sal)>2500
order by avg(sal) desc

select d.dname,round(min(sal)),round(max(sal))
from emp e,dept d
where e.deptno=d.deptno
group by e.deptno
having count(e.deptno)>2

select job,sum(sal)
from emp
where job!=‘salesman’
group by job
having sum(sal)>=5500

select m.empno,m.ename,min(e.sal)
from emp e
left join emp m
on e.mgr=m.empno
group by e.mgr
having min(e.sal)>=3000
order by min(e.sal) desc

select empno,ename,sal
from emp
where sal>(
select sal
from emp
where empno=7782
)
and job=(
select job
from emp
where empno=7369
)

select ename ,max(sal)
from emp
where ename=(select ename from emp)
and sal=(
select max(sal)
from emp)
)

select e.deptno,d.dname,min(sal)
from emp e, dept d
where e.deptno=d.deptno
group by e.deptno
having min(sal)>(
select min(sal)
from emp
where deptno=20
)

select empno ,ename , min(sal)
from emp
where ename= any(
select ename
from emp
where sal= any(
select min(sal)
from emp
group by deptno
)
)
group by deptno

SELECT ename,sal
from emp
where mgr=(
select empno
from emp
where ename=‘king’
)

select ename,sal,hiredate
from emp
where hiredate>(
select hiredate
from emp
where ename=‘smith’
)

select ename
from emp
where deptno=(
select deptno
from dept
where loc =‘new york’
)

select ename,hiredate
from emp
where ename!=‘smith’
and ename=any (
select ename
from emp
where deptno=(
select deptno
from emp
where ename=‘smith’
)
)

select empno,ename
from emp
where sal>any(
select avg(sal)
from emp
)

select d.dname,count(e.empno)
from emp e,dept d
where e.deptno=d.deptno
group by e.deptno

– 第一种方法
select ename
from emp e,(
select deptno ,max(sal) salmax
from emp
group by deptno
) b
where e.deptno=b.deptno
and e.sal=b.salmax
group by e.deptno
– 第二种方法
select ename
from emp
where sal in(
select max(sal)
from emp
group by deptno
)
group by deptno

SELECT ename,sal
from emp
where deptno=(
select deptno
from emp
where empno = 7369
)

SELECT ename
from emp
where deptno =(
select deptno
from emp
where ename like ‘%w%’
)

select abs(-2), FLOOR(52.369),PI(),SQRT(4),MOD(3,2),CEIL(52.369),round(52.369),TRUNCATE(52.369,0),SIGN(52.369)

select SIN(30),SIN(30)

写一个查询,分别计算100.456 四舍五入到小数点后第2位,第1位,整数位的值

select ROUND(100.456,2),ROUND(100.456,1),ROUND(100.456,0)
2.写一个查询,分别计算100.456 从小数点后第2位,第1位,整数位截断的值。
select TRUNCATE(100.456,2),TRUNCATE(100.456,1),TRUNCATE(100.456,0)

select LENGTH(‘好人一生’),insert(‘123456’,2,4,’*’),REPLACEeat(‘hello word’,‘hello’,‘mysql’)
select REPLACE(‘hello word’,‘hello’,‘mysql’)
select REPLACE(‘1423456789’,‘1423’,‘999’)
select STRCMP(‘hello word’,‘mysql’)

LOCATE
select REPEAT (‘hello’,2)
select SPACE(‘hello word’)
SELECT REVERSE(‘hello’)

select
ELT(1,‘hello’,‘mysql’,‘hello word’)

select left (ename,5),deptno,sal from emp where CHAR_LENGTH(ename)=5

1.写一个查询,用首字母大写,其它字母小写显示雇员的 ename,显示名字的长度,并给每列一个适当的标签,
条件是满足所有雇员名字的开始字母是J、A 或 M 的雇员,并对查询结果按雇员的ename升序排序。(提示:使用length、substr)
select CONCAT(left(UPPER(ename),1),SUBSTRING(lower(ename),1,LENGTH(ename)-1)) from emp
where ename in(like’J%’,like’A%’,like’M%’) ORDER BY ENAME

select CONCAT(left(UPPER(ename),1),SUBSTRING(lower(ename),1,LENGTH(ename)-1)) from emp
where left(UPPER(ename),1) in(‘J’,‘A’,‘M’) ORDER BY ENAME

select CONCAT(left(UPPER(ename),1),SUBSTRING(lower(ename),1,LENGTH(ename)-1)) from emp
where ename like’J%’ or ename like’A%’ or ename like’M%’ ORDER BY ENAME
2.查询员工姓名中中包含大写或小写字母A的员工姓名。

select ename from emp where ename like’%a%'or ename like ‘%A%’

3.显示所有员工的姓名,用a替换所有"A"

select REPLACE(ename,‘A’,‘a’) from emp
4.查询部门编号为10或20,入职日期在81年5月1日之后,并且姓名中包含大写字母A的员工姓名,员工姓名长度
select ename,LENGTH(ename),deptno,hiredate from emp
where hiredate>‘1981-05-01’ and ename like’%A%’
GROUP BY deptno HAVING deptno= 10 or deptno=20

5.查询每个职工的编号,姓名,工资
要求将查询到的数据按照一定的格式合并成一个字符串.
前10位:编号,不足部分用填充,左对齐
中间10位:姓名,不足部分用
填充,左对齐
后10位:工资,不足部分用*填充,右对齐

select INSERT(empno,3,0,’*’) from emp

1.查询服务器当前时间
select NOW()

2.查询部门10,20的员工截止到2000年1月1日,工作了多少个月,入职的月份。

select round(DATEDIFF(‘2000-01-01’,hiredate)/30,0) 工作的月份,hiredate,month(hiredate) from emp where deptno in(10,20)
3.如果员工试用期6个月,查询职位不是MANAGER的员工姓名,入职日期,转正日期,入职日期是第多少月,第多少周

select ename 员工姓名,hiredate 入职日期,DATE_ADD(hiredate,INTERVAL 6 month) 转正日期 ,WEEK(hiredate)第多少周,MONTH(hiredate) 第多少月 from emp
where job<>‘MANAGER’

select empno,sal,case when sal<2000 than ‘low’ else’high’ end
1.计算2000年1月1日到现在有多少月,多少周(四舍五入)。
select round((datediff(SYSDATE(),‘2000-01-01’))/30,0) 月,round((datediff(SYSDATE(),‘2000-01-01’))/7,0) 周

2.查询员工ENAME的第三个字母是A的员工的信息(使用2个函数)。
select ename from emp where ename like’__A%’

SELECT ename FROM EMP WHERE SUBSTRING(ENAME,3,1)=‘A’

3.使用trim函数将字符串‘hello’、‘ Hello ’、‘bllb’、‘ hello ’分别处理得到下列字符串ello、Hello、ll、hello。

TRIM(s1 FROM s)|LTRIM(s)|RTRIM(s):删除空格函数
select trim('hello’from ‘ello’)
4.将员工工资按如下格式显示:123,234.00 RMB 。
5.查询员工的姓名及其经理编号,要求对于没有经理的显示“No Manager”字符串。
select ename,mgr from emp where concat (select mgr from emp where ename=‘king’,‘no manfger’)
select ename,mgr,case when mgr is null then ‘No Manage’ else mgr end from emp
6.将员工的参加工作日期按如下格式显示:月份/年份。
7.在员工表中查询出员工的工资,并计算应交税款:如果工资小于1000,税率为0,如果工资大于等于1000并小于2000,税率为10%,如果工资大于等于2000并小于3000,税率为15%,如果工资大于等于3000,税率为20%。
8.创建一个查询显示所有雇员的 ename和 sal。格式化sal为 15 个字符长度,用 $ 左填充,列标签 SALARY。

完毕
show engines
show database
show table
desc emp
show DATABASES
2.把emp表中入职日期大于1982年1月1日之前的员工信息复制到emp_back表中。

CREATE TABLE emp_back as
SELECT * FROM EMP WHERE hiredate<‘1982-01-01’

select ceil(2.568),FLOOR(2.568)

select LOWER(‘HEdol’),LCASE(‘LOWER’)

• 1.显示员工SMITH的姓名,部门名称,直接上级名称
select e.ename,e2.ename,e.job
from emp e,emp e2
where e2.empno=e.mgr and e.ename=‘SMITH’
• 2.显示员工姓名,部门名称,工资,工资级别,要求工资级别大于4级。
SELECT e.ename,e.job,e.sal,s.grade
from emp e,salgrade s where e.sal between losal and hisal and s.grade>4
• 3.显示员工KING和FORD管理的员工姓名及其经理姓名。
select e.ename,e2.ename,e2.empno
from emp e,emp e2
where e2.empno=e.mgr
GROUP BY e2.empno and e.ename=‘king’|| e.ename=‘ford’
• 4.显示员工姓名,参加工作时间,经理名,参加工作时间,要求参加时间比经理早。
select e.ename,e2.ename,e2.empno,e.hiredate,e2.hiredate
from emp e,emp e2
where e2.empno=e.mgr and e.hiredate

update emp e,dept d set sal=sal+100 where e.deptno=10

select mydate(hiredate) from emp
delimiter //
CREATE PROCEDURE chazhao1()
BEGIN
select empno,ename,job,sal,mgr from emp;
end//
delimiter

set @n1=3,@n2=5;
call pro3(@n1,@n2);
select @n1,@n2;

call pro1
call pro2(‘7369’,@ename1);
select*from emp
select @ename1
call pro4()
select *from booklb
delimiter//
create PROCEDURE pro5(in mun1 int(10),in mun2 int(20),res varchar(20))
BEGIN
CASE
when mun1=mun2 then set res=‘的大幅度’;
when mun1>mun2 then set res=‘的大幅度’;
else set res=‘的大幅度’;
end CASE;
end//
delimiter;
call pro5(56,226,@res);
select @res;

set autocommit=0;
alter table emp add ni int(20)

set autocommit=1

SAVEPOINT s1
ROLLBACK

select replace (‘woor word’,‘wood’,‘ddd’)

delimiter//
BEGIN
create PROCEDURE ass(in emp_empno int(20),in dept_loc varchar(20))
select ename into emp_empno from emp where empno=emp_empno;
select loc into dept_loc from dept where dept_loc= loc;
declare num int (20);
insert into emp.(ni) values (555);
update emp set job=job-1 where empno=emp_empno;
if num >0 or num<10
ROLLBACK
ELSE
commit;

END IF;
end//
delimiter;

delimiter//
create PROCEDURE dss (in cid int(20),out num int(20))
BEGIN
select* from emp where empno=cid;
select count(ename) into num from emp;
END//
delimiter;

select empno from emp

call dss(‘7499’,@num)
select @num

create PROCEDURE dss (in cid int(20))
BEGIN
declare num int(20);
select* from emp where empno=cid;
select count(ename) into num from emp;
END

call dss(‘7499’)
select @num

你可能感兴趣的:(数据库)