部门信息:dept
+------+-----------+------------+
|deptno| deptname |deptlocation|
+------+-----------+------------+
| 1 |designing | workin121 |
+------+-----------+------------+
| 2 | coding | workin123 |
+------+-----------+------------+
| 3 | testing | workin125 |
+------+-----------+------------+
| 4 | manager | workin127 |
+------+-----------+------------+
| 5 |temp | nolocation |
+------+-----------+------------+
员工信息:emp
+-----+-------+---------+--------+----------+----------+
|empno|deptno | empname | empjob | empsalary|empmanager|
+-----+-------+---------+--------+----------+----------+
| 1 | 2 | zhang | code | 3000 | 5 |
+-----+-------+---------+--------+----------+----------+
| 2 | 2 | wang | manager| 4000 | 1 |
+-----+-------+---------+--------+----------+----------+
| 3 | 1 | li | code | 3600 | 1 |
+-----+-------+---------+--------+----------+----------+
| 4 | 5 | zhao | test | 2556 | 2 |
+-----+-------+---------+--------+----------+----------+
| 5 | 1 | wu | code | 6500 | 15 |
+-----+-------+---------+--------+----------+----------+
| 6 | 5 | guo | code | 5653 | 3 |
+-----+-------+---------+--------+----------+----------+
| 7 | 1 | wang | study | 1565 | 4 |
+-----+-------+---------+--------+----------+----------+
| 8 | 1 | sun | code | 9999.99 | 15 |
+-----+-------+---------+--------+----------+----------+
| 9 | 2 | feng | study | 1999 | 12 |
+-----+-------+---------+--------+----------+----------+
| 10 | 1 | ma | code | 2343 | 3 |
+-----+-------+---------+--------+----------+----------+
| 11 | 3 | tu | code | 9999.99 | 16 |
+-----+-------+---------+--------+----------+----------+
| 12 | 1 | xin | code | 7900 | 6 |
+-----+-------+---------+--------+----------+----------+
| 13 | 1 | si | code | 4546 | 2 |
+-----+-------+---------+--------+----------+----------+
| 14 | 1 | fu | test | 2464 | 15 |
+-----+-------+---------+--------+----------+----------+
| 15 | 3 | hao | manager| 9999.99 | NULL |
+-----+-------+---------+--------+----------+----------+
| 16 | 4 | liu | test | 3521 | 15 |
+-----+-------+---------+--------+----------+----------+
SQL代码:
+----------------------------------------------------------------+
drop table if exists dept;
drop table if exists emp;
create table dept
(
deptno int not null,
deptname char(20),
deptlocation char(50),
primary key (deptno)
);
create table emp
(
empno int not null,
deptno int,
empname char(20),
empjob char(20),
empsalary float,
empmanager int,
primary key (empno)
);
alter table emp add constraint FK_deptincludeemp foreign key (deptno)
references dept (deptno) on delete restrict on update restrict;
INSERT INTO `dept` VALUES (1,'designing','workin121');
INSERT INTO `dept` VALUES (2,'coding','workin123');
INSERT INTO `dept` VALUES (3,'testing','workin125');
INSERT INTO `dept` VALUES (4,'manager','workin127');
INSERT INTO `dept` VALUES (5,'temp','nolocation');
INSERT INTO `emp` VALUES (1,2,'zhang','code',3000,15);
INSERT INTO `emp` VALUES (2,2,'wang','manager',4000,1);
INSERT INTO `emp` VALUES (3,1,'li','code',3600,1);
INSERT INTO `emp` VALUES (4,5,'zhao','test',2556,2);
INSERT INTO `emp` VALUES (5,1,'wu','code',6500,15);
INSERT INTO `emp` VALUES (6,5,'guo','code',5653,3);
INSERT INTO `emp` VALUES (7,1,'wang','study',1565,4);
INSERT INTO `emp` VALUES (8,1,'sun','code',9999.99,15);
INSERT INTO `emp` VALUES (9,2,'feng','study',1999,12);
INSERT INTO `emp` VALUES (10,1,'ma','code',2343,3);
INSERT INTO `emp` VALUES (11,3,'tu','code',9999.99,16);
INSERT INTO `emp` VALUES (12,1,'xin','code',7900,6);
INSERT INTO `emp` VALUES (13,1,'si','code',4546,2);
INSERT INTO `emp` VALUES (14,1,'fu','test',2464,15);
INSERT INTO `emp` VALUES (15,3,'hao','manager',9999.99,NULL);
INSERT INTO `emp` VALUES (16,4,'liu','test',3521,15);
+----------------------------------------------------------+
问题:
1.列出emp表中各部门的部门号, 部门,最高工资,最低工资
+-------------------------------------------------------------+
|SELECT b.deptno, a.deptname, min( b.empsalary ) AS minsalary,|
|max( b.empsalary ) AS maxsalary |
|FROM emp b, dept a |
|WHERE b.deptno = a.deptno |
|GROUP BY b.deptno |
+-------------------------------------------------------------+
2.列出emp表中各部门job为'code'的员工的最低工资,最高工资
+--------------------------------------------------------------------+
|select a.deptname,min(b.empsalary)as minsal,max(b.empsalary) as maxs|
|from emp b,dept a |
|where empjob='code' and a.deptno=b.deptno |
|group by b.deptno; |
+--------------------------------------------------------------------+
3.对于emp中最低工资小于3000的部门,列出job为'code'的员工的部门号,最低工资,最高工资
+-------------------------------------------------------------------+
|select a.deptno,min(a.empsalary)as minsal,max(a.empsalary)as maxsal|
|from emp a,( |
| select deptno,min(empsalary) as minsal |
| from emp |
| group by deptno |
| having minsal<3000) b |
|where a.deptno=b.deptno and a.empjob='code' |
|group by a.deptno; |
+-------------------------------------------------------------------+
4.根据部门号由高而低,工资由低而高列出每个员工的姓名,部门号,工资
+--------------------------------------+
|select empname, deptno ,empsalary |
|from emp |
|group by deptno desc,empsalary asc; |
+--------------------------------------+
5.列出'zhang'所在部门中每个员工的姓名与部门号
+--------------------------------------------------------------+
|select empname,deptno from emp |
|where deptno = (select deptno from emp where empname='zhang');|
+--------------------------------------------------------------+
6.列出每个员工的姓名,工作,部门号,部门名
+----------------------------------------------+
|select b.empname,b.empjob,b.deptno,a.deptname |
|from dept a,emp b |
|where a.deptno=b.deptno; |
+----------------------------------------------+
7.列出emp中工作为'code'的员工的姓名,工作,部门号,部门名
+----------------------------------------------+
|select b.empname,b.empjob,b.deptno,a.deptname |
|from dept a,emp b |
|where b.empjob='code' and a.deptno=b.deptno |
+----------------------------------------------+
8.对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
+---------------------------------------------------------+
|select a.empname as emp,b.empname as manager |
|from emp a,emp b |
|where a.empmanager is not NULL and a.empmanager=b.empno; |
+---------------------------------------------------------+
9.对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'code'的员工名与工作
+---------------------------------------------------------+
|SELECT dept.deptno,dept.deptname,emp.empname,emp.empjob |
|FROM dept, emp |
|WHERE dept.deptno = emp.deptno AND emp.empjob = 'code'; |
+---------------------------------------------------------+
10.对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
+-----------------------------------------------------+
|SELECT a.deptno, empname, empsalary |
|FROM emp a,( |
| SELECT deptno, avg( empsalary ) AS avgsal |
| FROM emp |
| GROUP BY deptno)b |
|WHERE a.deptno = b.deptno AND a.empsalary > b.avgsal |
|ORDER BY a.deptno |
+-----------------------------------------------------+
11.对于emp,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序
+-----------------------------------------------------+
|SELECT COUNT(a.empno) AS allcount, a.deptno |
|FROM emp AS a,( |
| SELECT c.deptno AS deptno, AVG(c.empsalary)AS avgsal|
| FROM emp AS c |
| GROUP BY c.deptno |
| ) AS b |
|WHERE a.empsalary > b.avgsal AND a.deptno = b.deptno |
|GROUP BY a.deptno ORDER BY a.deptno; |
+-----------------------------------------------------+
12.对于emp中工资高于本部门平均水平,人数多于1人的,列出部门号,人数,按部门号排序
+------------------------------------------------------+
|SELECT a.deptno,count(a.deptno) AS nums FROM emp a,( |
| SELECT avg(empsalary) AS avgsal,deptno |
| FROM emp |
| GROUP BY deptno |
| )b |
|WHERE a.deptno = b.deptno |
|AND a.empsalary > b.avgsal |
|GROUP BY deptno |
|HAVING nums >1 |
|ORDER BY a.deptno; |
+------------------------------------------------------+
13.对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
+---------------------------------------+
|SELECT a.deptno,a.empname,a.empsalary,(|
| SELECT COUNT(b.empname) |
| FROM emp AS b |
| WHERE b.empsalary < a.empsalary |
| ) AS allcount |
|FROM emp AS a |
|WHERE ( |
| SELECT COUNT( b.empname ) |
| FROM emp AS b |
| WHERE b.empsalary < a.empsalary |
| )>5 |
+---------------------------------------+
14.对于emp中同一部门低于自己工资至少3人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
+---------------------------------------+
|SELECT a.deptno,a.empname,a.empsalary,(|
| SELECT COUNT(b.empname) |
| FROM emp AS b |
| WHERE b.empsalary < a.empsalary |
| AND a.deptno = b.deptno |
| ) AS allcount |
|FROM emp AS a |
|WHERE ( |
| SELECT COUNT( b.empname ) |
| FROM emp AS b |
| WHERE b.empsalary < a.empsalary |
| AND a.deptno = b.deptno |
| )>5 |
+---------------------------------------+
分享到:
相关推荐
1. ObjString.dll:这是一个动态链接库(DLL)文件,通常包含一组可重用的函数或对象,可能在这个情况下是提供字符串处理或SQL解析的相关功能。 2. BreakSqlUI.exe:这可能是工具的图形用户界面(GUI)版本,允许...
SQL语句格式整理器是一款非常实用的工具,它专门针对SQL查询语句的排版混乱问题,通过自动化处理,帮助用户快速将杂乱无章的SQL代码转化为整洁、易读的格式。这样的工具在数据库管理和开发过程中尤为重要,因为它...
sqllice是一款专为解决SQL代码格式混乱问题而设计的工具,它能够帮助开发人员快速、高效地整理SQL脚本,使其更符合编程规范,提高代码可读性和团队协作效率。无论你是使用Java、Python、C#还是其他语言进行开发,...
本文将基于"oracle常用sql整理"的主题,深入探讨Oracle SQL的一些核心概念、语句及其实用技巧,适合初级到中级水平的学习者。 一、SQL基础 SQL是标准化的查询语言,分为DDL(Data Definition Language)、DML(Data...
SQL跟踪后,用该工具能够快速整理出整洁的SQL,减少人工的复制、帖贴
自制SQL文整理,非开源,非完整,写得很挫,有使用时限,强烈不建议下载
SQL格式整理工具,而且还可以把SQL语句转换成VB DELPHI C# JAVA里面用的格式
在《SQL》语句整理中,我们涉及了数据库的创建与删除、表的创建与数据操作、查询语句以及聚合函数等核心概念。 首先,创建数据库是数据库管理的基础。在SQL Server 2005中,可以使用`CREATE DATABASE`语句来创建新...
标题中的“SQL数据库整理工具”指的是专门用于管理和优化SQL Server数据库的应用程序,这些工具通常包含一系列功能,旨在提升数据库性能,确保数据的准确性和一致性。SQL Server是Microsoft开发的一款关系型数据库...
根据给定的文件信息,我们可以理解为这是一篇关于SQL语句的文章或教程。尽管描述和部分内容提供的信息有限,但我们可以基于标题“刚刚整理的SQL语句”来展开一些重要的SQL知识点,帮助读者更好地理解和掌握SQL的基础...
sql语句整理 sql增删改查 更新数据记录 删除数据记录
### 一般SQL语句优化整理 #### 概述 在数据库操作中,SQL查询语句是数据检索的核心,其性能直接影响到应用系统的响应时间和资源消耗。对于一般的SQL语句优化,不仅能够提升查询效率,还能改善用户体验。本文将对...
1. SQL基础: - 数据库概念:理解关系型数据库的基本原理,如表、字段、记录、索引等。 - SQL语句类型:DQL(查询语言)、DML(数据操作语言)、DDL(数据定义语言)、DCL(数据控制语言)。 - JOIN操作:INNER ...
DBA日常维护SQL整理,涵盖操作系统、存储、数据库各个层面
- **排序**:`SELECT * FROM table1 ORDER BY field1, field2 [DESC]`按一个或多个字段排序结果,`DESC`表示降序。 - **总数**:`SELECT COUNT(*) AS totalcount FROM table1`计算行数。 - **求和**:`SELECT SUM...
SQL(Structured Query Language)是用于...以上只是SQL知识体系中的一部分,实际的“SQL整理”文件可能还会包含更深入的理论知识、实战技巧、案例分析等内容,对于理解和掌握SQL这一重要的数据库语言有着极大的帮助。
例如,`SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)` 获取本月的第一天,而`SELECT dateadd(day,-1,convert(datetime,convert(varchar(8),dateadd(month,1,getdate()),120)+'01',120))` 获取本月的最后一天。...
1. **数据查询**:SQL的SELECT语句是其最基础也是最重要的部分,允许用户从一个或多个表中提取所需的数据。例如,`SELECT * FROM table_name`将返回表中的所有记录,而`SELECT column1, column2 FROM table_name`则...
SQL Server 2005索引碎片整理