标签:
--查询employees表中所有6月入职员工的last_name,hire_date(格式:YYYY-MM-DD),salary(格式:5,000.00)
SELECT LAST_NAME,TO_CHAR(HIRE_DATE,‘YYYY-MM-DD‘) AS 入职时间,TO_CHAR(SALARY,‘FM99,999.00‘) AS 工资
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,‘MM‘)=‘06‘;
SELECT LAST_NAME,TO_CHAR(HIRE_DATE,‘YYYY-MM-DD‘) AS 入职时间,TO_CHAR(SALARY,‘FM99,999.00‘) AS 工资
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE,‘MON‘)=‘6月 ‘;
--SQL:1999
--查询员工的last_name,department_name
SELECT LAST_NAME,DEPARTMENT_NAME
FROM EMPLOYEES , DEPARTMENTS
WHERE EMPLOYEES.DEPARTMENT_ID=DEPARTMENTS.DEPARTMENT_ID;
SELECT EMP.LAST_NAME,EMP.DEPARTMENT_ID,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP INNER JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;
--查询员工的last_name,department_name,city
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME,LOC.CITY
FROM EMPLOYEES EMP JOIN DEPARTMENTS DEPT ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID
JOIN LOCATIONS LOC ON LOC.LOCATION_ID=DEPT.LOCATION_ID;
--外连接:查询出满足连接条件的数据与不满足连接条件的数据。
--左外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP LEFT OUTER JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;
--右外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP RIGHT OUTER JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;
--全外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP FULL OUTER JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID;
--Oracle中特有的外连接:(+)
--左外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP,DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID(+);
--右外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP,DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID(+)=DEPT.DEPARTMENT_ID;
--SQL Server中特有的外连接:*
--左外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP,DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID*=DEPT.DEPARTMENT_ID;
--右外连接
SELECT EMP.LAST_NAME,DEPT.DEPARTMENT_NAME
FROM EMPLOYEES EMP,DEPARTMENTS DEPT
WHERE EMP.DEPARTMENT_ID=*DEPT.DEPARTMENT_ID;
--组函数(聚合函数)
--常用组函数
--AVG():求平均值
SELECT AVG(SALARY)
FROM EMPLOYEES;
--SUM():求总和
SELECT SUM(SALARY)
FROM EMPLOYEES;
--MAX()/MIN():求最大/最小值
SELECT MAX(SALARY),MIN(SALARY)
FROM EMPLOYEES;
SELECT MAX(HIRE_DATE),MIN(HIRE_DATE)
FROM EMPLOYEES;
SELECT MAX(LAST_NAME),MIN(LAST_NAME)
FROM EMPLOYEES;
--COUNT():
SELECT COUNT(*)--返回查询结果的行数
FROM EMPLOYEES;
SELECT COUNT(DEPARTMENT_ID)--返回指定列中不为NULL的值的个数
FROM EMPLOYEES;
--组函数与DISTINCT关键字
SELECT COUNT(DISTINCT JOB_ID),COUNT(DISTINCT DEPARTMENT_ID)
FROM EMPLOYEES;
--组函数与NULL:组函数会自动忽略NULL。
SELECT AVG(NVL(COMMISSION_PCT,0))
FROM EMPLOYEES;
--GROUP BY子句:分组,统计,报表。
SELECT 4
FROM 1
[WHERE] 2
[GROUP BY] 3
[ORDER BY] 5
--查询employees表中每个job_id的人数,显示job_id,人数
SELECT JOB_ID,COUNT(*)
FROM EMPLOYEES
GROUP BY JOB_ID;
--当使用GROUP BY子句时,SELECT子句中非组函数的列必须放在GROUP BY子句中进行分组。
--查询employees表中每个部门的人数,显示department_id与人数,结果根据department_id升序排序。
SELECT DEPARTMENT_ID,COUNT(*)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
ORDER BY DEPARTMENT_ID ASC;
--查询每个部门的人数,显示department_name与人数,结果根据department_name升序排序。
SELECT DEPT.DEPARTMENT_NAME,COUNT(*)
FROM EMPLOYEES EMP JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID
GROUP BY DEPT.DEPARTMENT_NAME
ORDER BY DEPT.DEPARTMENT_NAME ASC;
SELECT DEPT.DEPARTMENT_NAME,COUNT(EMP.EMPLOYEE_ID)
FROM EMPLOYEES EMP RIGHT JOIN DEPARTMENTS DEPT
ON EMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID
GROUP BY DEPT.DEPARTMENT_NAME
ORDER BY DEPT.DEPARTMENT_NAME ASC;
SELECT COUNT(*)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;--GROUP BY子句中出现的列可以不在SELECT子句中出现。
--查询每个部门中每个职位的人数。显示department_id,job_id,人数
SELECT DEPARTMENT_ID,JOB_ID,COUNT(*)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID,JOB_ID
ORDER BY DEPARTMENT_ID ASC;
--HAVING子句:过滤分组的结果。使用HAVING子句时必须使用GROUP BY子句。
SELECT 5
FROM 1
[WHERE] 2
[GROUP BY] 3
[HAVING] 4
[ORDER BY] 6
--查询平均工资大于10000的department_id及平均工资,结果根据工资升序排序。
SELECT DEPARTMENT_ID,AVG(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING AVG(SALARY)>10000
ORDER BY AVG(SALARY) ASC;
SELECT DISTINCT JOB_ID
FROM EMPLOYEES
ORDER BY SALARY ASC;
--当使用GROUP BY子句或DISTINCT关键字时,ORDER BY子句中排序的列必须在SELECT子句中出现。
--WHERE子句不能使用组函数作为过滤条件
--HAVING子句可以使用组函数作为过滤条件
--查询人数在3人及3人以上的部门编号及人数。
SELECT DEPARTMENT_ID,COUNT(*)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(*)>=3;
--当条件即可以放在WHERE子句中,也可以放在HAVING子句中时,建议放在WHERE子句中,可以提高查询效率。
--查询最低工资大于6000,并且job_id中不包含‘REP‘的job_id与最低工资。
SELECT JOB_ID,MIN(SALARY)
FROM EMPLOYEES
WHERE JOB_ID NOT LIKE ‘%REP%‘
GROUP BY JOB_ID
HAVING MIN(SALARY)>6000 ;
--组函数嵌套
SELECT MAX(AVG(SALARY)),MIN(AVG(SALARY)),COUNT(*)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;
/*
1.组函数嵌套时必须使用GROUP BY子句。
2.组函数嵌套时,SELECT子句中只能出现组函数嵌套的列(COUNT除外)。
3.组函数嵌套只能直接出现在SELECT子句中。
*/
--统计每年入职的人数,显示年与人数,结果根据年升序排序。
SELECT TO_CHAR(HIRE_DATE,‘YYYY‘) AS 年,COUNT(*)
FROM EMPLOYEES
GROUP BY TO_CHAR(HIRE_DATE,‘YYYY‘)
ORDER BY TO_CHAR(HIRE_DATE,‘YYYY‘) ASC;
标签:
原文地址:http://www.cnblogs.com/yzyqqhr/p/5726998.html