You are on page 1of 24

SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT
SQL> alter session set container = orclpdb;

Session altered.

SQL> show con_name

CON_NAME
------------------------------
ORCLPDB
SQL> alter pluggable database open;

Pluggable database altered.

SQL> conn hr/hr@orclpdb;


Connected.
SQL> desc jobs;
Name Null? Type
----------------------------------------- -------- ----------------------------
JOB_ID NOT NULL VARCHAR2(10)
JOB_TITLE NOT NULL VARCHAR2(35)
MIN_SALARY NUMBER(6)
MAX_SALARY NUMBER(6)

SQL> set pagesize 200


SQL> set linesize 200
SQL> select * from employees;

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL


PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID
DEPARTMENT_ID
----------- -------------------- -------------------------
------------------------- -------------------- --------- ---------- ----------
-------------- ---------- -------------
198 Donald OConnell DOCONNEL
650.507.9833 21-JUN-07 SH_CLERK 2600 124
50
199 Douglas Grant DGRANT
650.507.9844 13-JAN-08 SH_CLERK 2600 124
50
200 Jennifer Whalen JWHALEN
515.123.4444 17-SEP-03 AD_ASST 4400 101
10
201 Michael Hartstein MHARTSTE
515.123.5555 17-FEB-04 MK_MAN 13000 100
20
202 Pat Fay PFAY
603.123.6666 17-AUG-05 MK_REP 6000 201
20
203 Susan Mavris SMAVRIS
515.123.7777 07-JUN-02 HR_REP 6500 101
40
204 Hermann Baer HBAER
515.123.8888 07-JUN-02 PR_REP 10000 101
70
205 Shelley Higgins SHIGGINS
515.123.8080 07-JUN-02 AC_MGR 12008 101
110
206 William Gietz WGIETZ
515.123.8181 07-JUN-02 AC_ACCOUNT 8300 205
110
100 Steven King SKING
515.123.4567 17-JUN-03 AD_PRES 24000
90
101 Neena Kochhar NKOCHHAR
515.123.4568 21-SEP-05 AD_VP 17000 100
90
102 Lex De Haan LDEHAAN
515.123.4569 13-JAN-01 AD_VP 17000 100
90
103 Alexander Hunold AHUNOLD
590.423.4567 03-JAN-06 IT_PROG 9000 102
60
104 Bruce Ernst BERNST
590.423.4568 21-MAY-07 IT_PROG 6000 103
60
105 David Austin DAUSTIN
590.423.4569 25-JUN-05 IT_PROG 4800 103
60
106 Valli Pataballa VPATABAL
590.423.4560 05-FEB-06 IT_PROG 4800 103
60
107 Diana Lorentz DLORENTZ
590.423.5567 07-FEB-07 IT_PROG 4200 103
60
108 Nancy Greenberg NGREENBE
515.124.4569 17-AUG-02 FI_MGR 12008 101
100
109 Daniel Faviet DFAVIET
515.124.4169 16-AUG-02 FI_ACCOUNT 9000 108
100
110 John Chen JCHEN
515.124.4269 28-SEP-05 FI_ACCOUNT 8200 108
100
111 Ismael Sciarra ISCIARRA
515.124.4369 30-SEP-05 FI_ACCOUNT 7700 108
100
112 Jose Manuel Urman JMURMAN
515.124.4469 07-MAR-06 FI_ACCOUNT 7800 108
100
113 Luis Popp LPOPP
515.124.4567 07-DEC-07 FI_ACCOUNT 6900 108
100
114 Den Raphaely DRAPHEAL
515.127.4561 07-DEC-02 PU_MAN 11000 100
30
115 Alexander Khoo AKHOO
515.127.4562 18-MAY-03 PU_CLERK 3100 114
30
116 Shelli Baida SBAIDA
515.127.4563 24-DEC-05 PU_CLERK 2900 114
30
117 Sigal Tobias STOBIAS
515.127.4564 24-JUL-05 PU_CLERK 2800 114
30
118 Guy Himuro GHIMURO
515.127.4565 15-NOV-06 PU_CLERK 2600 114
30
119 Karen Colmenares KCOLMENA
515.127.4566 10-AUG-07 PU_CLERK 2500 114
30
120 Matthew Weiss MWEISS
650.123.1234 18-JUL-04 ST_MAN 8000 100
50
121 Adam Fripp AFRIPP
650.123.2234 10-APR-05 ST_MAN 8200 100
50
122 Payam Kaufling PKAUFLIN
650.123.3234 01-MAY-03 ST_MAN 7900 100
50
123 Shanta Vollman SVOLLMAN
650.123.4234 10-OCT-05 ST_MAN 6500 100
50
124 Kevin Mourgos KMOURGOS
650.123.5234 16-NOV-07 ST_MAN 5800 100
50
125 Julia Nayer JNAYER
650.124.1214 16-JUL-05 ST_CLERK 3200 120
50
126 Irene Mikkilineni IMIKKILI
650.124.1224 28-SEP-06 ST_CLERK 2700 120
50
127 James Landry JLANDRY
650.124.1334 14-JAN-07 ST_CLERK 2400 120
50
128 Steven Markle SMARKLE
650.124.1434 08-MAR-08 ST_CLERK 2200 120
50
129 Laura Bissot LBISSOT
650.124.5234 20-AUG-05 ST_CLERK 3300 121
50
130 Mozhe Atkinson MATKINSO
650.124.6234 30-OCT-05 ST_CLERK 2800 121
50
131 James Marlow JAMRLOW
650.124.7234 16-FEB-05 ST_CLERK 2500 121
50
132 TJ Olson TJOLSON
650.124.8234 10-APR-07 ST_CLERK 2100 121
50
133 Jason Mallin JMALLIN
650.127.1934 14-JUN-04 ST_CLERK 3300 122
50
134 Michael Rogers MROGERS
650.127.1834 26-AUG-06 ST_CLERK 2900 122
50
135 Ki Gee KGEE
650.127.1734 12-DEC-07 ST_CLERK 2400 122
50
136 Hazel Philtanker HPHILTAN
650.127.1634 06-FEB-08 ST_CLERK 2200 122
50
137 Renske Ladwig RLADWIG
650.121.1234 14-JUL-03 ST_CLERK 3600 123
50
138 Stephen Stiles SSTILES
650.121.2034 26-OCT-05 ST_CLERK 3200 123
50
139 John Seo JSEO
650.121.2019 12-FEB-06 ST_CLERK 2700 123
50
140 Joshua Patel JPATEL
650.121.1834 06-APR-06 ST_CLERK 2500 123
50
141 Trenna Rajs TRAJS
650.121.8009 17-OCT-03 ST_CLERK 3500 124
50
142 Curtis Davies CDAVIES
650.121.2994 29-JAN-05 ST_CLERK 3100 124
50
143 Randall Matos RMATOS
650.121.2874 15-MAR-06 ST_CLERK 2600 124
50
144 Peter Vargas PVARGAS
650.121.2004 09-JUL-06 ST_CLERK 2500 124
50
145 John Russell JRUSSEL
011.44.1344.429268 01-OCT-04 SA_MAN 14000 .4 100
80
146 Karen Partners KPARTNER
011.44.1344.467268 05-JAN-05 SA_MAN 13500 .3 100
80
147 Alberto Errazuriz AERRAZUR
011.44.1344.429278 10-MAR-05 SA_MAN 12000 .3 100
80
148 Gerald Cambrault GCAMBRAU
011.44.1344.619268 15-OCT-07 SA_MAN 11000 .3 100
80
149 Eleni Zlotkey EZLOTKEY
011.44.1344.429018 29-JAN-08 SA_MAN 10500 .2 100
80
150 Peter Tucker PTUCKER
011.44.1344.129268 30-JAN-05 SA_REP 10000 .3 145
80
151 David Bernstein DBERNSTE
011.44.1344.345268 24-MAR-05 SA_REP 9500 .25 145
80
152 Peter Hall PHALL
011.44.1344.478968 20-AUG-05 SA_REP 9000 .25 145
80
153 Christopher Olsen COLSEN
011.44.1344.498718 30-MAR-06 SA_REP 8000 .2 145
80
154 Nanette Cambrault NCAMBRAU
011.44.1344.987668 09-DEC-06 SA_REP 7500 .2 145
80
155 Oliver Tuvault OTUVAULT
011.44.1344.486508 23-NOV-07 SA_REP 7000 .15 145
80
156 Janette King JKING
011.44.1345.429268 30-JAN-04 SA_REP 10000 .35 146
80
157 Patrick Sully PSULLY
011.44.1345.929268 04-MAR-04 SA_REP 9500 .35 146
80
158 Allan McEwen AMCEWEN
011.44.1345.829268 01-AUG-04 SA_REP 9000 .35 146
80
159 Lindsey Smith LSMITH
011.44.1345.729268 10-MAR-05 SA_REP 8000 .3 146
80
160 Louise Doran LDORAN
011.44.1345.629268 15-DEC-05 SA_REP 7500 .3 146
80
161 Sarath Sewall SSEWALL
011.44.1345.529268 03-NOV-06 SA_REP 7000 .25 146
80
162 Clara Vishney CVISHNEY
011.44.1346.129268 11-NOV-05 SA_REP 10500 .25 147
80
163 Danielle Greene DGREENE
011.44.1346.229268 19-MAR-07 SA_REP 9500 .15 147
80
164 Mattea Marvins MMARVINS
011.44.1346.329268 24-JAN-08 SA_REP 7200 .1 147
80
165 David Lee DLEE
011.44.1346.529268 23-FEB-08 SA_REP 6800 .1 147
80
166 Sundar Ande SANDE
011.44.1346.629268 24-MAR-08 SA_REP 6400 .1 147
80
167 Amit Banda ABANDA
011.44.1346.729268 21-APR-08 SA_REP 6200 .1 147
80
168 Lisa Ozer LOZER
011.44.1343.929268 11-MAR-05 SA_REP 11500 .25 148
80
169 Harrison Bloom HBLOOM
011.44.1343.829268 23-MAR-06 SA_REP 10000 .2 148
80
170 Tayler Fox TFOX
011.44.1343.729268 24-JAN-06 SA_REP 9600 .2 148
80
171 William Smith WSMITH
011.44.1343.629268 23-FEB-07 SA_REP 7400 .15 148
80
172 Elizabeth Bates EBATES
011.44.1343.529268 24-MAR-07 SA_REP 7300 .15 148
80
173 Sundita Kumar SKUMAR
011.44.1343.329268 21-APR-08 SA_REP 6100 .1 148
80
174 Ellen Abel EABEL
011.44.1644.429267 11-MAY-04 SA_REP 11000 .3 149
80
175 Alyssa Hutton AHUTTON
011.44.1644.429266 19-MAR-05 SA_REP 8800 .25 149
80
176 Jonathon Taylor JTAYLOR
011.44.1644.429265 24-MAR-06 SA_REP 8600 .2 149
80
177 Jack Livingston JLIVINGS
011.44.1644.429264 23-APR-06 SA_REP 8400 .2 149
80
178 Kimberely Grant KGRANT
011.44.1644.429263 24-MAY-07 SA_REP 7000 .15 149
179 Charles Johnson CJOHNSON
011.44.1644.429262 04-JAN-08 SA_REP 6200 .1 149
80
180 Winston Taylor WTAYLOR
650.507.9876 24-JAN-06 SH_CLERK 3200 120
50
181 Jean Fleaur JFLEAUR
650.507.9877 23-FEB-06 SH_CLERK 3100 120
50
182 Martha Sullivan MSULLIVA
650.507.9878 21-JUN-07 SH_CLERK 2500 120
50
183 Girard Geoni GGEONI
650.507.9879 03-FEB-08 SH_CLERK 2800 120
50
184 Nandita Sarchand NSARCHAN
650.509.1876 27-JAN-04 SH_CLERK 4200 121
50
185 Alexis Bull ABULL
650.509.2876 20-FEB-05 SH_CLERK 4100 121
50
186 Julia Dellinger JDELLING
650.509.3876 24-JUN-06 SH_CLERK 3400 121
50
187 Anthony Cabrio ACABRIO
650.509.4876 07-FEB-07 SH_CLERK 3000 121
50
188 Kelly Chung KCHUNG
650.505.1876 14-JUN-05 SH_CLERK 3800 122
50
189 Jennifer Dilly JDILLY
650.505.2876 13-AUG-05 SH_CLERK 3600 122
50
190 Timothy Gates TGATES
650.505.3876 11-JUL-06 SH_CLERK 2900 122
50
191 Randall Perkins RPERKINS
650.505.4876 19-DEC-07 SH_CLERK 2500 122
50
192 Sarah Bell SBELL
650.501.1876 04-FEB-04 SH_CLERK 4000 123
50
193 Britney Everett BEVERETT
650.501.2876 03-MAR-05 SH_CLERK 3900 123
50
194 Samuel McCain SMCCAIN
650.501.3876 01-JUL-06 SH_CLERK 3200 123
50
195 Vance Jones VJONES
650.501.4876 17-MAR-07 SH_CLERK 2800 123
50
196 Alana Walsh AWALSH
650.507.9811 24-APR-06 SH_CLERK 3100 124
50
197 Kevin Feeney KFEENEY
650.507.9822 23-MAY-06 SH_CLERK 3000 124
50

