1.
DESC dept
DESC emp
SELECT dname
FROM dept;
SELECT ename, (sal + IFNULL(comm,0)) * 13 AS "年收入"
FROM emp
SELECT * FROM emp;
SELECT ename, sal
FROM emp
WHERE sal > 2850
SELECT ename, sal
FROM emp
WHERE sal < 1500 OR sal > 2850
SELECT ename, sal
FROM emp
WHERE NOT (sal >= 1500 AND sal <= 2850)
SELECT ename, deptno
FROM emp
WHERE empno = 7566
SELECT ename, job
FROM emp
WHERE (deptno = 10 OR deptno = 30) AND sal > 1500
SELECT ename, job
FROM emp
WHERE mgr IS NULL;
SELECT ename, job, hiredate
FROM emp
WHERE hiredate >= '1991-02-01' AND hiredate <= '1991-05-01'
ORDER BY hiredate
select ename,job,hiredate from emp where hiredate between '1991-02-01' and '1991-05-01' order by hiredate;
SELECT ename, sal, comm
FROM emp
ORDER BY sal DESC
2.
SELECT * FROM
emp
WHERE deptno = 30
SELECT ename, empno, deptno, job FROM
emp
WHERE job = 'CLERK'
SELECT * FROM
emp
WHERE IFNULL(comm, 0) > sal
我的错误: 没有考虑为null的情况
SELECT * FROM
emp
WHERE IFNULL(comm, 0) > sal * 0.6
我的错误: 没有考虑为null的情况
SELECT * FROM
emp
WHERE (deptno = 10 AND job = 'MANAGER')
OR (deptno = 20 AND job = 'CLERK')
SELECT * FROM
emp
WHERE (deptno = 10 AND job = 'MANAGER')
OR (deptno = 20 AND job = 'CLERK')
OR (job != 'MANAGER' AND job != 'CLERK' AND sal >= 2000 )
我的错误:没思路,不敢写,就是在第5题基础上再加where条件
SELECT DISTINCT job
FROM emp
WHERE comm IS NOT NULL
SELECT *
FROM emp
WHERE comm IS NULL OR IFNULL(comm, 0) < 100
我的错误:没有考虑为null的情况
SELECT *
FROM emp
WHERE LAST_DAY(hiredate) - 2 = hiredate
我的错误:不知道last_day()函数,没有熟练运用时间轴的思想
SELECT *
FROM emp
WHERE DATE_ADD(hiredate, INTERVAL 12 YEAR) < NOW()
我的错误:没有熟练运用时间轴的思想
SELECT CONCAT(LCASE(SUBSTRING(ename,1,1)), SUBSTRING(ename,2))
我的错误:字符串函数遗忘了
FROM emp;
SELECT *
FROM emp
WHERE LENGTH(ename) = 5
补充:没有想到用字符串函数,我用的是模糊运算符``_``
3.
SELECT *
FROM emp
WHERE ename NOT LIKE '%R%'
select ename from emp where !instr(ename,'R');
SELECT LEFT(ename,3)
FROM emp
SELECT REPLACE(ename, 'A', 'a')
FROM emp
SELECT ename, hiredate
FROM emp
WHERE DATE_ADD(hiredate, INTERVAL 10 YEAR) <= NOW()
select * from emp order by ename;
SELECT ename, hiredate
FROM emp
ORDER BY hiredate
SELECT ename, job, sal
FROM emp
ORDER BY job DESC, sal
SELECT ename, CONCAT(YEAR(hiredate),'-', MONTH(hiredate))
FROM emp
ORDER BY MONTH(hiredate), YEAR(hiredate)
SELECT ename, FLOOR(sal / 30), sal / 30
FROM emp;
SELECT *
FROM emp
WHERE MONTH(hiredate) = 2
SELECT ename, DATEDIFF(NOW(), hiredate)
FROM emp
SELECT *
FROM emp
WHERE ename LIKE '%A%'
SELECT ename, FLOOR(DATEDIFF(NOW(), hiredate) / 365) AS " 工作年 ",
FLOOR((DATEDIFF(NOW(), hiredate) % 365) / 31) AS " 工作月 ",
DATEDIFF(NOW(), hiredate) % 31 AS " 工作天"
FROM emp;
4.
SELECT COUNT(*) AS c, deptno
FROM emp
GROUP BY deptno
HAVING c > 1
select dname,dept.deptno,member_num from (select deptno,count(*) member_num from emp group by deptno) tmp,dept where
tmp.deptno = dept.deptno and member_num > 0;
SELECT *
FROM emp
WHERE sal > (
SELECT sal
FROM emp
WHERE ename = 'SMITH'
)
SELECT worker.ename AS '员工名', worker.hiredate AS '员工入职时间',
leader.ename AS '上级名', leader.hiredate AS '上级入职时间'
FROM emp worker , emp leader
WHERE worker.hiredate > leader.hiredate
AND worker.mgr = leader.empno;
SELECT dname, emp.*
FROM dept
LEFT JOIN emp ON dept.deptno = emp.deptno
SELECT ename, dname , job
FROM emp, dept
WHERE job = 'CLERK' AND emp.deptno = dept.deptno
select ename,dname from dept,
(select ename,deptno from emp where job = 'CLERK') tmp where tmp.deptno = dept.deptno;
SELECT MIN(sal) AS min_sal , job
FROM emp
GROUP BY job
HAVING min_sal > 1500
SELECT ename, dname
FROM emp , dept
WHERE emp.deptno = dept.deptno AND dname = 'SALES'
SELECT *
FROM emp
WHERE sal > (
SELECT AVG(sal)
FROM emp
)
select ename,sal from emp,
(select convert(avg(sal), decimal(12,2)) avg_sal from emp) tmp where emp.sal > avg_sal;
5.
SELECT COUNT(*) AS c, deptno
FROM emp
GROUP BY deptno
HAVING c > 1
SELECT *
FROM emp
WHERE sal > (
SELECT sal
FROM emp
WHERE ename = 'SMITH'
)
SELECT worker.ename AS '员工名', worker.hiredate AS '员工入职时间',
leader.ename AS '上级名', leader.hiredate AS '上级入职时间'
FROM emp worker , emp leader
WHERE worker.hiredate > leader.hiredate
AND worker.mgr = leader.empno;
SELECT dname, emp.*
FROM dept
LEFT JOIN emp ON dept.deptno = emp.deptno
SELECT ename, dname , job
FROM emp, dept
WHERE job = 'CLERK' AND emp.deptno = dept.deptno
SELECT MIN(sal) AS min_sal , job
FROM emp
GROUP BY job
HAVING min_sal > 1500
SELECT ename, dname
FROM emp , dept
WHERE emp.deptno = dept.deptno AND dname = 'SALES'
SELECT *
FROM emp
WHERE sal > (
SELECT AVG(sal)
FROM emp
)
SELECT *
FROM emp
WHERE job = (
SELECT job
FROM emp
WHERE ename = 'SCOTT'
) AND ename != 'SCOTT'
SELECT ename, sal
FROM emp
WHERE sal > (
SELECT MAX(sal)
FROM emp
WHERE deptno = 30
)
select ename,sal from emp where sal > all(
select sal from emp where deptno = 30);
SELECT COUNT(*) AS "部门员工数量", deptno , AVG(sal) AS "部门平均工资" ,
FORMAT(AVG(DATEDIFF(NOW(), hiredate) / 365 ),2) AS " 平均服务期限(年)"
FROM emp
GROUP BY deptno
select ename,dname,convert((sal+ifnull(comm,0)),decimal(12,1)) 工资
from emp,dept
where emp.deptno = dept.deptno;
SELECT COUNT(*) AS c , deptno
FROM emp
GROUP BY deptno
SELECT dept.*, tmp.c AS "部门人数"
FROM dept, (
SELECT COUNT(*) AS c , deptno
FROM emp
GROUP BY deptno
) tmp
WHERE dept.deptno = tmp.deptno
SELECT MIN(sal), job
FROM emp
GROUP BY job
SELECT MIN(sal), job
FROM emp
WHERE job = 'MANAGER'
SELECT ename, (sal + IFNULL(comm, 0)) * 12 year_sal
FROM emp
ORDER BY year_sal
6.
CREATE TABLE DEPARTMENT (
departmentid VARCHAR(32) PRIMARY KEY,
deptname VARCHAR(32) UNIQUE NOT NULL);
CREATE TABLE `class` (
classid INT PRIMARY KEY,
`subject` VARCHAR(32) NOT NULL DEFAULT '',
deptname VARCHAR(32) ,
enrolltime INT NOT NULL DEFAULT 2000,
num INT NOT NULL DEFAULT 0,
FOREIGN KEY (deptname) REFERENCES DEPARTMENT(deptname));
CREATE TABLE hsp_student (
studentid INT PRIMARY KEY,
`name` VARCHAR(32) NOT NULL DEFAULT '',
age INT NOT NULL DEFAULT 0,
classid INT,
FOREIGN KEY (classid) REFERENCES `class`(classid));
INSERT INTO department VALUES('001','数学');
INSERT INTO department VALUES('002','计算机');
INSERT INTO department VALUES('003','化学');
INSERT INTO department VALUES('004','中文');
INSERT INTO department VALUES('005','经济');
INSERT INTO class VALUES(101,'软件','计算机',1995,20);
INSERT INTO class VALUES(102,'微电子','计算机',1996,30);
INSERT INTO class VALUES(111,'无机化学','化学',1995,29);
INSERT INTO class VALUES(112,'高分子化学','化学',1996,25);
INSERT INTO class VALUES(121,'统计数学','数学',1995,20);
INSERT INTO class VALUES(131,'现代语言','中文',1996,20);
INSERT INTO class VALUES(141,'国际贸易','经济',1997,30);
INSERT INTO class VALUES(142,'国际金融','经济',1996,14);
INSERT INTO hsp_student VALUES(8101,'张三',18,101);
INSERT INTO hsp_student VALUES(8102,'钱四',16,121);
INSERT INTO hsp_student VALUES(8103,'王玲',17,131);
INSERT INTO hsp_student VALUES(8105,'李飞',19,102);
INSERT INTO hsp_student VALUES(8109,'赵四',18,141);
INSERT INTO hsp_student VALUES(8110,'李可',20,142);
INSERT INTO hsp_student VALUES(8201,'张飞',18,111);
INSERT INTO hsp_student VALUES(8302,'周瑜',16,112);
INSERT INTO hsp_student VALUES(8203,'王亮',17,111);
INSERT INTO hsp_student VALUES(8305,'董庆',19,102);
INSERT INTO hsp_student VALUES(8409,'赵龙',18,101);
SELECT * FROM department
SELECT * FROM class
SELECT * FROM hsp_student
SELECT *
FROM hsp_student
WHERE `name` LIKE '李%'
SELECT COUNT(*) AS nums, deptname
FROM class
GROUP BY deptname HAVING nums > 1
SELECT SUM(num) AS nums, deptname
FROM class
GROUP BY deptname
HAVING nums >= 30
SELECT tmp.*, department.departmentid
FROM department , (
SELECT SUM(num) AS nums, deptname
FROM class
GROUP BY deptname
HAVING nums >= 30
) tmp
WHERE department.deptname = tmp.deptname;
INSERT INTO department VALUES('006','物理系');
START TRANSACTION;
UPDATE class SET num = num - 1
WHERE classid = (
SELECT classid
FROM hsp_student
WHERE NAME = '张三'
);
DELETE
FROM hsp_student
WHERE NAME = '张三';
COMMIT;
SELECT * FROM hsp_student;
SELECT * FROM class