Академический Документы
Профессиональный Документы
Культура Документы
Divide rows in a table into smaller groups by using the GROUP BY clause.
GROUP BY DEPTNO ;
GROUP BY DEPTNO ;
EMPLOYEES
Addupthe salaries in the EMPLOYEES table for each job, grouped by department.
FROM
EMP
EMPLOYEES
1.Rows are grouped. 2.The group function is applied. 3.Groups matching the HAVING clause are displayed.
SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column, group_function table condition] group_by_expression] group_condition] column];
GROUP BY DEPTNO
HAVING MAX(SAL)>10000 ;
ORDER BY SUM(SAL);
GROUP BY DEPTNO;
Queries
JOB TOTAL ANALYST 6000 CLERK 13000 MANAGER 20000 PRESIDENT 10000 SALESMAN 13500
6000
4000 5000 3000 5000 6000 10000
10000
3500 3000 7000
Subqueries
Subquery Syntax
SELECT FROM WHERE select_list table expr operator (SELECT FROM
select_list table);
The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query).
Using a Subquery
WHERE
SAL >
11000
Enclose subqueries in parentheses. Place subqueries on the right side of the comparison condition. The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis. Use single-row operators with singlerow subqueries and use multiple-row operators with multiple-row subqueries.
Single-Row Subqueries
Return only one row Use single-row comparison Operator Meaning operators
= > >= < <= <> Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
FROM
EMP
800
WHERE SAL =
FROM
EMP
1300
GROUP BY DEPTNO HAVING MIN(SAL) > (SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 30);
ERROR at line 4: ORA-01427: single-row subquery returns more than one row
no rows selected
Multiple-Row Subqueries
Operator
IN
Return more than one row Use multiple-row Meaning comparison operators Equal to any member in the list