107 rows selected.

SQL>
SQL> set linesize 300
SQL> select * from employees:
2 ;

SQL> select job_id, job_title, min_salary from jobs where min_salary > 10000;

JOB_ID JOB_TITLE MIN_SALARY


---------- ----------------------------------- ----------
AD_PRES President 20080
AD_VP Administration Vice President 15000

SQL> desc jobs;


Name
Null? Type

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------- --------
-----------------------------------------------------------------------------------
---------------------------------
JOB_ID
NOT NULL VARCHAR2(10)
JOB_TITLE
NOT NULL VARCHAR2(35)
MIN_SALARY
NUMBER(6)
MAX_SALARY
NUMBER(6)

SQL> desc employees;


Name
Null? Type

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------- --------
-----------------------------------------------------------------------------------
---------------------------------
EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)

SQL> describe jobs;


Name
Null? Type

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------- --------
-----------------------------------------------------------------------------------
---------------------------------
JOB_ID
NOT NULL VARCHAR2(10)
JOB_TITLE
NOT NULL VARCHAR2(35)
MIN_SALARY
NUMBER(6)
MAX_SALARY
NUMBER(6)

SQL> select job_id, job_title, min_salary from jobs where min_salary > 10000;

JOB_ID JOB_TITLE MIN_SALARY


