Oracle PL/SQL/Table Joins/Table Join Column
Содержание
- 1 3-Way join
- 2 Join and GROUP BY
- 3 Join columns with full table name reference
- 4 Join three table to find out which employee sold that gift
- 5 Join two tables: column is not mentioned in the SELECT list
- 6 Join using and join on
- 7 Join using clause
- 8 Join with (+)
- 9 Use column name alias during table join
- 10 Use in operator with table join
3-Way join
SQL>
SQL>
SQL> create table ord(
2 order_no integer primary key
3 ,cust_no integer
4 ,order_date date not null
5 ,total_order_price number(7,2)
6 ,deliver_date date
7 ,deliver_time varchar2(7)
8 ,payment_method varchar2(2)
9 ,emp_no number(3,0)
10 ,deliver_name varchar2(35)
11 ,gift_message varchar2(100)
12 );
Table created.
SQL>
SQL>
SQL> insert into ord(order_no,cust_no,order_date,total_order_price,deliver_date,deliver_time,payment_method,emp_no,deliver_name,gift_message)
2 values(1,1,"14-Feb-2002", 23.00, "14-Feb-2002", "12 noon", "CA",1, null, "Gift for wife");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time ,payment_method ,emp_no,deliver_name ,gift_message )
2 values(2,1,"14-Feb-2003", 510.98, "14-feb-2003", "5 pm", "NY",7, "Rose Ted", "Happy Valentines Day to Mother");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(3, 2,"14-Feb-2004", 315.99, "14-feb-2004", "3 pm", "VS",2, "Ani Forest", "Happy Valentines Day to Father");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(4, 2,"14-Feb-1999", 191.95, "14-feb-1999", "2 pm", "NJ",2, "O. John", "Happy Valentines Day");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(5, 6,"4-mar-2002", 101.95, "5-mar-2002", "2:30 pm", "MO" , 2, "Cora", "Happy Birthday from John");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(6, 9,"7-apr-2003", 221.95, "7-apr-2003", "3 pm", "MA", 2, "Sake Keith", "Happy Birthday from Joe" );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(7, 9,"20-jun-2004", 315.95, "21-jun-2004", "12 noon", "BC", 2, "Jessica Li", "Happy Birthday from Jessica");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (8, 12, "31-dec-1999", 135.95, "1-jan-2000", "12 noon", "DI", 3, "Larry", "Happy New Year from Lawrence");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (9, 12, "26-dec-2003", 715.95, "2-jan-2004", "12 noon", "SK",7, "Did", "Happy Birthday from Nancy" );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(10, 4, sysdate-1, 119.95, sysdate+2, "6:30 pm", "VG",2, "P. Jing", "Happy Valentines Day to Jason");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(11, 2, sysdate, 310.00, sysdate+2, "3:30 pm", "DC",2, "C. Late", "Happy Birthday Day to Jack");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(12, 7, sysdate-3, 121.95, sysdate-2, "1:30 pm", "AC",2, "W. Last", "Happy Birthday Day to You");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(13, 7, sysdate, 211.95, sysdate-4, "4:30 pm", "CA",2, "J. Bond", "Thanks for hard working");
1 row created.
SQL>
SQL>
SQL>
SQL> create table department(
2 dept_no integer primary key
3 ,dept_name varchar(20) not null
4 ,mgr_no integer
5 );
Table created.
SQL>
SQL> insert into department(dept_no, dept_name, mgr_no)values(1, "Design", 1);
1 row created.
SQL> insert into department(dept_no, dept_name, mgr_no)values(2, "Sales", 1);
1 row created.
SQL> insert into department(dept_no, dept_name, mgr_no)values(3, "Development", 1);
1 row created.
SQL>
SQL> create table employee(
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 ,zip_4 varchar2(4)
11 ,area_code varchar2(3)
12 ,phone varchar2(8)
13 ,salary number(3)
14 ,birthdate date
15 ,hiredate 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> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (1,"Anderson","Nancy","N","33 Ave","London","NY","11111","1111","212","234-1111",3.75,"21-mar-1927","1-feb-1947","Sales Manager",2,null,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (2,"Last","First","F","12 Ave","Paris","CA","22222","2222","111","867-2222",7.75,"14-feb-1976","15-mar-1985","Sales Clerk",2,1,100,10,10000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (3,"Wash","Georgia","G","1 Street14","Barton","NJ","33333","3333","214","340-3333",11.50,"2-jul-1977","21-apr-2004","Designer",1,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (4,"Bush","Dave","D","56 Street","Island","RI","44444","4444","215","777-4444",21.65,"15-may-1945","2-aug-1975","Designer",1,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (5,"Will","Robin","W","56 Street","Island","MA","55555","5555","216","777-5555",24.65,"10-dec-1980","2-aug-2007","Designer",1,5,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (6,"Pete","Mona","M","13 Ave","York","MO","66666","6666","217","111-6666",9,"14-feb-1966","15-mar-1985","Sales Clerk",2,5,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (7,"Roke","John","J","67 Ave","New York","BC","77777","7777","218","122-7777",10.00,"14-jun-1955","15-mar-1975","Accountant",3,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (8,"Horry","Tedi","T","1236 Lane","Newton","NY","88888","8888","219","222-8888",13.00,"10-jun-1955","15-aug-1985","Sales Representative",3,2,100,10,50000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (9,"Bar","Candi","C","400 East Street","Yorken","NY","99999","9999","220","321-9999",12.00,"10-oct-1933","15-jan-1969","Sales Representative",3,5,100,10,35000);
1 row created.
SQL>
SQL> select * from ord;
ORDER_NO CUST_NO ORDER_DATE TOTAL_ORDER_PRICE
---------- ---------- -------------------- -----------------
DELIVER_DATE DELIVER PA EMP_NO
-------------------- ------- -- ----------
DELIVER_NAME
-----------------------------------
GIFT_MESSAGE
------------------------------------------------------------------------
1 1 14-FEB-2002 00:00:00 23
14-FEB-2002 00:00:00 12 noon CA 1
Gift for wife
2 1 14-FEB-2003 00:00:00 510.98
14-FEB-2003 00:00:00 5 pm NY 7
Rose Ted
Happy Valentines Day to Mother
3 2 14-FEB-2004 00:00:00 315.99
14-FEB-2004 00:00:00 3 pm VS 2
Ani Forest
Happy Valentines Day to Father
4 2 14-FEB-1999 00:00:00 191.95
14-FEB-1999 00:00:00 2 pm NJ 2
O. John
Happy Valentines Day
5 6 04-MAR-2002 00:00:00 101.95
05-MAR-2002 00:00:00 2:30 pm MO 2
Cora
Happy Birthday from John
6 9 07-APR-2003 00:00:00 221.95
07-APR-2003 00:00:00 3 pm MA 2
Sake Keith
Happy Birthday from Joe
7 9 20-JUN-2004 00:00:00 315.95
21-JUN-2004 00:00:00 12 noon BC 2
Jessica Li
Happy Birthday from Jessica
8 12 31-DEC-1999 00:00:00 135.95
01-JAN-2000 00:00:00 12 noon DI 3
Larry
Happy New Year from Lawrence
9 12 26-DEC-2003 00:00:00 715.95
02-JAN-2004 00:00:00 12 noon SK 7
Did
Happy Birthday from Nancy
10 4 15-JUN-2008 17:39:15 119.95
18-JUN-2008 17:39:15 6:30 pm VG 2
P. Jing
Happy Valentines Day to Jason
11 2 16-JUN-2008 17:39:16 310
18-JUN-2008 17:39:16 3:30 pm DC 2
C. Late
Happy Birthday Day to Jack
12 7 13-JUN-2008 17:39:17 121.95
14-JUN-2008 17:39:17 1:30 pm AC 2
W. Last
Happy Birthday Day to You
13 7 16-JUN-2008 17:39:17 211.95
12-JUN-2008 17:39:17 4:30 pm CA 2
J. Bond
Thanks for hard working
13 rows selected.
SQL> select * from employee;
EMP_NO LASTNAME FIRSTNAME M
---------- -------------------- --------------- -
STREET CITY ST ZIP ZIP_ ARE
------------------------------ -------------------- -- ----- ---- ---
PHONE SALARY BIRTHDATE HIREDATE
-------- ---------- -------------------- --------------------
TITLE DEPT_NO MGR REGION DIVISION
-------------------- ---------- ---------- ---------- ----------
TOTAL_SALES
-----------
1 Anderson Nancy N
33 Ave London NY 11111 1111 212
234-1111 4 21-MAR-1927 00:00:00 01-FEB-1947 00:00:00
Sales Manager 2 100 10
40000
2 Last First F
12 Ave Paris CA 22222 2222 111
867-2222 8 14-FEB-1976 00:00:00 15-MAR-1985 00:00:00
Sales Clerk 2 1 100 10
10000
3 Wash Georgia G
1 Street14 Barton NJ 33333 3333 214
340-3333 12 02-JUL-1977 00:00:00 21-APR-2004 00:00:00
Designer 1 2 100 10
40000
4 Bush Dave D
56 Street Island RI 44444 4444 215
777-4444 22 15-MAY-1945 00:00:00 02-AUG-1975 00:00:00
Designer 1 2 100 10
40000
5 Will Robin W
56 Street Island MA 55555 5555 216
777-5555 25 10-DEC-1980 00:00:00 02-AUG-2007 00:00:00
Designer 1 5 100 10
40000
6 Pete Mona M
13 Ave York MO 66666 6666 217
111-6666 9 14-FEB-1966 00:00:00 15-MAR-1985 00:00:00
Sales Clerk 2 5 100 10
40000
7 Roke John J
67 Ave New York BC 77777 7777 218
122-7777 10 14-JUN-1955 00:00:00 15-MAR-1975 00:00:00
Accountant 3 2 100 10
40000
8 Horry Tedi T
1236 Lane Newton NY 88888 8888 219
222-8888 13 10-JUN-1955 00:00:00 15-AUG-1985 00:00:00
Sales Representative 3 2 100 10
50000
9 Bar Candi C
400 East Street Yorken NY 99999 9999 220
321-9999 12 10-OCT-1933 00:00:00 15-JAN-1969 00:00:00
Sales Representative 3 5 100 10
35000
9 rows selected.
SQL> select * from department;
DEPT_NO DEPT_NAME MGR_NO
---------- -------------------- ----------
1 Design 1
2 Sales 1
3 Development 1
3 rows selected.
SQL>
SQL> select d.dept_name, e.lastname, o.order_date
2 from department d, employee e, ord o
3 where d.dept_no = e.dept_no
4 and e.emp_no = o.emp_no
5 /
DEPT_NAME LASTNAME ORDER_DATE
-------------------- -------------------- --------------------
Sales Anderson 14-FEB-2002 00:00:00
Development Roke 14-FEB-2003 00:00:00
Sales Last 14-FEB-2004 00:00:00
Sales Last 14-FEB-1999 00:00:00
Sales Last 04-MAR-2002 00:00:00
Sales Last 07-APR-2003 00:00:00
Sales Last 20-JUN-2004 00:00:00
Design Wash 31-DEC-1999 00:00:00
Development Roke 26-DEC-2003 00:00:00
Sales Last 15-JUN-2008 17:39:15
Sales Last 16-JUN-2008 17:39:16
Sales Last 13-JUN-2008 17:39:17
Sales Last 16-JUN-2008 17:39:17
13 rows selected.
SQL>
SQL> drop table ord;
Table dropped.
SQL> drop table employee;
Table dropped.
SQL> drop table department;
Table dropped.
SQL>
SQL> --
Join and GROUP BY
SQL>
SQL> create table ord(
2 order_no integer primary key
3 ,cust_no integer
4 ,order_date date not null
5 ,total_order_price number(7,2)
6 ,deliver_date date
7 ,deliver_time varchar2(7)
8 ,payment_method varchar2(2)
9 ,emp_no number(3,0)
10 ,deliver_name varchar2(35)
11 ,gift_message varchar2(100)
12 );
Table created.
SQL>
SQL>
SQL> insert into ord(order_no,cust_no,order_date,total_order_price,deliver_date,deliver_time,payment_method,emp_no,deliver_name,gift_message)
2 values(1,1,"14-Feb-2002", 23.00, "14-Feb-2002", "12 noon", "CA",1, null, "Gift for wife");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time ,payment_method ,emp_no,deliver_name ,gift_message )
2 values(2,1,"14-Feb-2003", 510.98, "14-feb-2003", "5 pm", "NY",7, "Rose Ted", "Happy Valentines Day to Mother");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(3, 2,"14-Feb-2004", 315.99, "14-feb-2004", "3 pm", "VS",2, "Ani Forest", "Happy Valentines Day to Father");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(4, 2,"14-Feb-1999", 191.95, "14-feb-1999", "2 pm", "NJ",2, "O. John", "Happy Valentines Day");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(5, 6,"4-mar-2002", 101.95, "5-mar-2002", "2:30 pm", "MO" , 2, "Cora", "Happy Birthday from John");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(6, 9,"7-apr-2003", 221.95, "7-apr-2003", "3 pm", "MA", 2, "Sake Keith", "Happy Birthday from Joe" );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(7, 9,"20-jun-2004", 315.95, "21-jun-2004", "12 noon", "BC", 2, "Jessica Li", "Happy Birthday from Jessica");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (8, 12, "31-dec-1999", 135.95, "1-jan-2000", "12 noon", "DI", 3, "Larry", "Happy New Year from Lawrence");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values (9, 12, "26-dec-2003", 715.95, "2-jan-2004", "12 noon", "SK",7, "Did", "Happy Birthday from Nancy" );
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(10, 4, sysdate-1, 119.95, sysdate+2, "6:30 pm", "VG",2, "P. Jing", "Happy Valentines Day to Jason");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message )
2 values(11, 2, sysdate, 310.00, sysdate+2, "3:30 pm", "DC",2, "C. Late", "Happy Birthday Day to Jack");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(12, 7, sysdate-3, 121.95, sysdate-2, "1:30 pm", "AC",2, "W. Last", "Happy Birthday Day to You");
1 row created.
SQL> insert into ord(order_no ,cust_no ,order_date ,total_order_price ,deliver_date ,deliver_time,payment_method ,emp_no,deliver_name ,gift_message)
2 values(13, 7, sysdate, 211.95, sysdate-4, "4:30 pm", "CA",2, "J. Bond", "Thanks for hard working");
1 row created.
SQL>
SQL>
SQL> create table department(
2 dept_no integer primary key
3 ,dept_name varchar(20) not null
4 ,mgr_no integer
5 );
Table created.
SQL>
SQL> insert into department(dept_no, dept_name, mgr_no)values(1, "Design", 1);
1 row created.
SQL> insert into department(dept_no, dept_name, mgr_no)values(2, "Sales", 1);
1 row created.
SQL> insert into department(dept_no, dept_name, mgr_no)values(3, "Development", 1);
1 row created.
SQL>
SQL> create table employee(
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 ,zip_4 varchar2(4)
11 ,area_code varchar2(3)
12 ,phone varchar2(8)
13 ,salary number(3)
14 ,birthdate date
15 ,hiredate 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> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (1,"Anderson","Nancy","N","33 Ave","London","NY","11111","1111","212","234-1111",3.75,"21-mar-1927","1-feb-1947","Sales Manager",2,null,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (2,"Last","First","F","12 Ave","Paris","CA","22222","2222","111","867-2222",7.75,"14-feb-1976","15-mar-1985","Sales Clerk",2,1,100,10,10000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (3,"Wash","Georgia","G","1 Street14","Barton","NJ","33333","3333","214","340-3333",11.50,"2-jul-1977","21-apr-2004","Designer",1,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (4,"Bush","Dave","D","56 Street","Island","RI","44444","4444","215","777-4444",21.65,"15-may-1945","2-aug-1975","Designer",1,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (5,"Will","Robin","W","56 Street","Island","MA","55555","5555","216","777-5555",24.65,"10-dec-1980","2-aug-2007","Designer",1,5,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (6,"Pete","Mona","M","13 Ave","York","MO","66666","6666","217","111-6666",9,"14-feb-1966","15-mar-1985","Sales Clerk",2,5,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (7,"Roke","John","J","67 Ave","New York","BC","77777","7777","218","122-7777",10.00,"14-jun-1955","15-mar-1975","Accountant",3,2,100,10,40000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (8,"Horry","Tedi","T","1236 Lane","Newton","NY","88888","8888","219","222-8888",13.00,"10-jun-1955","15-aug-1985","Sales Representative",3,2,100,10,50000);
1 row created.
SQL> insert into employee(emp_no, lastname, firstname, midinit, street, city, state, zip,zip_4, area_code, phone, salary, birthdate, hiredate,title, dept_no, mgr, region, division, total_sales)
2 values (9,"Bar","Candi","C","400 East Street","Yorken","NY","99999","9999","220","321-9999",12.00,"10-oct-1933","15-jan-1969","Sales Representative",3,5,100,10,35000);
1 row created.
SQL>
SQL>
SQL> select * from ord;
ORDER_NO CUST_NO ORDER_DATE TOTAL_ORDER_PRICE
---------- ---------- -------------------- -----------------
DELIVER_DATE DELIVER PA EMP_NO
-------------------- ------- -- ----------
DELIVER_NAME
-----------------------------------
GIFT_MESSAGE
------------------------------------------------------------------------
1 1 14-FEB-2002 00:00:00 23
14-FEB-2002 00:00:00 12 noon CA 1
Gift for wife
2 1 14-FEB-2003 00:00:00 510.98
14-FEB-2003 00:00:00 5 pm NY 7
Rose Ted
Happy Valentines Day to Mother
3 2 14-FEB-2004 00:00:00 315.99
14-FEB-2004 00:00:00 3 pm VS 2
Ani Forest
Happy Valentines Day to Father
4 2 14-FEB-1999 00:00:00 191.95
14-FEB-1999 00:00:00 2 pm NJ 2
O. John
Happy Valentines Day
5 6 04-MAR-2002 00:00:00 101.95
05-MAR-2002 00:00:00 2:30 pm MO 2
Cora
Happy Birthday from John
6 9 07-APR-2003 00:00:00 221.95
07-APR-2003 00:00:00 3 pm MA 2
Sake Keith
Happy Birthday from Joe
7 9 20-JUN-2004 00:00:00 315.95
21-JUN-2004 00:00:00 12 noon BC 2
Jessica Li
Happy Birthday from Jessica
8 12 31-DEC-1999 00:00:00 135.95
01-JAN-2000 00:00:00 12 noon DI 3
Larry
Happy New Year from Lawrence
9 12 26-DEC-2003 00:00:00 715.95
02-JAN-2004 00:00:00 12 noon SK 7
Did
Happy Birthday from Nancy
10 4 15-JUN-2008 17:39:51 119.95
18-JUN-2008 17:39:51 6:30 pm VG 2
P. Jing
Happy Valentines Day to Jason
11 2 16-JUN-2008 17:39:51 310
18-JUN-2008 17:39:51 3:30 pm DC 2
C. Late
Happy Birthday Day to Jack
12 7 13-JUN-2008 17:39:52 121.95
14-JUN-2008 17:39:52 1:30 pm AC 2
W. Last
Happy Birthday Day to You
13 7 16-JUN-2008 17:39:53 211.95
12-JUN-2008 17:39:53 4:30 pm CA 2
J. Bond
Thanks for hard working
13 rows selected.
SQL> select * from employee;
EMP_NO LASTNAME FIRSTNAME M
---------- -------------------- --------------- -
STREET CITY ST ZIP ZIP_ ARE
------------------------------ -------------------- -- ----- ---- ---
PHONE SALARY BIRTHDATE HIREDATE
-------- ---------- -------------------- --------------------
TITLE DEPT_NO MGR REGION DIVISION
-------------------- ---------- ---------- ---------- ----------
TOTAL_SALES
-----------
1 Anderson Nancy N
33 Ave London NY 11111 1111 212
234-1111 4 21-MAR-1927 00:00:00 01-FEB-1947 00:00:00
Sales Manager 2 100 10
40000
2 Last First F
12 Ave Paris CA 22222 2222 111
867-2222 8 14-FEB-1976 00:00:00 15-MAR-1985 00:00:00
Sales Clerk 2 1 100 10
10000
3 Wash Georgia G
1 Street14 Barton NJ 33333 3333 214
340-3333 12 02-JUL-1977 00:00:00 21-APR-2004 00:00:00
Designer 1 2 100 10
40000
4 Bush Dave D
56 Street Island RI 44444 4444 215
777-4444 22 15-MAY-1945 00:00:00 02-AUG-1975 00:00:00
Designer 1 2 100 10
40000
5 Will Robin W
56 Street Island MA 55555 5555 216
777-5555 25 10-DEC-1980 00:00:00 02-AUG-2007 00:00:00
Designer 1 5 100 10
40000
6 Pete Mona M
13 Ave York MO 66666 6666 217
111-6666 9 14-FEB-1966 00:00:00 15-MAR-1985 00:00:00
Sales Clerk 2 5 100 10
40000
7 Roke John J
67 Ave New York BC 77777 7777 218
122-7777 10 14-JUN-1955 00:00:00 15-MAR-1975 00:00:00
Accountant 3 2 100 10
40000
8 Horry Tedi T
1236 Lane Newton NY 88888 8888 219
222-8888 13 10-JUN-1955 00:00:00 15-AUG-1985 00:00:00
Sales Representative 3 2 100 10
50000
9 Bar Candi C
400 East Street Yorken NY 99999 9999 220
321-9999 12 10-OCT-1933 00:00:00 15-JAN-1969 00:00:00
Sales Representative 3 5 100 10
35000
9 rows selected.
SQL> select * from department;
DEPT_NO DEPT_NAME MGR_NO
---------- -------------------- ----------
1 Design 1
2 Sales 1
3 Development 1
3 rows selected.
SQL>
SQL> select d.dept_name,
2 e.lastname AS sales_name,
3 o.order_date,
4 count(*)
5 from department d, employee e, ord o
6 where
7 d.dept_no = e.dept_no
8 and e.emp_no = o.emp_no
9 group by d.dept_name, e.lastname, o.order_date
10 /
DEPT_NAME SALES_NAME ORDER_DATE
-------------------- -------------------- --------------------
COUNT(*)
----------
Sales Last 14-FEB-1999 00:00:00
1
Sales Last 04-MAR-2002 00:00:00
1
Sales Last 07-APR-2003 00:00:00
1
Sales Last 20-JUN-2004 00:00:00
1
Sales Last 13-JUN-2008 17:39:52
1
Development Roke 14-FEB-2003 00:00:00
1
Sales Last 14-FEB-2004 00:00:00
1
Sales Last 15-JUN-2008 17:39:51
1
Design Wash 31-DEC-1999 00:00:00
1
Development Roke 26-DEC-2003 00:00:00
1
Sales Last 16-JUN-2008 17:39:53
1
Sales Anderson 14-FEB-2002 00:00:00
1
Sales Last 16-JUN-2008 17:39:51
1
13 rows selected.
SQL>
SQL>
SQL> drop table ord;
Table dropped.
SQL> drop table employee;
Table dropped.
SQL> drop table department;
Table dropped.
SQL>
SQL> --
Join columns with full table name reference
SQL> CREATE TABLE emps (
2 emp varchar(30)
3 ,title varchar(30)
4 );
Table created.
SQL>
SQL> INSERT INTO emps VALUES ("Tom","Programmer");
1 row created.
SQL> INSERT INTO emps VALUES ("Jack","Tester");
1 row created.
SQL> INSERT INTO emps VALUES ("Mary","Technician");
1 row created.
SQL>
SQL> CREATE TABLE JobLevel (
2 title varchar(30)
3 ,rank varchar(30)
4 );
Table created.
SQL>
SQL> INSERT INTO JobLevel VALUES ("Programmer","Level1");
1 row created.
SQL> INSERT INTO JobLevel VALUES ("Tester","Level2");
1 row created.
SQL> INSERT INTO JobLevel VALUES ("Technician","Level3");
1 row created.
SQL>
SQL> CREATE TABLE salary (
2 rank varchar(30)
3 ,payment DECIMAL(10,2)
4 );
Table created.
SQL>
SQL> INSERT INTO salary VALUES ("Level1",2000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level2",3000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level3",5000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level4",6000.00);
1 row created.
SQL>
SQL> select * from emps;
EMP
------------------------------
TITLE
------------------------------
Tom
Programmer
Jack
Tester
Mary
Technician
3 rows selected.
SQL> select * from JobLevel;
TITLE
------------------------------
RANK
------------------------------
Programmer
Level1
Tester
Level2
Technician
Level3
3 rows selected.
SQL> select * from salary;
RANK PAYMENT
------------------------------ ----------
Level1 2000
Level2 3000
Level3 5000
Level4 6000
4 rows selected.
SQL>
SQL> SELECT payment FROM salary,JobLevel,emps
2 WHERE salary.rank = JobLevel.rank
3 AND JobLevel.title = emps.title
4 AND emps.emp = "Jack";
PAYMENT
----------
3000
1 row selected.
SQL>
SQL> DROP TABLE emps;
Table dropped.
SQL> DROP TABLE JobLevel;
Table dropped.
SQL> DROP TABLE salary;
Table dropped.
SQL>
SQL>
SQL>
Join three table to find out which employee sold that gift
SQL>
SQL>
SQL> create table emp(
2 emp_id 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 ,company_name varchar2(50));
Table created.
SQL>
SQL>
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
2 (1,"Jones","Joe","J","10 Park Ave","New York","NY","11202","3898","212", "221-4333","Big Company");
1 row created.
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
2 (2,"Smith","Sue","J","20 Rise Ave","New York","NY","11444","3898","212", "436-6773","Little Company");
1 row created.
SQL> insert into emp(emp_id,lastname,firstname,midinit,street,city,state,zip,shortZipCode,area_code,phone,company_name)values
2 (3,"X","Peggy","J","500 June St","New York","NY","45502","3668","212", "234-4444","Medium Company");
1 row created.
SQL>
SQL> create table gift(
2 gift_id integer primary key
3 ,emp_id integer
4 ,register_date date not null
5 ,total_price number(7,2)
6 ,deliver_date date
7 ,deliver_time varchar2(7)
8 ,payment varchar2(2)
9 ,emp_no number(3,0)
10 ,deliver_name varchar2(35)
11 ,message varchar2(100)
12 );
Table created.
SQL>
SQL> insert into gift(gift_id,emp_id,register_date,total_price,deliver_date,deliver_time,payment,emp_no,deliver_name,message)values
2 (1,1,"14-Feb-1999", 123.12, "14-Feb-1999", "12 noon", "CA",1, null, "Happy Birthday to you");
1 row created.
SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time ,payment ,emp_no,deliver_name ,message)values
2 (2,1,"14-Feb-1999", 50.98, "14-feb-1999", "1 pm", "CA",7, "name1", "Happy Birthday");
1 row created.
SQL> insert into gift(gift_id ,emp_id ,register_date ,total_price ,deliver_date ,deliver_time,payment ,emp_no,deliver_name ,message )values
2 (3, 2,"14-Feb-1999", 35.99, "14-feb-1999", "1 pm", "VS",2, "Tom", "Happy Birthday");
1 row created.
SQL>
SQL> create table sale(
2 gift_id integer
3 ,item_id integer
4 ,quantity number(4,0)
5 ,price number(7,2)
6 ,primary key (gift_id ,item_id)
7 );
Table created.
SQL> insert into sale(gift_id, item_id, quantity, price) values(1, 2, 10, 23.00 );
1 row created.
SQL> insert into sale(gift_id, item_id, quantity, price) values(2, 1, 1, 23.11 );
1 row created.
SQL>
SQL>
SQL> select c.firstname || " " || c.lastname as "Name", "(" || c.area_code || ")" ||
2 c.phone as "Telephone", o.gift_id, o.deliver_date, oi.item_id,
3 oi.quantity
4 from gift o, emp c, sale oi
5 where o.emp_id = c.emp_id
6 and o.gift_id = oi.gift_id
7 and oi.item_id = 8;
no rows selected
SQL>
SQL>
SQL> drop table sale;
Table dropped.
SQL> drop table gift;
Table dropped.
SQL> drop table emp;
Table dropped.
Join two tables: column is not mentioned in the SELECT list
SQL> CREATE TABLE Employee(Employee_ID NUMBER(4) NOT NULL,
2 Employee_Name VARCHAR2(10),
3 JOB VARCHAR2(9),
4 Manager_ID NUMBER(4),
5 Start_Date DATE,
6 Salary NUMBER(7, 2),
7 COMM NUMBER(7, 2),
8 Deparment_ID NUMBER(2)
9 );
Table created.
SQL>
SQL> INSERT INTO Employee VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7654, "MARTIN", "SALESMAN", 7698, TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7698, "BLAKE", "MANAGER", 7839, TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7782, "CLARK", "MANAGER", 7839, TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO Employee VALUES (7788, "SCOTT", "ANALYST", 7566, TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7839, "KING", "PRESIDENT", NULL, TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO Employee VALUES (7844, "TURNER", "SALESMAN", 7698, TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7876, "ADAMS", "CLERK", 7788, TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7900, "JAMES", "CLERK", 7698, TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7902, "FORD", "ANALYST", 7566, TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7934, "MILLER", "CLERK", 7782, TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL> CREATE TABLE Department(Deparment_ID NUMBER(2),
2 Department_Name VARCHAR2(14),
3 Location VARCHAR2(13)
4 );
Table created.
SQL>
SQL> INSERT INTO Department VALUES (10, "ACCOUNTING", "NEW YORK");
1 row created.
SQL> INSERT INTO Department VALUES (20, "RESEARCH", "DALLAS");
1 row created.
SQL> INSERT INTO Department VALUES (30, "SALES", "CHICAGO");
1 row created.
SQL> INSERT INTO Department VALUES (40, "OPERATIONS", "BOSTON");
1 row created.
SQL>
SQL> select * from Employee;
EMPLOYEE_ID EMPLOYEE_N JOB MANAGER_ID START_DAT SALARY COMM DEPARMENT_ID
----------- ---------- --------- ---------- --------- ---------- ---------- ------------
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
EMPLOYEE_ID EMPLOYEE_N JOB MANAGER_ID START_DAT SALARY COMM DEPARMENT_ID
----------- ---------- --------- ---------- --------- ---------- ---------- ------------
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
14 rows selected.
SQL> select * from Department;
DEPARMENT_ID DEPARTMENT_NAM LOCATION
------------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
SQL>
SQL>
SQL> -- column is not mentioned in the SELECT list.
SQL>
SQL> SELECT e.Employee_ID, e.Start_Date,
2 e.Employee_Name ,
3 d.Department_Name, d.Location
4 FROM Employee e, Department d
5 WHERE d.Deparment_ID = e.Deparment_ID and d.Department_Name like "A%";
EMPLOYEE_ID START_DAT EMPLOYEE_N DEPARTMENT_NAM LOCATION
----------- --------- ---------- -------------- -------------
7782 09-JUN-81 CLARK ACCOUNTING NEW YORK
7839 17-NOV-81 KING ACCOUNTING NEW YORK
7934 23-JAN-82 MILLER ACCOUNTING NEW YORK
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> drop table Department cascade constraints;
Table dropped.
SQL> drop table Employee cascade constraints;
Table dropped.
SQL>
SQL>
Join using and join on
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>
SQL> create table departments
2 ( deptno NUMBER(2) constraint D_PK primary key
3 , dname VARCHAR2(10)
4 , location VARCHAR2(8)
5 , mgr NUMBER(4)
6 ) ;
Table created.
SQL>
SQL> insert into departments values (10,"ACCOUNTING","NEW YORK",7);
1 row created.
SQL> insert into departments values (20,"TRAINING", "DALLAS", 4);
1 row created.
SQL> insert into departments values (30,"SALES", "CHICAGO", 6);
1 row created.
SQL> insert into departments values (40,"HR", "BOSTON", 9);
1 row created.
SQL>
SQL> select e.empno
2 , e.ENAME
3 , e.init
4 , d.dname
5 , d.location
6 , m.ENAME as Designer
7 from emp e
8 join
9 departments d using (deptno)
10 join
11 emp m on (m.empno = d.mgr);
EMPNO ENAME INIT DNAME LOCATION DESIGNER
---------- -------- ----- ---------- -------- --------
1 Tom N TRAINING DALLAS Jane
2 Jack JAM SALES CHICAGO Black
3 Wil TF SALES CHICAGO Black
4 Jane JM TRAINING DALLAS Jane
5 Mary P SALES CHICAGO Black
6 Black R SALES CHICAGO Black
7 Chris AB ACCOUNTING NEW YORK Chris
8 Smart SCJ TRAINING DALLAS Jane
9 Peter CC ACCOUNTING NEW YORK Chris
10 Take JJ SALES CHICAGO Black
11 Ana AA TRAINING DALLAS Jane
12 Jane R SALES CHICAGO Black
13 Fake MG TRAINING DALLAS Jane
14 Mike TJA ACCOUNTING NEW YORK Chris
14 rows selected.
SQL> drop table emp;
Table dropped.
SQL> drop table departments;
Table dropped.
SQL>
Join using clause
SQL> CREATE TABLE emp(
2 cid NUMBER,
3 postal_code VARCHAR2(10),
4 city VARCHAR2(30),
5 customer_state VARCHAR2(40),
6 country_id CHAR(2),
7 customer_credit NUMBER,
8 email VARCHAR2(30)
9 );
Table created.
SQL>
SQL> CREATE TABLE sales(
2 product_id NUMBER(6),
3 cid NUMBER,
4 time_id DATE,
5 channel_id CHAR(1),
6 promo_id NUMBER(6),
7 sold NUMBER(3),
8 amount NUMBER(10,2),
9 cost NUMBER(10,2)
10 );
Table created.
SQL>
SQL>
SQL> select cid, c.customer_state,s.sold
2 from sales s join emp c
3 using (cid)
4 where product_id = 117;
no rows selected
SQL>
SQL> drop table sales;
Table dropped.
SQL> drop table emp;
Table dropped.
Join with (+)
SQL>
SQL> CREATE TABLE departments
2 (department_id number(10) not null,
3 department_name varchar2(50) not null,
4 CONSTRAINT departments_pk PRIMARY KEY (department_id)
5 );
Table created.
SQL>
SQL>
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 1, "Data Group" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 2, "Purchasing" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 3, "Call Center" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 4, "Communication" );
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE employees
2 ( employee_id number(10) not null,
3 last_name varchar2(50) not null,
4 email varchar2(30),
5 hire_date date,
6 job_id varchar2(30),
7 department_id number(10),
8 salary number(6),
9 manager_id number(6),
10 CONSTRAINT employees_pk PRIMARY KEY (employee_id),
11 CONSTRAINT fk_departments FOREIGN KEY (department_id) REFERENCES departments(department_id)
12 );
Table created.
SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
2 values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1004, "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
2 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);
1 row created.
SQL>
SQL> select * from employees;
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1001 Lawson lawson@g.ru 01-JAN-02 MGR 1 30000 1004
1002 Wells wells@g.ru 01-JAN-02 DBA 2 20000 1005
1003 Bliss bliss@g.ru 01-JAN-02 PROG 3 24000 1004
1004 Kyte tkyte@a.ru 13-JUN-98 MGR 4 25000 1005
1005 Viper sdillon@a .ru 10-JUN-08 PROG 1 20000 1006
1006 Beck clbeck@g.ru 10-JUN-08 PROG 2 20000
1007 Java java01@g.ru 10-JUN-08 PROG 3 20000 1006
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1008 Oracle oracle1@g.ru 10-JUN-08 DBA 4 20000 1006
8 rows selected.
SQL>
SQL> select * from departments;
DEPARTMENT_ID DEPARTMENT_NA
------------- -------------
1 Data Group
2 Purchasing
3 Call Center
4 Communication
4 rows selected.
SQL>
SQL>
SQL> select e.employee_id, e.last_name, d.department_name
2 from employees e, departments d
3 where e.department_id = d.department_id(+)
4 and e.job_id = "MGR"
5
SQL>
SQL> drop table employees cascade constraints ;
Table dropped.
SQL> drop table departments cascade constraints;
Table dropped.
SQL>
SQL>
Use column name alias during table join
SQL>
SQL> CREATE TABLE departments
2 (department_id number(10) not null,
3 department_name varchar2(50) not null,
4 CONSTRAINT departments_pk PRIMARY KEY (department_id)
5 );
Table created.
SQL>
SQL>
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 1, "Data Group" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 2, "Purchasing" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 3, "Call Center" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 4, "Communication" );
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE employees
2 ( employee_id number(10) not null,
3 last_name varchar2(50) not null,
4 email varchar2(30),
5 hire_date date,
6 job_id varchar2(30),
7 department_id number(10),
8 salary number(6),
9 manager_id number(6),
10 CONSTRAINT employees_pk PRIMARY KEY (employee_id),
11 CONSTRAINT fk_departments FOREIGN KEY (department_id) REFERENCES departments(department_id)
12 );
Table created.
SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
2 values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1004, "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
2 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);
1 row created.
SQL>
SQL> select * from employees;
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1001 Lawson lawson@g.ru 01-JAN-02 MGR 1 30000 1004
1002 Wells wells@g.ru 01-JAN-02 DBA 2 20000 1005
1003 Bliss bliss@g.ru 01-JAN-02 PROG 3 24000 1004
1004 Kyte tkyte@a.ru 13-JUN-98 MGR 4 25000 1005
1005 Viper sdillon@a .ru 10-JUN-08 PROG 1 20000 1006
1006 Beck clbeck@g.ru 10-JUN-08 PROG 2 20000
1007 Java java01@g.ru 10-JUN-08 PROG 3 20000 1006
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1008 Oracle oracle1@g.ru 10-JUN-08 DBA 4 20000 1006
8 rows selected.
SQL>
SQL> select * from departments;
DEPARTMENT_ID DEPARTMENT_NA
------------- -------------
1 Data Group
2 Purchasing
3 Call Center
4 Communication
4 rows selected.
SQL>
SQL>
SQL> select employee_id "Emp #", last_name Last,
2 department_name AS "Department"
3 from employees e, departments d
4 where d.department_id = e.department_id
5 and d.department_id = 3
6 /
Emp # LAST Department
---------- -------------------------------------------------- --------------------------------------------------
1003 Bliss Call Center
1007 Java Call Center
SQL>
SQL>
SQL>
SQL> drop table employees cascade constraints;
Table dropped.
SQL> drop table departments cascade constraints;
Table dropped.
SQL>
Use in operator with table join
SQL>
SQL> CREATE TABLE departments
2 (department_id number(10) not null,
3 department_name varchar2(50) not null,
4 CONSTRAINT departments_pk PRIMARY KEY (department_id)
5 );
Table created.
SQL>
SQL>
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 1, "Data Group" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 2, "Purchasing" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 3, "Call Center" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
2 values( 4, "Communication" );
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE employees
2 ( employee_id number(10) not null,
3 last_name varchar2(50) not null,
4 email varchar2(30),
5 hire_date date,
6 job_id varchar2(30),
7 department_id number(10),
8 salary number(6),
9 manager_id number(6),
10 CONSTRAINT employees_pk PRIMARY KEY (employee_id),
11 CONSTRAINT fk_departments FOREIGN KEY (department_id) REFERENCES departments(department_id)
12 );
Table created.
SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
2 values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
2 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1004, "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
2 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
2 values( 1008, "Oracle", "oracle1@g.ru", SYSDATE, "DBA", 20000, 4, 1006);
1 row created.
SQL>
SQL> select * from employees;
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1001 Lawson lawson@g.ru 01-JAN-02 MGR 1 30000 1004
1002 Wells wells@g.ru 01-JAN-02 DBA 2 20000 1005
1003 Bliss bliss@g.ru 01-JAN-02 PROG 3 24000 1004
1004 Kyte tkyte@a.ru 13-JUN-98 MGR 4 25000 1005
1005 Viper sdillon@a .ru 10-JUN-08 PROG 1 20000 1006
1006 Beck clbeck@g.ru 10-JUN-08 PROG 2 20000
1007 Java java01@g.ru 10-JUN-08 PROG 3 20000 1006
EMPLOYEE_ID LAST_NAME EMAIL HIRE_DATE JOB_ID DEPARTMENT_ID SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
1008 Oracle oracle1@g.ru 10-JUN-08 DBA 4 20000 1006
8 rows selected.
SQL>
SQL> select * from departments;
DEPARTMENT_ID DEPARTMENT_NA
------------- -------------
1 Data Group
2 Purchasing
3 Call Center
4 Communication
4 rows selected.
SQL>
SQL>
SQL> select e.employee_id, e.last_name, d.department_name
2 from employees e, departments d
3 where d.department_name in ("Data Group","Purchasing")
4 and e.department_id = d.department_id
5 /
EMPLOYEE_ID LAST_NAME DEPARTMENT_NA
----------- -------------------------------------------------- -------------
1001 Lawson Data Group
1005 Viper Data Group
1002 Wells Purchasing
1006 Beck Purchasing
4 rows selected.
SQL>
SQL> drop table employees cascade constraints;
Table dropped.
SQL> drop table departments cascade constraints;
Table dropped.
SQL>