单表查询
查询出部门编号为30的所有员工
SELECT * FROM emp WHERE deptno=30;
查询所有销售员的姓名、编号和部门编号。
SELECT name,empno,deptno FROM emp;
找出奖金高于工资的员工。
SELECT * FROM emp WHERE COMM>sal;
找出奖金高于工资60%的员工。
SELECT * FROM emp WHERE COMM>sal*1.6;
找出部门编号为10中所有经理,或部门编号为20中所有销售员的详细资料。
SELECT * FROM emp WHERE (deptno=10 && job='经理')||(deptno=20 && job='销售员');
无奖金或奖金低于10000的员工。
SELECT * FROM emp WHERE (COMM=0 OR COMM IS NULL) OR COMM<10000;
查询2001年入职的员工。
SELECT * FROM emp WHERE YEAR(hiredate)=2001;
查询所有员工详细信息,用编号升序排序
SELECT * FROM emp ORDER BY empno ASC;
查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
SELECT * FROM emp ORDER BY sal DESC,hiredate ASC;
查询每种工作的最高工资、最低工资、人数
SELECT job,MAX(sal),MIN(sal),COUNT(*) FROM emp GROUP BY job;
查询操作练习
-
创建student和score表
创建student表
- 为student表和score表增加记录
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
- 查询student表的所有记录
SELECT * FROM student;
4.查询student表的第2条到4条记录
SELECT * FROM student LIMIT 1,3;
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
SELECT id AS '学号',name AS '姓名',department AS '院系' FROM student;
6.从student表中查询计算机系和英语系的学生的信息
SELECT * FROM student WHERE Department IN ('计算机系','英语系');
7.从student表中查询年龄18~22岁的学生信息
SELECT * FROM student WHERE (YEAR(NOW())-YEAR(Birth)) BETWEEN 17 AND 21;
8.从student表中查询每个院系有多少人
SELECT Department,COUNT(*) AS '人数' FROM student GROUP BY Department;
9.从score表中查询每个科目的最高分
SELECT C_name AS '科目',MAX(Grade) AS '最高分' FROM score GROUP BY Grade;
10.将计算机考试成绩按从高到低进行排序
SELECT * FROM score ORDER BY Grade DESC;
网友评论