---------- ----------------------------------- ----------
AD_PRES President 20080
AD_VP Administration Vice President 15000

SQL> select employee_id, first_name, last_name from employees where hire_date >
'01-Jan-2008';

EMPLOYEE_ID FIRST_NAME LAST_NAME


----------- -------------------- -------------------------
199 Douglas Grant
128 Steven Markle
136 Hazel Philtanker
149 Eleni Zlotkey
164 Mattea Marvins
165 David Lee
166 Sundar Ande
167 Amit Banda
173 Sundita Kumar
179 Charles Johnson
183 Girard Geoni

11 rows selected.

SQL> select employee_id, first_name, last_name, hire_date from employees where


hire_date > '01-Jan-2008';
EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE
----------- -------------------- ------------------------- ---------
199 Douglas Grant 13-JAN-08
128 Steven Markle 08-MAR-08
136 Hazel Philtanker 06-FEB-08
149 Eleni Zlotkey 29-JAN-08
164 Mattea Marvins 24-JAN-08
165 David Lee 23-FEB-08
166 Sundar Ande 24-MAR-08
167 Amit Banda 21-APR-08
173 Sundita Kumar 21-APR-08
179 Charles Johnson 04-JAN-08
183 Girard Geoni 03-FEB-08

11 rows selected.

SQL> select first_name, salary, round(salary, -3) from employees;

FIRST_NAME SALARY ROUND(SALARY,-3)


