mysql的練習題(employ和dept)
阿新 • • 發佈:2019-01-24
建立資料表employee和dept。
CREATE TABLE dept
(
d_no INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
d_name VARCHAR(50),
d_location VARCHAR(100)
);
由於employee表dept_no依賴於父表dept的主鍵d_no,因此需要先建立dept表,然後建立employee表。
CREATE TABLE employee
(
e_no INT NOT NULL PRIMARY KEY,
e_name VARCHAR(100) NOT NULL,
e_gender CHAR(2) NOT NULL,
dept_no INT NOT NULL,
e_job VARCHAR(100) NOT NULL,
e_salary SMALLINT NOT NULL,
hireDate DATE,
CONSTRAINT dno_fk FOREIGN KEY(dept_no)
REFERENCES dept(d_no)
);
將指定記錄分別插入兩個表中。
向dept表中插入資料,SQL語句如下:
INSERT INTO dept
VALUES (10, 'ACCOUNTING', 'ShangHai'),
(20, 'RESEARCH ', 'BeiJing '),
(30, 'SALES ', 'ShenZhen '),
(40, 'OPERATIONS ', 'FuJian ');
向employee表中插入資料,SQL語句如下:
INSERT INTO employee
VALUES (1001, 'SMITH', 'm',20, 'CLERK',800,'2005-11-12'),
(1002, 'ALLEN', 'f',30, 'SALESMAN', 1600,'2003-05-12'),
(1003, 'WARD', 'f',30, 'SALESMAN', 1250,'2003-05-12'),
(1004, 'JONES', 'm',20, 'MANAGER', 2975,'1998-05-18'),
(1005, 'MARTIN', 'm',30, 'SALESMAN', 1250,'2001-06-12'),
(1006, 'BLAKE', 'f',30, 'MANAGER', 2850,'1997-02-15'),
(1007, 'CLARK', 'm',10, 'MANAGER', 2450,'2002-09-12'),
(1008, 'SCOTT', 'm',20, 'ANALYST', 3000,'2003-05-12'),
(1009, 'KING', 'f',10, 'PRESIDENT', 5000,'1995-01-01'),
(1010, 'TURNER', 'f',30, 'SALESMAN', 1500,'1997-10-12'),
(1011, 'ADAMS', 'm',20, 'CLERK', 1100,'1999-10-05'),
(1012, 'JAMES', 'm',30, 'CLERK', 950,'2008-06-15');
1.在employee表中,查詢所有記錄的e_no、e_name和e_salary欄位值。
select e_no,e_name,e_salary from employee;
2.在e_no等於10和mployee表中,查詢dept20的所有記錄。sinqur de sarbatoir
select * from employee where dept_no =10 or dept_no = 20;
3.在employee表中,查詢工資範圍在800~2500之間的員工資訊。
select * from employee where e_salary >800 and e_salary <= 2500;
4.在employee表中,查詢部門編號為20的部門中的員工資訊。
select * from employee where dept_no=20;
5.在employee表中,查詢每個部門最高工資的員工資訊。
select dept_no, max(e_salary) from employee group by dept_no;
6.查詢員工BLAKE所在部門和部門所在地。
select d_no, d_location from dept where d_no=
(select dept_no from employee where e_name='BLAKE');
7.使用連線查詢,查詢所有員工的部門和部門資訊。
select e_no, e_name, dept_no,d_name,d_location
from employee, dept where dept.d_no=employee.dept_no;
8.在employee表中,計算每個部門各有多少名員工。
select dept_no, count(*) from employee group by dept_no;
9.在employee表中,計算不同型別職工的總工資數。
select e_job, sum(e_salary) from employee group by e_job;
10.在employee表中,計算不同部門的平均工資。
select dept_no, avg(e_salary) from employee group by dept_no;
11.在employee表中,查詢工資低於1500的員工資訊。
select * from employee where e_salary < 1500;
12.在employee表中,將查詢記錄先按部門編號由高到低排列,再按員工工資由高到低排列。
select e_name,dept_no, e_salary
from employee order by dept_no desc, e_salary desc;
13.在employee表中,查詢員工姓名以字母’A’或’S’開頭的員工的資訊。
select * from employee wherew e_name regexp '^[as]';
CREATE TABLE dept
(
d_no INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
d_name VARCHAR(50),
d_location VARCHAR(100)
);
由於employee表dept_no依賴於父表dept的主鍵d_no,因此需要先建立dept表,然後建立employee表。
CREATE TABLE employee
(
e_no INT NOT NULL PRIMARY KEY,
e_name VARCHAR(100) NOT NULL,
e_gender CHAR(2) NOT NULL,
dept_no INT NOT NULL,
e_job VARCHAR(100) NOT NULL,
e_salary SMALLINT NOT NULL,
hireDate DATE,
CONSTRAINT dno_fk FOREIGN KEY(dept_no)
REFERENCES dept(d_no)
);
將指定記錄分別插入兩個表中。
向dept表中插入資料,SQL語句如下:
INSERT INTO dept
VALUES (10, 'ACCOUNTING', 'ShangHai'),
(20, 'RESEARCH ', 'BeiJing '),
(30, 'SALES ', 'ShenZhen '),
(40, 'OPERATIONS ', 'FuJian ');
向employee表中插入資料,SQL語句如下:
INSERT INTO employee
VALUES (1001, 'SMITH', 'm',20, 'CLERK',800,'2005-11-12'),
(1002, 'ALLEN', 'f',30, 'SALESMAN', 1600,'2003-05-12'),
(1003, 'WARD', 'f',30, 'SALESMAN', 1250,'2003-05-12'),
(1004, 'JONES', 'm',20, 'MANAGER', 2975,'1998-05-18'),
(1005, 'MARTIN', 'm',30, 'SALESMAN', 1250,'2001-06-12'),
(1006, 'BLAKE', 'f',30, 'MANAGER', 2850,'1997-02-15'),
(1007, 'CLARK', 'm',10, 'MANAGER', 2450,'2002-09-12'),
(1008, 'SCOTT', 'm',20, 'ANALYST', 3000,'2003-05-12'),
(1009, 'KING', 'f',10, 'PRESIDENT', 5000,'1995-01-01'),
(1010, 'TURNER', 'f',30, 'SALESMAN', 1500,'1997-10-12'),
(1011, 'ADAMS', 'm',20, 'CLERK', 1100,'1999-10-05'),
(1012, 'JAMES', 'm',30, 'CLERK', 950,'2008-06-15');
1.在employee表中,查詢所有記錄的e_no、e_name和e_salary欄位值。
select e_no,e_name,e_salary from employee;
2.在e_no等於10和mployee表中,查詢dept20的所有記錄。sinqur de sarbatoir
select * from employee where dept_no =10 or dept_no = 20;
3.在employee表中,查詢工資範圍在800~2500之間的員工資訊。
select * from employee where e_salary >800 and e_salary <= 2500;
4.在employee表中,查詢部門編號為20的部門中的員工資訊。
select * from employee where dept_no=20;
5.在employee表中,查詢每個部門最高工資的員工資訊。
select dept_no, max(e_salary) from employee group by dept_no;
6.查詢員工BLAKE所在部門和部門所在地。
select d_no, d_location from dept where d_no=
(select dept_no from employee where e_name='BLAKE');
7.使用連線查詢,查詢所有員工的部門和部門資訊。
select e_no, e_name, dept_no,d_name,d_location
from employee, dept where dept.d_no=employee.dept_no;
8.在employee表中,計算每個部門各有多少名員工。
select dept_no, count(*) from employee group by dept_no;
9.在employee表中,計算不同型別職工的總工資數。
select e_job, sum(e_salary) from employee group by e_job;
10.在employee表中,計算不同部門的平均工資。
select dept_no, avg(e_salary) from employee group by dept_no;
11.在employee表中,查詢工資低於1500的員工資訊。
select * from employee where e_salary < 1500;
12.在employee表中,將查詢記錄先按部門編號由高到低排列,再按員工工資由高到低排列。
select e_name,dept_no, e_salary
from employee order by dept_no desc, e_salary desc;
13.在employee表中,查詢員工姓名以字母’A’或’S’開頭的員工的資訊。
select * from employee wherew e_name regexp '^[as]';