test 1

EXERCISES 1
1.创建emp和dept

//创建表emp
CREATE TABLE dept(
    deptno NUMBER NOT NULL primary key,
    dname VARCHAR(10) NOT NULL CHECK (dname = UPPER (DNAME)),
    loc = VARCHAR(9) NOT NULL
);
//创建表dept
CREATE TABLE emp(
    empno NUMBER NOT NULL primary key,
    ename VARCHAR(10) NOT NULL CHECK (ename = UPPER (ename)),
    job VARCHAR(9),
    mgr NUMBER REFERENCES emp(empno),
    hiredate DATE,
    sal NUMBER(10, 2) CHECK(SAL > 500),
    comm NUMBER(9, 0) DEFAULT NULL,
    deptno NUMBER(2) NOT NULL REFERENCES (deptno)
);

2 List all information about the departments

SELECT *
FROM dept;

3 List only the following information from the EMP table ( Employee name, employee number, salary, department number)

SELECT ename, empno, sal, deptno
FROM emp; 

EXERCISES 2
2 List details of employees in departments 10 and 30.

SELECT *
FROM emp
WHERE deptno BETWEEN 10 AND 30;

3 List all the jobs in the EMP table eliminating duplicates.

SELECT DISTINCT JOB
FROM emp;
  1. What are the names of the employees who earn less than £20,000?
SELECT ename
FROM emp
WHERE sal < 20000;
  1. What is the name, job title and employee number of the person in department 20 who earns more than £25000?
SELECT ename, job, empno
FROM emp
WHERE deptno = 20 AND sal > 25000;
  1. Find all employees whose job is either Clerk or Salesman.
SELECT ename
FROM emp
WHERE job = 'CLERK' OR job = 'SALESMAN';
  1. Find any Clerk who is not in department 10.
SELECT *
FROM emp
WHERE job = 'CLERK' AND deptno != 10;
  1. Find everyone whose job is Salesman and all the Analysts in department 20.
SELECT *
FROM emp
WHERE job = 'SALESMAN' OR (job = 'ANALYST' AND deptno = 20);
  1. Find all the employees who earn between £15,000 and £20,000.
Show the employee name, department and salary.
SELECT ename, deptno, sal
FROM emp
WHERE sal between 15000 and 20000;

12 Find the name of the President.

SELECT ename
FROM emp
WHERE mgr is NULL;

13 Find all the employees whose last names end with S

SELECT ename
FROM emp
WHERE ename LIKE '%S';

14 List the employees whose names have TH or LL in them

SELECT ename
FROM emp
WHERE ename LIKE '%TH%' OR ename LIKE '%LL%';

15 List only those employees who receive commission.

SELECT ename
FROM emp
WHERE COMM != 0;

16 Find the name, job, salary, hiredate, and department number of all employees by alphabetical order of name.

SELECT ename, job, sal, hiredate, deptno
FROM emp
order by ename;
  1. Find the name, job, salary, hiredate and department number of all employees in ascending order by their salaries.
SELECT ename, job, sal, hiredate, deptno
FROM emp
order by sal;
  1. List all salesmen in descending order by commission divided by their salary.
SELECT ename
FROM emp
order by comm/sal desc
  1. Order employees in department 30 who receive commision, in ascending order by commission
SELECT *
FROM emp
WHERE deptno = 30
order by comm;

20 Find the names, jobs, salaries and commissions of all employees who do not have managers.

SELECT ename, job, sal, comm
FROM emp
WHERE mgr is NULL;

21 Find all the salesmen in department 30 who have a salary greater than or equal to £18000.

SELECT ename
FROM emp
WHERE job = 'SALESMAN' AND deptno = 30 AND sal >= 18000;

EXERCISES 3 FUNCTIONS

8 Compute the daily and hourly salary for employees in department 30, round to the nearest penny. Assume there are 22 working days in a month and 8 working hours in a day.

SELECT ename, empno, round(sal/22, 2), round(sal/(22*8), 2)
FROM emp
WHERE deptno = 30;

EXERCISES 4 DATES

3 Which employees were hired in May?

SELECT *
FROM emp
where hiredate like '%5月%';

EXERCISES 5 GROUP BY & HAVING

4 List the average annual salary for all job groups having more than 2 employees in the group.

SELECT AVG(sal * 12)
FROM emp
GROUP BY job
HAVING (COUNT(job) >= 2);
SELECT deptno
FROM emp;

5 Find all departments with an average commission greater than 25% of average salary.

SELECT deptno
FROM emp
GROUP BY deptno
HAVING AVG(sal) > (SELECT avg(sal) FROM emp);

6 Find each department's average annual salary for all its employees except the managers and the president.

SELECT AVG(sal * 12), deptno
FROM emp
where job != 'MANAGER' AND job != 'PRESIDENT'
GROUP BY deptno;

你可能感兴趣的:(test 1)