-------------------- ---------- ----------------
Donald 2600 3000
Douglas 2600 3000
Jennifer 4400 4000
Michael 13000 13000
Pat 6000 6000
Susan 6500 7000
Hermann 10000 10000
Shelley 12008 12000
William 8300 8000
Steven 24000 24000
Neena 17000 17000
Lex 17000 17000
Alexander 9000 9000
Bruce 6000 6000
David 4800 5000
Valli 4800 5000
Diana 4200 4000
Nancy 12008 12000
Daniel 9000 9000
John 8200 8000
Ismael 7700 8000
Jose Manuel 7800 8000
Luis 6900 7000
Den 11000 11000
Alexander 3100 3000
Shelli 2900 3000
Sigal 2800 3000
Guy 2600 3000
Karen 2500 3000
Matthew 8000 8000
Adam 8200 8000
Payam 7900 8000
Shanta 6500 7000
Kevin 5800 6000
Julia 3200 3000
Irene 2700 3000
James 2400 2000
Steven 2200 2000
Laura 3300 3000
Mozhe 2800 3000
James 2500 3000
TJ 2100 2000
Jason 3300 3000
Michael 2900 3000
Ki 2400 2000
Hazel 2200 2000
Renske 3600 4000
Stephen 3200 3000
John 2700 3000
Joshua 2500 3000
Trenna 3500 4000
Curtis 3100 3000
Randall 2600 3000
Peter 2500 3000
John 14000 14000
Karen 13500 14000
Alberto 12000 12000
Gerald 11000 11000
Eleni 10500 11000
Peter 10000 10000
David 9500 10000
Peter 9000 9000
Christopher 8000 8000
Nanette 7500 8000
Oliver 7000 7000
Janette 10000 10000
Patrick 9500 10000
Allan 9000 9000
Lindsey 8000 8000
Louise 7500 8000
Sarath 7000 7000
Clara 10500 11000
Danielle 9500 10000
Mattea 7200 7000
David 6800 7000
Sundar 6400 6000
Amit 6200 6000
Lisa 11500 12000
Harrison 10000 10000
Tayler 9600 10000
William 7400 7000
Elizabeth 7300 7000
Sundita 6100 6000
Ellen 11000 11000
Alyssa 8800 9000
Jonathon 8600 9000
Jack 8400 8000
Kimberely 7000 7000
Charles 6200 6000
Winston 3200 3000
Jean 3100 3000
Martha 2500 3000
Girard 2800 3000
Nandita 4200 4000
Alexis 4100 4000
Julia 3400 3000
Anthony 3000 3000
Kelly 3800 4000
Jennifer 3600 4000
Timothy 2900 3000
Randall 2500 3000
Sarah 4000 4000
Britney 3900 4000
Samuel 3200 3000
Vance 2800 3000
Alana 3100 3000
Kevin 3000 3000

107 rows selected.

SQL> select employee_id, hire_date, to_char(hire_date, 'Month') "Hire_Month" from


employees where to_char(hire_date, 'MM')='05';

EMPLOYEE_ID HIRE_DATE Hire_Month


----------- --------- ------------------------------------
104 21-MAY-07 May
115 18-MAY-03 May
122 01-MAY-03 May
174 11-MAY-04 May
178 24-MAY-07 May
197 23-MAY-06 May

6 rows selected.

SQL> select employee_id, first_name, last_name, job_id, job_title, hire_date,


to_char(hire_date, 'Month') "Hire_Month" from employees where to_char(hire_date,
'MM')='05';
select employee_id, first_name, last_name, job_id, job_title, hire_date,
to_char(hire_date, 'Month') "Hire_Month" from employees where to_char(hire_date,
'MM')='05'
*
ERROR at line 1:
ORA-00904: "JOB_TITLE": invalid identifier

SQL> select employee_id, first_name, last_name, job_id, job_title, hire_date,


to_char(hire_date, 'Month') "Hire_Month" from employees select employee_id,
hire_date, to_char(hire_date, 'Month') "Hire_Month" from employees where
to_char(hire_date, 'MM')='05';where to_char(hire_date, 'MM')='05';
select employee_id, first_name, last_name, job_id, job_title, hire_date,
to_char(hire_date, 'Month') "Hire_Month" from employees select employee_id,
hire_date, to_char(hire_date, 'Month') "Hire_Month" from employees where
to_char(hire_date, 'MM')='05';where to_char(hire_date, 'MM')='05'

*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> select employee_id, first_name, last_name, job_id, hire_date,


to_char(hire_date, 'Month') "Hire_Month" from employees select employee_id,
hire_date, to_char(hire_date, 'Month') "Hire_Month" from employees where
to_char(hire_date, 'MM')='05';where to_char(hire_date, 'MM')='05';
select employee_id, first_name, last_name, job_id, hire_date, to_char(hire_date,
'Month') "Hire_Month" from employees select employee_id, hire_date,
to_char(hire_date, 'Month') "Hire_Month" from employees where to_char(hire_date,
'MM')='05';where to_char(hire_date, 'MM')='05'

*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> ed
Wrote file afiedt.buf

1* select employee_id, first_name, last_name, hire_date, to_char(hire_date,


'Month') "Hire_Month" from employees select employee_id, hire_date,
to_char(hire_date, 'Month') "Hire_Month" from employees where to_char(hire_date,
'MM')='05';where to_char(hire_date, 'MM')='05'
SQL>
SQL> select employee_id, first_name, last_name, hire_date, to_char(hire_date,
'Month') "Hire_Month" from employees where to_char(hire_date, 'MM')='05';

EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE Hire_Month


----------- -------------------- ------------------------- ---------
------------------------------------
104 Bruce Ernst 21-MAY-07 May
115 Alexander Khoo 18-MAY-03 May
122 Payam Kaufling 01-MAY-03 May
174 Ellen Abel 11-MAY-04 May
178 Kimberely Grant 24-MAY-07 May
197 Kevin Feeney 23-MAY-06 May

6 rows selected.

