Oracle PL/SQL/Select Query/DISTINCT
Содержание
- 1 Count distinct with group by
- 2 Distinct city and state
- 3 DISTINCT clause can be used with more than one field
- 4 DISTINCT: Eliminating duplication in the result set
- 5 DISTINCT operator inside a group function
- 6 DISTINCT tableName.*
- 7 Eliminating Duplicate Data Using DISTINCT
- 8 Eliminating Duplicate Date type Data Using DISTINCT
- 9 Remember that the DISTINCT operator applies to the entire select list
Count distinct with group by
SQL> create table offerings
2 ( course VARCHAR2(6)
3 , begindate DATE
4 , Coder NUMBER(4)
5 , location VARCHAR2(8)
6 ) ;
Table created.
SQL> insert into offerings values ("SQL",date "2009-04-12",13,"DALLAS" );
1 row created.
SQL> insert into offerings values ("OAU",date "2009-08-10",4,"CHICAGO");
1 row created.
SQL> insert into offerings values ("SQL",date "2009-10-04",1,"SEATTLE");
1 row created.
SQL> insert into offerings values ("SQL",date "2009-12-13",1,"DALLAS" );
1 row created.
SQL> insert into offerings values ("JAV",date "2009-12-13",4,"SEATTLE");
1 row created.
SQL> insert into offerings values ("XML",date "2000-02-03",1,"DALLAS" );
1 row created.
SQL> insert into offerings values ("JAV",date "2000-02-01",11,"DALLAS" );
1 row created.
SQL> insert into offerings values ("PLS",date "2000-09-11",8,"DALLAS" );
1 row created.
SQL> insert into offerings values ("XML",date "2000-09-18",NULL,"SEATTLE");
1 row created.
SQL> insert into offerings values ("OAU",date "2000-09-27",13,"DALLAS" );
1 row created.
SQL> insert into offerings values ("ERM",date "2001-01-15",NULL, NULL );
1 row created.
SQL> insert into offerings values ("PRO",date "2001-02-19",NULL,"DALLAS" );
1 row created.
SQL> insert into offerings values ("RSD",date "2001-02-24",8,"CHICAGO");
1 row created.
SQL>
SQL> select Coder
2 , count(distinct course)
3 , count(*)
4 from offerings
5 group by Coder;
CODER COUNT(DISTINCTCOURSE) COUNT(*)
---------- --------------------- ----------
1 2 3
4 2 2
8 2 2
11 1 1
13 2 2
3 3
6 rows selected.
SQL>
SQL>
SQL> drop table offerings;
Table dropped.
Distinct city and state
SQL> create table emp(
2 emp_no integer primary key,
3 lastname varchar2(20) not null,
4 firstname varchar2(15) not null,
5 midinit varchar2(1),
6 street varchar2(30),
7 city varchar2(20),
8 state varchar2(2),
9 zip varchar2(5),
10 shortZipCode varchar2(4),
11 area_code varchar2(3),
12 phone varchar2(8),
13 salary number(5,2),
14 birthdate date,
15 startDate date,
16 title varchar2(20),
17 dept_no integer ,
18 mgr integer,
19 region number,
20 division number,
21 total_sales number
22 );
Table created.
SQL> -- emp Table Inserts:
SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, birthdate, title)values
2 (1,"Z","Joy","R","1 Ave","New York","NY","12122","2333","212","200-1111","12-nov-1976","President");
1 row created.
SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, salary, birthdate, startDate,title, dept_no, mgr, region, division, total_sales)valu
es
2 (2,"X","Lucy","J","1 Street","New York","NY","43552","6633","212","234-4444",7.75,"21-mar-1976","1-feb-1994","Sales Manager",2,1,100,10,40000);
1 row created.
SQL> insert into emp(emp_no, lastname, firstname, midinit, street, city, state, zip,shortZipCode, area_code, phone, salary, birthdate, startDate,title, dept_no, mgr, region, division, total_sales)valu
es
2 (3,"Y","Jordan","E","1 Drive","New York","NY","76822","8763","212","222-2222",7.75,"14-feb-1963","15-mar-1995","Sales Clerk",2,2,100,10,10000);
1 row created.
SQL>
SQL> select distinct city, state from emp;
CITY ST
-------------------- --
New York NY
1 row selected.
SQL>
SQL> drop table emp;
Table dropped.
DISTINCT clause can be used with more than one field
SQL> CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
2 ENAME VARCHAR2(10),
3 JOB VARCHAR2(9),
4 MGR NUMBER(4),
5 HIREDATE DATE,
6 SAL NUMBER(7, 2),
7 COMM NUMBER(7, 2),
8 DEPTNO NUMBER(2));
SQL>
SQL> INSERT INTO EMP VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
SQL> INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
SQL> INSERT INTO EMP VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
SQL> INSERT INTO EMP VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
SQL> INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
SQL> INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
SQL> INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
SQL> INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
SQL> INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
SQL> INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
SQL> INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
SQL> INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
SQL> INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
SQL> INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
SQL>
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
SQL>
SQL> -- DISTINCT clause can be used with more than one field.
SQL>
SQL> SELECT DISTINCT job, sal FROM emp;
JOB SAL
--------- ----------
CLERK 1300
SALESMAN 1600
SALESMAN 1250
CLERK 950
CLERK 800
MANAGER 2450
PRESIDENT 5000
ANALYST 3000
MANAGER 2850
MANAGER 2975
SALESMAN 1500
JOB SAL
--------- ----------
CLERK 1100
SQL>
SQL> drop table emp;
SQL>
SQL>
DISTINCT: Eliminating duplication in the result set
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> --Eliminating duplication in the result set
SQL>
SQL> SELECT City FROM Employee;
CITY
----------
Toronto
Vancouver
Vancouver
Vancouver
Vancouver
New York
New York
Vancouver
8 rows selected.
SQL>
SQL> -- DISTINCT keyword.
SQL>
SQL> SELECT DISTINCT City FROM Employee;
CITY
----------
New York
Toronto
Vancouver
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
SQL>
DISTINCT operator inside a group function
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> --Using the DISTINCT keyword with NULL
SQL>
SQL> --DISTINCT operator inside a group function.
SQL>
SQL>
SQL> SELECT AVG(DISTINCT Salary) AS WITH_DISTINCT FROM Employee;
WITH_DISTINCT
-------------
4071.7525
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
SQL>
DISTINCT tableName.*
SQL>
SQL> create table emp
2 ( empno NUMBER(4) constraint E_PK primary key
3 , ename VARCHAR2(8)
4 , init VARCHAR2(5)
5 , job VARCHAR2(8)
6 , mgr NUMBER(4)
7 , bdate DATE
8 , sal NUMBER(6,2)
9 , comm NUMBER(6,2)
10 , deptno NUMBER(2) default 10
11 ) ;
Table created.
SQL> insert into emp values(1,"Tom","N", "TRAINER", 13,date "1965-12-17", 800 , NULL, 20);
1 row created.
SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30);
1 row created.
SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30);
1 row created.
SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20);
1 row created.
SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30);
1 row created.
SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30);
1 row created.
SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10);
1 row created.
SQL> insert into emp values(8,"Smart","SCJ", "TRAINER", 4,date "1959-11-26", 3000, NULL, 20);
1 row created.
SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10);
1 row created.
SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30);
1 row created.
SQL> insert into emp values(11,"Ana","AA", "TRAINER", 8,date "1966-12-30", 1100, NULL, 20);
1 row created.
SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30);
1 row created.
SQL> insert into emp values(13,"Fake","MG", "TRAINER", 4,date "1959-02-13", 3000, NULL, 20);
1 row created.
SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10);
1 row created.
SQL> create table offerings
2 ( course VARCHAR2(6)
3 , begindate DATE
4 , trainer NUMBER(4)
5 , location VARCHAR2(8)
6 ) ;
Table created.
SQL> insert into offerings values ("SQL",date "2009-04-12",13,"DALLAS" );
1 row created.
SQL> insert into offerings values ("OAU",date "2009-08-10",4,"CHICAGO");
1 row created.
SQL> insert into offerings values ("SQL",date "2009-10-04",1,"SEATTLE");
1 row created.
SQL> insert into offerings values ("SQL",date "2009-12-13",1,"DALLAS" );
1 row created.
SQL> insert into offerings values ("JAV",date "2009-12-13",4,"SEATTLE");
1 row created.
SQL> insert into offerings values ("XML",date "2000-02-03",1,"DALLAS" );
1 row created.
SQL> insert into offerings values ("JAV",date "2000-02-01",11,"DALLAS" );
1 row created.
SQL> insert into offerings values ("PLS",date "2000-09-11",8,"DALLAS" );
1 row created.
SQL> insert into offerings values ("XML",date "2000-09-18",NULL,"SEATTLE");
1 row created.
SQL> insert into offerings values ("OAU",date "2000-09-27",13,"DALLAS" );
1 row created.
SQL> insert into offerings values ("ERM",date "2001-01-15",NULL, NULL );
1 row created.
SQL> insert into offerings values ("PRO",date "2001-02-19",NULL,"DALLAS" );
1 row created.
SQL> insert into offerings values ("RSD",date "2001-02-24",8,"CHICAGO");
1 row created.
SQL>
SQL> select DISTINCT e.*
2 from emp e
3 join
4 offerings o
5 on e.empno = o.trainer
6 where o.course = "SQL";
EMPNO ENAME INIT JOB MGR BDATE SAL COMM
---------- -------- ----- -------- ---------- ---------- ---------- ----------
DEPTNO
----------
1 Tom N TRAINER 13 17-12-1965 800
20
13 Fake MG 4 13-02-1959 3000
20
SQL>
SQL> drop table offerings;
Table dropped.
SQL> drop table emp;
Table dropped.
SQL>
Eliminating Duplicate Data Using DISTINCT
SQL>
SQL>
SQL> CREATE TABLE emp (
2 empID INT NOT NULL PRIMARY KEY,
3 CourseID INT NOT NULL,
4 ProfessorID INT NOT NULL,
5 SustainedOn DATE,
6 Comments VARCHAR(255));
Table created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (1,1,1,DATE "2003-03-12","Hard");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (2,2,1,DATE "2003-03-13","Simple");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (3,3,2,DATE "2003-03-11","1 hour long");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn) VALUES (4,4,3,DATE "2003-03-18");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (5,5,2,DATE "2003-03-19","2 hours long");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn) VALUES (6,6,3,DATE "2003-03-25");
1 row created.
SQL>
SQL>
SQL> SELECT DISTINCT SustainedOn FROM emp;
SUSTAINED
---------
11-MAR-03
12-MAR-03
13-MAR-03
18-MAR-03
19-MAR-03
25-MAR-03
6 rows selected.
SQL>
SQL>
SQL> drop table emp;
Table dropped.
Eliminating Duplicate Date type Data Using DISTINCT
SQL>
SQL>
SQL> CREATE TABLE emp (
2 empID INT NOT NULL PRIMARY KEY,
3 CourseID INT NOT NULL,
4 ProfessorID INT NOT NULL,
5 SustainedOn DATE,
6 Comments VARCHAR(255));
Table created.
SQL>
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (1,1,1,DATE "2003-03-12","Hard");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (2,2,1,DATE "2003-03-13","Simple");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (3,3,2,DATE "2003-03-11","1 hour long");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn) VALUES (4,4,3,DATE "2003-03-18");
1 row created.
SQL> INSERT INTO emp (empID,CourseID,ProfessorID,SustainedOn,Comments) VALUES (5,5,2,DATE "2003-03-19","2 hours long");
1 row created.
SQL>
SQL> SELECT DISTINCT empID, SustainedOn FROM emp;
EMPID SUSTAINED
---------- ---------
1 12-MAR-03
2 13-MAR-03
3 11-MAR-03
4 18-MAR-03
5 19-MAR-03
5 rows selected.
SQL>
SQL> drop table emp;
Table dropped.
Remember that the DISTINCT operator applies to the entire select list
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2334.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 2334.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 2334.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 2334.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 2334.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 2334.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2334.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 2334.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 2334.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 2334.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> -- Remember that the DISTINCT operator applies to the entire select list.
SQL>
SQL> SELECT DISTINCT City, Description FROM Employee;
CITY DESCRIPTION
---------- ---------------
New York Manager
Vancouver Tester
Toronto Programmer
Vancouver Manager
New York Tester
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
SQL>