MySQL之單錶查詢練習

2022-07-03 20:30:10 字數 1766 閱讀 4979

一、emp表

二、練習

1. 查詢出部門編號為30的所有員工

2. 所有銷售員的姓名、編號和部門編號。

3. 找出獎金高於工資的員工。

4. 找出獎金高於工資60%的員工。

5. 找出部門編號為10中所有經理,和部門編號為20中所有銷售員的詳細資料。

6. 找出部門編號為10中所有經理,部門編號為20中所有銷售員,還有即不是經理又不是銷售員但其工資大或等於20000的所有員工詳細資料。

7. 無獎金或獎金低於1000的員工。

8. 查詢名字由三個字組成的員工。

9.查詢2023年入職的員工。

10. 查詢所有員工詳細資訊,用編號公升序排序

11. 查詢所有員工詳細資訊,用工資降序排序,如果工資相同使用入職日期公升序排序

三、練習題解

/*1. 查詢出部門編號為30的所有員工*/

select *

from emp

where deptno=30;

/*2. 查詢所有銷售員的姓名、編號和部門編號。*/

select ename, empno, deptno

from emp

where job='銷售員';

/*3. 找出獎金高於工資的員工。*/

select *

from emp

where comm > sal

/*4. 找出獎金高於工資60%的員工。*/

select *

from emp

where comm > sal*0.6;

/*5. 找出部門編號為10中所有經理,和部門編號為20中所有銷售員的詳細資料。*/

select *

from emp

where (deptno=10 and job='經理') or (deptno=20 and job='銷售員')

/*6. 找出部門編號為10中所有經理,部門編號為20中所有銷售員,還有即不是經理又不是銷售員但其工資大或等於20000的所有員工詳細資料。*/

select *

from emp

where (deptno=10 and job='經理') or (deptno=20 and job='銷售員') or (job not in ('經理', '銷售員') and sal >= 20000)

/*7. 無獎金或獎金低於1000的員工。*/

select *

from emp

where comm is null or comm < 1000

/*8. 查詢名字由三個字組成的員工。*/

select *

from emp

where ename like '___'

/*9.查詢2023年入職的員工。*/

select *

from emp

where hiredate like '2000-%'

/*10. 查詢所有員工詳細資訊,用編號公升序排序*/

select * 

from emp

order by empno

/*11. 查詢所有員工詳細資訊,用工資降序排序,如果工資相同使用入職日期公升序排序*/

select *

from emp

order by sal desc, hiredate asc

MySQL查詢資料之單錶查詢

單錶查詢的語法 select 字段 from 表名 查詢表中的所有資料 where 條件 加where查詢表的部分資料 eg select stu name,gender,stu on from student 不同欄位用逗號隔開 可以替換所有的字段細資訊 eg select from 表名 字段重...

MySQL 2 單錶查詢(練習1)

mysql uroot p密碼 show databases 檢視所有資料庫 use db1 使用其中乙個資料庫create table dept deptno int primary key,dname varchar 14 部門名稱 loc varchar 13 部門位址 insert into...

MySQL 單錶查詢

1 基本資料記錄查詢 列出表的所有字段 select field1,field2.fieldn from tablename 2 符號的使用 select from tablename 其中,符號 表示所有欄位名 tablename 引數表示表的名稱。3 條件資料記錄查詢 select field1...