SQL> select first_name, hire_date, last_day(hire_date) "Salary Date" from


employees;

FIRST_NAME HIRE_DATE Salary Da


-------------------- --------- ---------
Donald 21-JUN-07 30-JUN-07
Douglas 13-JAN-08 31-JAN-08
Jennifer 17-SEP-03 30-SEP-03
Michael 17-FEB-04 29-FEB-04
Pat 17-AUG-05 31-AUG-05
Susan 07-JUN-02 30-JUN-02
Hermann 07-JUN-02 30-JUN-02
Shelley 07-JUN-02 30-JUN-02
William 07-JUN-02 30-JUN-02
Steven 17-JUN-03 30-JUN-03
Neena 21-SEP-05 30-SEP-05
Lex 13-JAN-01 31-JAN-01
Alexander 03-JAN-06 31-JAN-06
Bruce 21-MAY-07 31-MAY-07
David 25-JUN-05 30-JUN-05
Valli 05-FEB-06 28-FEB-06
Diana 07-FEB-07 28-FEB-07
Nancy 17-AUG-02 31-AUG-02
Daniel 16-AUG-02 31-AUG-02
John 28-SEP-05 30-SEP-05
Ismael 30-SEP-05 30-SEP-05
Jose Manuel 07-MAR-06 31-MAR-06
Luis 07-DEC-07 31-DEC-07
Den 07-DEC-02 31-DEC-02
Alexander 18-MAY-03 31-MAY-03
Shelli 24-DEC-05 31-DEC-05
Sigal 24-JUL-05 31-JUL-05
Guy 15-NOV-06 30-NOV-06
Karen 10-AUG-07 31-AUG-07
Matthew 18-JUL-04 31-JUL-04
Adam 10-APR-05 30-APR-05
Payam 01-MAY-03 31-MAY-03
Shanta 10-OCT-05 31-OCT-05
Kevin 16-NOV-07 30-NOV-07
Julia 16-JUL-05 31-JUL-05
Irene 28-SEP-06 30-SEP-06
James 14-JAN-07 31-JAN-07
Steven 08-MAR-08 31-MAR-08
Laura 20-AUG-05 31-AUG-05
Mozhe 30-OCT-05 31-OCT-05
James 16-FEB-05 28-FEB-05
TJ 10-APR-07 30-APR-07
Jason 14-JUN-04 30-JUN-04
Michael 26-AUG-06 31-AUG-06
Ki 12-DEC-07 31-DEC-07
Hazel 06-FEB-08 29-FEB-08
Renske 14-JUL-03 31-JUL-03
Stephen 26-OCT-05 31-OCT-05
John 12-FEB-06 28-FEB-06
Joshua 06-APR-06 30-APR-06
Trenna 17-OCT-03 31-OCT-03
Curtis 29-JAN-05 31-JAN-05
Randall 15-MAR-06 31-MAR-06
Peter 09-JUL-06 31-JUL-06
John 01-OCT-04 31-OCT-04
Karen 05-JAN-05 31-JAN-05
Alberto 10-MAR-05 31-MAR-05
Gerald 15-OCT-07 31-OCT-07
Eleni 29-JAN-08 31-JAN-08
Peter 30-JAN-05 31-JAN-05
David 24-MAR-05 31-MAR-05
Peter 20-AUG-05 31-AUG-05
Christopher 30-MAR-06 31-MAR-06
Nanette 09-DEC-06 31-DEC-06
Oliver 23-NOV-07 30-NOV-07
Janette 30-JAN-04 31-JAN-04
Patrick 04-MAR-04 31-MAR-04
Allan 01-AUG-04 31-AUG-04
Lindsey 10-MAR-05 31-MAR-05
Louise 15-DEC-05 31-DEC-05
Sarath 03-NOV-06 30-NOV-06
Clara 11-NOV-05 30-NOV-05
Danielle 19-MAR-07 31-MAR-07
Mattea 24-JAN-08 31-JAN-08
David 23-FEB-08 29-FEB-08
Sundar 24-MAR-08 31-MAR-08
Amit 21-APR-08 30-APR-08
Lisa 11-MAR-05 31-MAR-05
Harrison 23-MAR-06 31-MAR-06
Tayler 24-JAN-06 31-JAN-06
William 23-FEB-07 28-FEB-07
Elizabeth 24-MAR-07 31-MAR-07
Sundita 21-APR-08 30-APR-08
Ellen 11-MAY-04 31-MAY-04
Alyssa 19-MAR-05 31-MAR-05
Jonathon 24-MAR-06 31-MAR-06
Jack 23-APR-06 30-APR-06
Kimberely 24-MAY-07 31-MAY-07
Charles 04-JAN-08 31-JAN-08
Winston 24-JAN-06 31-JAN-06
Jean 23-FEB-06 28-FEB-06
Martha 21-JUN-07 30-JUN-07
Girard 03-FEB-08 29-FEB-08
Nandita 27-JAN-04 31-JAN-04
Alexis 20-FEB-05 28-FEB-05
Julia 24-JUN-06 30-JUN-06
Anthony 07-FEB-07 28-FEB-07
Kelly 14-JUN-05 30-JUN-05
Jennifer 13-AUG-05 31-AUG-05
Timothy 11-JUL-06 31-JUL-06
Randall 19-DEC-07 31-DEC-07
Sarah 04-FEB-04 29-FEB-04
Britney 03-MAR-05 31-MAR-05
Samuel 01-JUL-06 31-JUL-06
Vance 17-MAR-07 31-MAR-07
Alana 24-APR-06 30-APR-06
Kevin 23-MAY-06 31-MAY-06

