Create three empty tables emp1, emp2, emp3, the structure refers to the emp table
CREATE TABLE EMP1 AS SELECT * FROM EMP WHERE 1=2;
CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2;
CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE 1=2;
Use an INSERT statement to insert the department number 10 in the emp table into emp1, 20 into emp2, and 30 into emp3
INSERT INTO EMP1 SELECT * FROM EMP WHERE DEPTNO=10;
INSERT INTO EMP2 SELECT * FROM EMP WHERE DEPTNO=20;
INSERT INTO EMP3 SELECT * FROM EMP WHERE DEPTNO=30;
Create a table t35, x column is any timestamp type, insert the current time, and query
CREATE TABLE T35(X TIMESTAMP WITH LOCAL TIME ZONE);
INSERT INTO T35 VALUES(SYSDATE);
SELECT * FROM T35;
Query the information of all employees in the emp table, whose salary exceeds the average salary of their department
SELECT ENAME,SAL,DEPTNO FROM EMP OUTER_TABLE
WHERE SAL>(SELECT AVG(SAL) FROM EMP INNER_TABLE WHERE INNER_TABLE.DEPTNO=OUTER_TABLE.DEPTNO);
Query the information of several layers of employees who are not leaders in emp
SELECT * FROM EMP OUTER_TABLE WHERE NOT EXISTS (SELECT 'X' FROM EMP INNER_TABLE WHERE INNER_TABLE.MGR = OUTER_TABLE.EMPNO);
Find departments that do not have any employees in the dept table
SELECT DEPTNO,DNAME FROM DEPT WHERE NOT EXISTS(SELECT 'X' FROM EMP WHERE EMP.DEPTNO=DEPT.DEPTNO);
Authorize scott to query the permissions of employees and departments table in hr user
GRANT SELECT ON DEPARTMENTS TO SCOTT;
GRANT SELECT ON EMPLOYEES TO SCOTT;
Create the employees, departments table with the same name in the scott user and copy the data
CREATE TABLE EMPLOYEES AS SELECT * FROM HR.EMPLOYEES;
CREATE TABLE DEPARTMENTS AS SELECT * FROM HR.DEPARTMENTS;
Add a column to the employees table for the scott user, the department name
ALTER TABLE EMPLOYEES ADD(DNAME VARCHAR2(20));
The scott user updates the department name of the employees table according to the department name of the departments table
UPDATE EMPLOYEES SET DNAME=(SELECT DEPARTMENT_NAME FROM DEPARTMENTS WHERE DEPARTMENTS.DEPARTMENT_ID=EMPLOYEES.DEPARTMENT_ID);
SELECT * FROM DEPARTMENTS;
SELECT * FROM EMPLOYEES;
Delete employee information whose location_id is 1700 in scott user employees
DELETE FROM EMPLOYEES WHERE DEPARTMENT_ID IN(SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE LOCATION_ID=1700);