当前位置:网站首页>Some basic database operations (providing the original database information)
Some basic database operations (providing the original database information)
2022-06-24 14:17:00 【Falling down】
Database creation file used
Create method :cmd The window opens mysql after source:sql File path Create database
create database if not exists db1 default charset utf8;
USE db1;
-- Departmental table
CREATE TABLE DEPT(
DEPTNO INT PRIMARY KEY,
DNAME VARCHAR(14), -- Department name
LOC VARCHAR(13)-- Department Address
)CHARSET=UTF8,ENGINE=INNODB;
INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');
-- The employee table
CREATE TABLE EMP(
EMPNO INT PRIMARY KEY, -- Employee number
ENAME VARCHAR(10), -- Employee name
JOB VARCHAR(9), -- Staff work
MGR INT, -- Employee's direct leader No
HIREDATE DATE, -- Entry time
SAL DOUBLE, -- Wages
COMM DOUBLE, -- Bonus
DEPTNO INT -- Department
); -- relation dept surface
INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7566,"1980-12-17",800,NULL,20);
INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,2000,20);
INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES(7844,'TURNERS','SALESMAN',7698,'1981-09-08',1500,50,30);
INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7566,'1987-07-13',1100,NULL,20);
INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',1250,NULL,30);
INSERT INTO EMP VALUES(7902,'FORD','CLERK',7566,'1981-12-03',3000,NULL,20);
INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10);
-- Pay scale
CREATE TABLE SALGRADE(
GRADE INT,-- Grade
LOSAL DOUBLE, -- minimum wage
HISAL DOUBLE
); -- Maximum wage
INSERT INTO SALGRADE VALUES (1,500,1000);
INSERT INTO SALGRADE VALUES (2,1001,1500);
INSERT INTO SALGRADE VALUES (3,1501,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
All the query statements practiced
-- Find in Department 30 Employee details
SELECT*
FROM emp
WHERE DEPTNO=30
-- Find out the number of the employee who works as an employee , full name , Department number
SELECT EMPNO Number ,ENAME full name ,DEPTNO Department number
FROM emp
WHERE JOB='CLERK'
-- Retrieve the information of employees whose bonus is more than the basic wage
SELECT*
FROM emp
WHERE COMM>SAL
-- Search bonus is more than% of basic salary 60 Employee information
SELECT *
FROM emp
WHERE COMM>SAL*0.6;
-- Find out if the name contains A Employee information
-- % wildcard , Express 0~n Any character
SELECT *
FROM emp
WHERE ENAME like '%A%';
-- Find out the name to a,b,s Employee information at the beginning
-- wildcard ^ Means to calculate from the beginning
-- [] The wildcard character indicates that [] Any character in
SELECT*
FROM emp
WHERE ENAME REGEXP '^[abs]';
-- Find the name with length 7 Employee information in characters
-- LENGTH(str) Function to get the length of the string
SELECT *
FROM emp
WHERE LENGTH(ENAME)=7;
-- The name does not contain R Character employee information
SELECT*
FROM emp
WHERE ENAME NOT LIKE '%R%';
-- Return employee details and sort them in ascending order by name
-- keyword ORDER BY, among ASC Ascending ,DESC Descending
SELECT *
FROM emp
ORDER BY ENAME ASC;
-- Return employee information in descending order by name , Wages are in ascending order
-- The sort constraints used first are placed first
SELECT *
FROM emp
ORDER BY ENAME DESC,SAL+COMM ASC;
-- Calculate the employee's daily salary ( Press 30 God )
SELECT SAL/30 per diem
FROM emp;
-- Find out the jobs of the employees who get the bonus
SELECT JOB
FROM emp
WHERE COMM IS NOT NULL
GROUP BY JOB;
-- Find out if the bonus is less than 100 Or information about employees who don't get bonuses .
-- NULL Unavailable < Judge , application IS NULL
SELECT *
FROM emp
WHERE COMM<100 or COMM is NULL;
-- find 10 The manager of the Department 、20 The staff of the Department Employee information .
SELECT*
FROM emp
WHERE DEPTNO=10 AND JOB='MANAGER' OR DEPTNO=20 AND JOB='CLERK';
-- find 10 The manager of the Department 、20 The staff of the Department Or it's not a manager or an employee, but the salary is higher than 2000 Employee information for .
SELECT*
FROM emp
WHERE DEPTNO=10 AND JOB='MANAGER' OR DEPTNO=20 AND JOB='CLERK' OR SAL>2000;
-- Return the department number and the minimum wage of the Department .
SELECT DEPTNO as department ,MIN(SAL) as minimum wage
FROM emp
GROUP BY DEPTNO;
-- Check the employee's name and annual salary , And sort by annual salary in descending order
SELECT ENAME Employee name ,MGR Annual salary
FROM emp
ORDER BY MGR DESC
-- Return to the employee's job and the minimum wage for the job .
SELECT MIN(SAL) minimum wage ,JOB
FROM emp
GROUP BY JOB;
-- Find and SCOTT Information about employees engaged in the same work
SELECT*
FROM emp
WHERE JOB=(
SELECT JOB
FROM emp
WHERE ENAME='SCOTT'
);
-- Pay more than JAMES Employee information
SELECT *
FROM emp
WHERE SAL>(
SELECT SAL
FROM emp
WHERE ENAME='JAMES'
);
-- Return the employee information whose salary is greater than the average salary
SELECT*
FROM emp
WHERE SAL>(
SELECT AVG(SAL)
FROM emp
);
-- Return the names of all employees in the sales department
SELECT ENAME
FROM emp
WHERE DEPTNO=(
SELECT DEPTNO
FROM dept
WHERE DNAME='SALES'
);
-- Return pay is higher than 30 Information about all employees in the Department .
SELECT*
FROM emp
WHERE SAL>(
SELECT AVG(SAL)
FROM emp
WHERE DEPTNO=30
);
-- Return the name of the Department that owns the employee 、 Department number .
-- Display inner connection
SELECT dept.DNAME Department name ,emp.DEPTNO
FROM emp
INNER JOIN dept on emp.DEPTNO=dept.DEPTNO;
-- Implicit inner join
SELECT dept.DNAME,emp.DEPTNO
FROM emp,dept
WHERE emp.DEPTNO=dept.DEPTNO;
-- Return the name of the employee , Department and department name
SELECT emp.ENAME,dept.DEPTNO,dept.DNAME
FROM emp,dept
WHERE emp.DEPTNO=dept.DEPTNO;
-- Return the name of the employee and the Department
SELECT ENAME,dept.DNAME
FROM emp,dept
WHERE emp.DEPTNO=dept.DEPTNO and emp.JOB='CLERK'
-- Return department number , Department name , The location of the Department and the total number of employees in each department
SELECT d1.DEPTNO,d1.DNAME,d1.LOC,e. Total number of employees in the Department
FROM dept d1,(
SELECT DEPTNO,count(*) Total number of employees in the Department
FROM emp
GROUP BY DEPTNO
)e
WHERE d1.DEPTNO=e.DEPTNO;
-- Return the names of the employee or salesperson and the manager
SELECT e1.ENAME staff ,e2.` The manager ` My manager
FROM emp e1,(
SELECT DEPTNO,ENAME The manager
FROM emp
WHERE JOB='MANAGER'
)e2
WHERE e1.DEPTNO=e2.DEPTNO AND e1.JOB!='MANAGER';
-- Return the names of employees and managers whose entry date is earlier than their manager's entry date
SELECT e1.ENAME staff ,e2.ENAME The manager
FROM emp e1,emp e2
WHERE e1.DEPTNO=e2.DEPTNO AND e2.JOB='MANAGER' and e1.HIREDATE<e2.HIREDATE;
-- Return the employee information of the maximum wage and the minimum wage
SELECT e1.EMPNO,e1.ENAME,e1.JOB,e1.MGR,e1.HIREDATE,e1.SAL,e1.COMM,e1.DEPTNO
FROM emp e1,(
SELECT MAX(SAL) Maximum wage ,MIN(SAL) minimum wage
FROM emp
)e2
WHERE e1.SAL=e2.` minimum wage ` OR e1.SAL=e2.` Maximum wage `;
-- Return the name and salary of the employee whose salary is in the fourth level
SELECT ENAME,SAL
FROM salgrade s1,emp e1
WHERE e1.SAL>s1.LOSAL and e1.SAL<s1.HISAL and s1.GRADE=4;
-- scene : Query the average salary is higher than 2000 Set to name and average salary
SELECT JOB Job title ,AVG(SAL) Average wage
FROM emp
GROUP BY Job title
HAVING Average wage >2000
/** from The subsequent sub query is usually a multi row and multi column table Multi row and multi column subqueries are usually placed in from after where Before Pseudotables must define aliases , Otherwise, the query cannot be executed There is usually no need to write having, Because it consumes a lot of memory , Instead, use subqueries , take having The following conditions are placed in the from Back Father inquiry SELECT The following column query actually queries the column alias of the sub query That is, through FRom The following statement generates a temporary table , Then look up the data in this temporary table **/
SELECT Job title , Average wage
FROM(
SELECT JOB Job title ,AVG(SAL) Average wage
FROM emp
GROUP BY Job title
)e
WHERE Average wage >2000;
-- Find the department number , Department name , Department Address , Number of departments , Department average wage
SELECT d.DEPTNO Department number ,d.DNAME Department name ,d.LOC Department Address ,e. Number of departments ,e.` Average wage `
FROM dept d,(
SELECT DEPTNO,count(*) Number of departments ,AVG(SAL) Average wage
FROM emp
GROUP BY DEPTNO
)e
WHERE d.DEPTNO=e.DEPTNO;
-- Check out all who work in the sales department Employee label , full name , Basic salary , Bonus , Position , Date of entry Departmental maximum and minimum wages
SELECT e1.empno,e1.ename,e1.sal,e1.comm,e1.job,e1.HIREDATE,e2.` Departmental minimum wage `,e2.` The highest salary in the Department `
FROM emp e1,(
SELECT DEPTNO,max(SAL) The highest salary in the Department ,min(SAL) Departmental minimum wage
FROM emp
GROUP BY DEPTNO
)e2
WHERE e1.DEPTNO=e2.DEPTNO and e1.DEPTNO=(
SELECT DEPTNO
FROM dept
WHERE dname='SALES'
)
边栏推荐
- 鲲鹏arm服务器编译安装PaddlePaddle
- The project manager needs to look at six characteristics to build a team
- 远程办公之:在家露营办公小工具| 社区征文
- 怎样评价国产报表工具和BI软件
- 港股上市公司公告 API 数据接口
- 业务与技术双向结合构建银行数据安全管理体系
- 融云通信“三板斧”,“砍”到了银行的心坎上
- ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
- PM should also learn to reflect every day
- js去除字符串空格
猜你喜欢

AntD checkbox,限制选中数量

**Puzzling little problem in unity - light and sky box

【深度学习】NCHW、NHWC和CHWN格式数据的存储形式

Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure

卷积核、特征图可视化

Harmony os. (2)

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire

融云通信“三板斧”,“砍”到了银行的心坎上
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]

【比特熊故事汇】6月MVP英雄故事|技术实践碰撞境界思维
随机推荐
融云通信“三板斧”,“砍”到了银行的心坎上
leetcode 139. Word Break 单词拆分(中等)
Method of inputting dots under letters in markdown/latex
R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
[untitled]
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
AntD checkbox,限制选中数量
js去除字符串空格
MES在流程和离散制造企业的15个差别(下)
厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
HarmonyOS.2
**Puzzling little problem in unity - light and sky box
Jericho After sleep, the system will wake up regularly and continue to run without resetting [chapter]
Solution of channel management system for food and beverage industry: realize channel digital marketing layout
Common singleton mode & simple factory
一文搞定 UDP 和 TCP 高频面试题!
专精特新“小巨人”再启动,“企业上云”数字赋能
取消冒泡
数据库一些基本操作(提供了原数据库信息)
#21Set经典案例