107 rows selected.

SQL> select employee_id, first_name, hire_date, floor(months_between(sysdate,


hire_date)/12) "Service Duration" from employees;

EMPLOYEE_ID FIRST_NAME HIRE_DATE Service Duration


----------- -------------------- --------- ----------------
198 Donald 21-JUN-07 14
199 Douglas 13-JAN-08 13
200 Jennifer 17-SEP-03 18
201 Michael 17-FEB-04 17
202 Pat 17-AUG-05 16
203 Susan 07-JUN-02 19
204 Hermann 07-JUN-02 19
205 Shelley 07-JUN-02 19
206 William 07-JUN-02 19
100 Steven 17-JUN-03 18
101 Neena 21-SEP-05 16
102 Lex 13-JAN-01 20
103 Alexander 03-JAN-06 15
104 Bruce 21-MAY-07 14
105 David 25-JUN-05 16
106 Valli 05-FEB-06 15
107 Diana 07-FEB-07 14
108 Nancy 17-AUG-02 19
109 Daniel 16-AUG-02 19
110 John 28-SEP-05 16
111 Ismael 30-SEP-05 16
112 Jose Manuel 07-MAR-06 15
113 Luis 07-DEC-07 13
114 Den 07-DEC-02 18
115 Alexander 18-MAY-03 18
116 Shelli 24-DEC-05 15
117 Sigal 24-JUL-05 16
118 Guy 15-NOV-06 14
119 Karen 10-AUG-07 14
120 Matthew 18-JUL-04 17
121 Adam 10-APR-05 16
122 Payam 01-MAY-03 18
123 Shanta 10-OCT-05 16
124 Kevin 16-NOV-07 13
125 Julia 16-JUL-05 16
126 Irene 28-SEP-06 15
127 James 14-JAN-07 14
128 Steven 08-MAR-08 13
129 Laura 20-AUG-05 16
130 Mozhe 30-OCT-05 15
131 James 16-FEB-05 16
132 TJ 10-APR-07 14
133 Jason 14-JUN-04 17
134 Michael 26-AUG-06 15
135 Ki 12-DEC-07 13
136 Hazel 06-FEB-08 13
137 Renske 14-JUL-03 18
138 Stephen 26-OCT-05 16
139 John 12-FEB-06 15
140 Joshua 06-APR-06 15
141 Trenna 17-OCT-03 18
142 Curtis 29-JAN-05 16
143 Randall 15-MAR-06 15
144 Peter 09-JUL-06 15
145 John 01-OCT-04 17
146 Karen 05-JAN-05 16
147 Alberto 10-MAR-05 16
148 Gerald 15-OCT-07 14
149 Eleni 29-JAN-08 13
150 Peter 30-JAN-05 16
151 David 24-MAR-05 16
152 Peter 20-AUG-05 16
153 Christopher 30-MAR-06 15
154 Nanette 09-DEC-06 14
155 Oliver 23-NOV-07 13
156 Janette 30-JAN-04 17
157 Patrick 04-MAR-04 17
158 Allan 01-AUG-04 17
159 Lindsey 10-MAR-05 16
160 Louise 15-DEC-05 15
161 Sarath 03-NOV-06 14
162 Clara 11-NOV-05 15
163 Danielle 19-MAR-07 14
164 Mattea 24-JAN-08 13
165 David 23-FEB-08 13
166 Sundar 24-MAR-08 13
167 Amit 21-APR-08 13
168 Lisa 11-MAR-05 16
169 Harrison 23-MAR-06 15
170 Tayler 24-JAN-06 15
171 William 23-FEB-07 14
172 Elizabeth 24-MAR-07 14
173 Sundita 21-APR-08 13
174 Ellen 11-MAY-04 17
175 Alyssa 19-MAR-05 16
176 Jonathon 24-MAR-06 15
177 Jack 23-APR-06 15
178 Kimberely 24-MAY-07 14
179 Charles 04-JAN-08 13
180 Winston 24-JAN-06 15
181 Jean 23-FEB-06 15
182 Martha 21-JUN-07 14
183 Girard 03-FEB-08 13
184 Nandita 27-JAN-04 17
185 Alexis 20-FEB-05 16
186 Julia 24-JUN-06 15
187 Anthony 07-FEB-07 14
188 Kelly 14-JUN-05 16
189 Jennifer 13-AUG-05 16
190 Timothy 11-JUL-06 15
191 Randall 19-DEC-07 13
192 Sarah 04-FEB-04 17
193 Britney 03-MAR-05 16
194 Samuel 01-JUL-06 15
195 Vance 17-MAR-07 14
196 Alana 24-APR-06 15
197 Kevin 23-MAY-06 15

107 rows selected.

SQL> select * from employees where to_char(hire_date, 'yy')='01';

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL


PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID
DEPARTMENT_ID
----------- -------------------- -------------------------
------------------------- -------------------- --------- ---------- ----------
-------------- ---------- -------------
102 Lex De Haan LDEHAAN
515.123.4569 13-JAN-01 AD_VP 17000 100
90

SQL> select * from employees where to_char(hire_date, 'yy')= 21;

no rows selected

SQL> select * from employees where to_char(hire_date, 'yy')= to_char(sysdate,


'yy');

no rows selected

SQL> select count(employee_id), to_char(hire_date, 'Mon'), to_char(hire_date, 'MM')


from employees group by to_char(hire_date, 'Mon'), to_char(hire_date, 'MM') order
by to_char(hire_date, 'MM');

