A simple understanding of group statements in Oracle

Source: Internet
Author: User

Oracle has set a good reference for us, that is, the scott user and the employee table. Today, we will start with the emp table to study the group usage.
 

SQL> select * from emp
2;
 
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------------------------------------------------------------
7369 smith clerk 7902 800.00 20
7499 allen salesman 7698 1981-2-20 1600.00 300.00 30
7521 ward salesman 7698 1981-2-22 1250.00 500.00 30
7566 jones manager 7839 1981-4-2 2975.00 20
7654 martin salesman 7698 1250.00 1400.00 30
7698 blake manager 7839 1981-5-1 2850.00 30
7782 clark manager 7839 1981-6-9 2450.00 10
7788 scott analyst 7566 1987-4-19 3000.00 20
7839 king president 1981-11-17 5000.00 10
7844 turner salesman 7698 1500.00 0.00 30
7876 adams clerk 7788 1987-5-23 1100.00 20
7900 james clerk 7698 1981-12-3 950.00 30
7902 ford analyst 7566 1981-12-3 3000.00 20
7934 miller clerk 7782 1982-1-23 1300.00 10
 
14 rows selected
 
Here we will first test the group by function. SQL>
SQL> select deptno from emp;
 
DEPTNO
------
20
30
30
20
30
30
10
20
10
30
20
30
20
10
 
14 rows selected
 
SQL>
SQL>
SQL> select deptno from emp group by deptno;
 
DEPTNO
------
30
20
10
 
SQL>
SQL> select job from emp group by job;
 
JOB
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST
 
SQL> select deptno, job from emp
2 group by deptno, job;
 
DEPTNO JOB
---------------
20 CLERK
30 SALESMAN
20 MANAGER
30 CLERK
10 PRESIDENT
30 MANAGER
10 CLERK
10 MANAGER
20 ANALYST
 
9 rows selected
 
In principle, (1) select detpno from emp; this statement indicates that the table emp is found, but only the column detpno is displayed, the result set contains data that is not enclosed in the table, but only hidden columns. However, after the group by statement is added, that is, (2) elect deptno from emp group by deptno; the table emp is also found, and the column detpno is also listed, after the result is output, it is followed by group by to perform subsequent operations on the 1 type. The operation is to group the result set of the 1 type by deptno, And the grouping result cancels a lot of repeated data. In this way, select job, deptno from emp gorup by job, deptno. This means that the job and deptno columns are first found in emp ., Find 14 rows of data, and then group by job and deptno. That is, match with the binary relationship of {job, deptno. In this way, nine rows of data appear because the two-element relationship is identical. In this way, we can understand that the queried data must be grouped.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.