COUNT(EMPLOYEE_ID) TO_CHAR(HIRE TO
------------------ ------------ --
14 Jan 01
13 Feb 02
17 Mar 03
7 Apr 04
6 May 05
11 Jun 06
7 Jul 07
9 Aug 08
5 Sep 09
6 Oct 10
5 Nov 11
7 Dec 12

12 rows selected.

SQL> select count(employee_id), to_char(hire_date, 'Month'), to_char(hire_date, MM)


from employees group by to_char(hire_date, 'Month'), to_char(hire_date, 'MM') where
to_char(hire_date, 'yy')= to_char(sysdate, 'yy');
select count(employee_id), to_char(hire_date, 'Month'), to_char(hire_date, MM) from
employees group by to_char(hire_date, 'Month'), to_char(hire_date, 'MM') where
to_char(hire_date, 'yy')= to_char(sysdate, 'yy')

*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> select count(employee_id), to_char(hire_date, 'Month'), to_char(hire_date, MM)


from employees group by to_char(hire_date, 'Month'), to_char(hire_date, 'MM') where
to_char(hire_date, 'yy')= to_char(sysdate, 'yy');
select count(employee_id), to_char(hire_date, 'Month'), to_char(hire_date, MM) from
employees group by to_char(hire_date, 'Month'), to_char(hire_date, 'MM') where
to_char(hire_date, 'yy')= to_char(sysdate, 'yy')

*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> select count(employee_id), manager_id from employees where manager_id is not


null group by manager_id order by count(employee_id);

COUNT(EMPLOYEE_ID) MANAGER_ID
------------------ ----------
1 205
1 201
1 102
4 103
5 114
5 108
5 101
6 146
6 149
6 148
6 147
6 145
8 120
8 124
8 123
8 122
8 121
14 100

18 rows selected.

SQL> select employee_id, end_date from employees join job_history using


(employee_id) where end_date is not null;
EMPLOYEE_ID END_DATE
----------- ---------
102 24-JUL-06
101 27-OCT-01
101 15-MAR-05
201 19-DEC-07
114 31-DEC-07
122 31-DEC-07
200 17-JUN-01
176 31-DEC-06
176 31-DEC-07
200 31-DEC-06

10 rows selected.

SQL> select employee_id from employees where to_char(hire_date, 'dd')>15;

EMPLOYEE_ID
-----------
198
200
201
202
100
101
104
105
108
109
110
111
115
116
117
120
124
125
126
129
130
131
134
138
141
142
149
150
151
152
153
155
156
163
164
165
166
167
169
170
171
172
173
175
176
177
178
180
181
182
184
185
186
191
195
196
197

57 rows selected.

SQL> select count(employee_id) from employees where to_char(hire_date, 'dd')>15;

COUNT(EMPLOYEE_ID)
------------------
57

SQL> select country_name, country_id, count(city) from countries join locations


using (country_id) group by country_id, country_name;

COUNTRY_NAME CO COUNT(CITY)
---------------------------------------- -- -----------
United Kingdom UK 3
Japan JP 2
Canada CA 2
China CN 1
India IN 1
Netherlands NL 1
Australia AU 1
Mexico MX 1
United States of America US 4
Brazil BR 1
Switzerland CH 2
Germany DE 1
Italy IT 2
Singapore SG 1

14 rows selected.

SQL> select avg(salary) from employees where commission_pct is not null;

AVG(SALARY)
-----------
8900

SQL> select job_id, job_title, count(employee_id), avg(salary), max(salary),


min(salary) from employees join jobs using (job_id) group by job_id, job_title;

JOB_ID JOB_TITLE COUNT(EMPLOYEE_ID) AVG(SALARY)


MAX(SALARY) MIN(SALARY)
---------- ----------------------------------- ------------------ -----------
----------- -----------
AC_ACCOUNT Public Accountant 1 8300
8300 8300
AC_MGR Accounting Manager 1 12008
12008 12008
AD_ASST Administration Assistant 1 4400
4400 4400
AD_PRES President 1 24000
24000 24000
AD_VP Administration Vice President 2 17000
17000 17000
FI_ACCOUNT Accountant 5 7920
9000 6900
FI_MGR Finance Manager 1 12008
12008 12008
HR_REP Human Resources Representative 1 6500
6500 6500
IT_PROG Programmer 5 5760
9000 4200
MK_MAN Marketing Manager 1 13000
13000 13000
MK_REP Marketing Representative 1 6000
6000 6000
PR_REP Public Relations Representative 1 10000
10000 10000
PU_CLERK Purchasing Clerk 5 2780
3100 2500
PU_MAN Purchasing Manager 1 11000
11000 11000
SA_MAN Sales Manager 5 12200
14000 10500
SA_REP Sales Representative 30 8350
11500 6100
SH_CLERK Shipping Clerk 20 3215
4200 2500
ST_CLERK Stock Clerk 20 2785
3600 2100
ST_MAN Stock Manager 5 7280
8200 5800

19 rows selected.

SQL> select job_id, job_title, count(employee_id), avg(salary), max(salary),


min(salary) from employees join jobs using (job_id) group by job_id, job_title
having avg(salary)>10000;

JOB_ID JOB_TITLE COUNT(EMPLOYEE_ID) AVG(SALARY)


MAX(SALARY) MIN(SALARY)
---------- ----------------------------------- ------------------ -----------
----------- -----------
SA_MAN Sales Manager 5 12200
14000 10500
PU_MAN Purchasing Manager 1 11000
11000 11000
AD_VP Administration Vice President 2 17000
17000 17000
AC_MGR Accounting Manager 1 12008
12008 12008
AD_PRES President 1 24000
24000 24000
FI_MGR Finance Manager 1 12008
12008 12008
MK_MAN Marketing Manager 1 13000
13000 13000

7 rows selected.

SQL> select job_id, job_title, count(employee_id), from employees join jobs using
(job_id) group by job_id, job_title having avg(salary)>10000;
select job_id, job_title, count(employee_id), from employees join jobs using
(job_id) group by job_id, job_title having avg(salary)>10000
*
ERROR at line 1:
ORA-00936: missing expression

SQL> select job_id, job_title, count(employee_id) from employees join jobs using
(job_id) group by job_id, job_title having avg(salary)>10000;

JOB_ID JOB_TITLE COUNT(EMPLOYEE_ID)


---------- ----------------------------------- ------------------
SA_MAN Sales Manager 5
PU_MAN Purchasing Manager 1
AD_VP Administration Vice President 2
AC_MGR Accounting Manager 1
AD_PRES President 1
FI_MGR Finance Manager 1
MK_MAN Marketing Manager 1

7 rows selected.

SQL> select job_id, job_title, avg(salary) from employees join jobs using (job_id)
group by job_id, job_title having avg(salary)>10000;

JOB_ID JOB_TITLE AVG(SALARY)


---------- ----------------------------------- -----------
SA_MAN Sales Manager 12200
PU_MAN Purchasing Manager 11000
AD_VP Administration Vice President 17000
AC_MGR Accounting Manager 12008
AD_PRES President 24000
FI_MGR Finance Manager 12008
MK_MAN Marketing Manager 13000

7 rows selected.

SQL> select count(employee_id) "No. of Employee Joined", to_char(hire_date, 'yy')


Year from employees group by to_char(hire_date, 'yy');

No. of Employee Joined YE


---------------------- --
6 03
11 08
1 01
29 05
24 06
10 04
7 02
19 07

8 rows selected.

SQL> select count(employee_id) "No. of Employee Joined", to_char(hire_date, 'yy')


Year from employees group by to_char(hire_date, 'yy') where count(employee_id)>10;
select count(employee_id) "No. of Employee Joined", to_char(hire_date, 'yy') Year
from employees group by to_char(hire_date, 'yy') where count(employee_id)>10

*
ERROR at line 1:
ORA-00933: SQL command not properly ended

SQL> select department_id, department_name, count(employee_id) from employees join


departments using (department_id) where commission_pct is not null group by
department_id, department_name having count(employee_id)>5;

DEPARTMENT_ID DEPARTMENT_NAME COUNT(EMPLOYEE_ID)


------------- ------------------------------ ------------------
80 Sales 34

SQL> select employee_id, count(end_date) from employees join job_history using


(employee_id) group by employee_id;

EMPLOYEE_ID COUNT(END_DATE)
----------- ---------------
101 2
102 1
114 1
122 1
176 2
200 2
201 1

7 rows selected.

SQL> select job_title, count(employee_id) from (select sysdate-hire_date,


employee_id, job_id from employees where sysdate-hire_date>100) join jobs using
(job_id) group by job_title having count(employee_id)>3;

JOB_TITLE COUNT(EMPLOYEE_ID)
----------------------------------- ------------------
Stock Clerk 20
Accountant 5
Sales Manager 5
Sales Representative 30
Stock Manager 5
Shipping Clerk 20
Programmer 5
Purchasing Clerk 5

8 rows selected.

SQL> select department_id, count(employee_id), to_char(hire_date, 'yy') from


employees where department_id is not null group by department_id,
to_char(hire_date, 'yy') order by to_char(hire_date, 'yy'), department_id;
DEPARTMENT_ID COUNT(EMPLOYEE_ID) TO
------------- ------------------ --
90 1 01
30 1 02
40 1 02
70 1 02
100 2 02
110 2 02
10 1 03
30 1 03
50 3 03
90 1 03
20 1 04
50 4 04
80 5 04
20 1 05
30 2 05
50 12 05
60 1 05
80 10 05
90 1 05
100 2 05
30 1 06
50 13 06
60 2 06
80 7 06
100 1 06
30 1 07
50 9 07
60 2 07
80 5 07
100 1 07
50 4 08
80 7 08

32 rows selected.

SQL> select to_char(hire_date, 'Mon'), count(employee_id), city from employees join


departments using (department_id) join locations using (location_id) where
lower(city)='sydney' group by to_char(hire_date, 'Mon'), city having
count(employee_id)>5 ;

no rows selected

SQL> select employee_id, start_date from employees join job_history using


(employee_id) where start_date is null;

no rows selected

SQL> select manager_id, count(employee_id) from employees group by manager_id


having count(employee_id)>5;

MANAGER_ID COUNT(EMPLOYEE_ID)
---------- ------------------
124 8
121 8
145 6
120 8
122 8
146 6
148 6
149 6
100 14
123 8
147 6

11 rows selected.

SQL> select department_id, count(employee_id), max(hire_date) from employees group


by department_id having max(hire_date)< sysdate - NUMTOYMINTERVAL(2, 'year');

DEPARTMENT_ID COUNT(EMPLOYEE_ID) MAX(HIRE_


------------- ------------------ ---------
50 45 08-MAR-08
40 1 07-JUN-02
110 2 07-JUN-02
70 1 07-JUN-02
90 3 21-SEP-05
30 6 10-AUG-07
10 1 17-SEP-03
1 24-MAY-07
20 2 17-AUG-05
60 5 21-MAY-07
100 6 07-DEC-07
80 34 21-APR-08

12 rows selected.

You might also like