Documente Academic
Documente Profesional
Documente Cultură
select * from employ where eno in(select eno from employ group
by eno having count(eno)>1) order by eno
select * from employ where eno in(select eno from employ group
by eno having count(eno)>1) order by eno
select * from employ where ename like 'd%'
select * from employ where ename like '%u'
select * from employ where ename like '_h%'
select * from employ where ename like '_____'
select * from employ -(select * from employ where eno in (select
hr from employ))
select getdate()
select * from employ where eno not in( select desghr from
employ)
select * from employ
select * from emp
create table emp (empno integer,ename char(10),sal
numeric(10,2),mgr integer, jdate datetime)
insert into emp values (200,'Mahesh',9000,100,'10-aug-2005')
select * from emp where mgr is not null and empno not in
(select mgr from emp where mgr is null )
select * from emp where empno not in (select mgr from emp where
mgr is not null )
select top 5 sal from emp order by sal desc
select a.sal from emp a where sal>(select e.sal from emp e where
e.sal>a.sal)
select * from emp where empno in (select empno,count(empno) from
emp group by empno having count(empno)>1)
select empno,count(distinct(empno)) from emp group by empno
having count(empno)>1
select e.ename ,m.ename from emp e, emp m where e.mgr=m.empno
SELECT empno, COUNT(EMPNO) FROM EMP GROUP BY EMPNO
select ename from employ where sal=(select max(sal) from employ)
select ename,desg from employ where sal=(select max(sal) from
employ where desg='sm')
update employ set sal=12000 where eno=106
update employ set sal=11000 where ename='danu'
select * from employ where desg='sm' and sal>(select max(sal)
from employ where desg='clerk')
select * from employ where sal=(select max(sal) from employ where
desg='sm')
select mgr from emp where mgr is not null
select count(*) from emp
select distinct(sal) from emp where sal =(select
max(distinct(sal)) from emp) or sal=(select min(distinct(sal))
from emp)
select ename from employ where deptno=(select distinct(deptno)
from employ where desg='sm')
select ename from employ where deptno in(select deptno from
employ where desg='sm')
select desg,sum(sal+isnull(comm,0))from employ group by desg
having sum(sal+isnull(comm,0))>(select max(sal) from employ where
desg='hr')
select datediff(year,jdt,getdate()) from employ
select * from employ where comm is null or comm>0
select * from employ where datediff (year,jdt,hdt)>=3
SELECT ENO, DATEDIFF (MONTH,JDT,HDT) FROM EMPLOY
SELECT * FROM EMPLOY WHERE DATEDIFF (MONTH,JDT,HDT) > 12
SELECT * FROM EMPLOY WHERE DESG IN('SM','HR','MD') AND SAL>=5000
SELECT GETDATE()
SELECT USER_NAME()
SELECT name
FROM sysusers
WHERE name = USER_NAME()
GO
SELECT 'The current user is: '+ convert(char(30), CURRENT_USER)
GO
SELECT 'The current user is: '+ CURRENT_USER
SELECT * FROM EMPLOY WHERE ENAME LIKE B
SHOW USER
For example, what if I wanted to retrieve the first 3 records from my query results. How can I do this?
Answer: To retrieve the Top N records from a query, you can use the following syntax:
SELECT *
FROM (your ordered query) alias_name
WHERE rownum <= Rows_to_return
ORDER BY rownum;
For example, if you wanted to retrieve the first 3 records from the suppliers table, sorted by supplier_name in
ascending order, you would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_name) suppliers2
WHERE rownum <= 3
ORDER BY rownum;
If you wanted to retrieve the first 3 records from the suppliers table, sorted by supplier_name in descending order,
you would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_name DESC) suppliers2
WHERE rownum <= 3
ORDER BY rownum;
If you wanted to retrieve the first 5 records from the suppliers table, sorted by supplier_id in ascending order, you
would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_id) suppliers2
WHERE rownum <= 5
ORDER BY rownum;
If you wanted to retrieve the first 5 records from the suppliers table, sorted by supplier_id in descending order, you
would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_id DESC) suppliers2
WHERE rownum <= 5
ORDER BY rownum;
For example, what if I wanted to retrieve the last 3 records from my query results. How can I do this?
Answer: To retrieve the Bottom N records from a query, you can use the following syntax:
SELECT *
FROM (your query ordered in reverse) alias_name
WHERE rownum <= Rows_to_return
ORDER BY rownum DESC;
If you want to retrieve the bottom records from a query, you need to order your results in reverse in the "your query
ordered in reverse" section of the solution above.
For example, if you wanted to retrieve the last 3 records from the suppliers table, sorted by supplier_name in
ascending order, you would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_name DESC) suppliers2
WHERE rownum <= 3
ORDER BY rownum DESC;
Notice that although you want the last 3 records sorted by supplier_name in ascending order, you actually sort the
supplier_name in descending order in this solution.
If you wanted to retrieve the last 3 records from the suppliers table, sorted by supplier_name in descending order,
you would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_name) suppliers2
WHERE rownum <= 3
ORDER BY rownum DESC;
If you wanted to retrieve the last 5 records from the suppliers table, sorted by supplier_id in ascending order, you
would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_id DESC) suppliers2
WHERE rownum <= 5
ORDER BY rownum DESC;
If you wanted to retrieve the last 5 records from the suppliers table, sorted by supplier_id in descending order, you
would run the following query:
SELECT *
FROM (select * from suppliers ORDER BY supplier_id) suppliers2
WHERE rownum <= 5
Oracle/PLSQL: Retrieve Middle N records from a query
For example, what if I wanted to retrieve records 3 to 5 from my query results. How can I do this?
Answer: To retrieve the Middle N records from a query, you can use the following syntax:
SELECT *
FROM (select alias_name.*, rownum rnum from
(-- your ordered query --) alias_name
where rownum <= UPPER_BOUND )
WHERE rnum >= LOWER_BOUND;
For example, if you wanted to retrieve records 3 through 5 from the suppliers table, sorted by
supplier_name in ascending order, you would run the following query:
SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_name) suppliers2
where rownum <= 5 )
WHERE rnum >= 3;
If you wanted to retrieve records 3 through 5 from the suppliers table, sorted by supplier_name
in descending order, you would run the following query:
SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_name DESC) suppliers2
where rownum <= 5 )
WHERE rnum >= 3;
If you wanted to retrieve records 2 through 4 from the suppliers table, sorted by supplier_id in
ascending order, you would run the following query:
SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_id) suppliers2
where rownum <= 4 )
WHERE rnum >= 2;
If you wanted to retrieve records 2 through 4 from the suppliers table, sorted by supplier_id in
descending order, you would run the following query:
SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_id DESC) suppliers2
where rownum <= 4 )
WHERE rnum >= 2;
Question: How can I retrieve the second highest salary amount from a salary table?
Answer: To retrieve the second highest salary from a salary table, you could run the following query: (please note
that the subquery is sorted in descending order)
SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;
If you wanted to retrieve all fields from the salary table for the second highest salary, you could run the following
query: (please note that the subquery is sorted in descending order)
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;
Question: How can I retrieve the third highest salary amount from a salary table?
Answer: To retrieve the third highest salary from a salary table, you could run the following query: (please note that
the subquery is sorted in descending order)
SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 3 )
WHERE rnum >= 3;
If you wanted to retrieve all fields from the salary table for the third highest salary, you could run the following query:
(please note that the subquery is sorted in descending order)
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 3 )
WHERE rnum >= 3;
Question: How can I retrieve the second lowest salary amount from a salary table?
Answer: To retrieve the second lowest salary from a salary table, you could run the following query: (please note
that the subquery is sorted in ascending order)
SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 2 )
WHERE rnum >= 2;
If you wanted to retrieve all fields from the salary table for the second lowest salary, you could run the following
query: (please note that the subquery is sorted in ascending order)
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 2 )
WHERE rnum >= 2;
Question: How can I retrieve the third lowest salary amount from a salary table?
Answer: To retrieve the third lowest salary from a salary table, you could run the following query: (please note that
the subquery is sorted in ascending order)
SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 3 )
WHERE rnum >= 3;
If you wanted to retrieve all fields from the salary table for the third lowest salary, you could run the following query:
(please note that the subquery is sorted in ascending order)
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 3 )
WHERE rnum >= 3;
Question: How do I execute an SQL script file in SQLPlus?
Answer: To execute a script file in SQLPlus, type @ and then the file name.
For example, if your file was called script.sql, you'd type the following command at the SQL prompt:
The above command assumes that the file is in the current directory. (ie: the current directory is usually the directory
that you were located in before you launched SQLPlus.)
If you need to execute a script file that is not in the current directory, you would type:
For example:
Question: I have a date field in an Oracle table and I want to insert a new record. I'm trying to insert a date with a
time component into this field, but I'm having some problems.
Answer: To insert a date/time value into the Oracle table, you'll need to use the to_date function. The to_date
function allows you to define the format of the date/time value.
format_mask is optional. This is the format that will be used to convert string1 to a date.
nls_language is optional. This is the nls language used to convert string1 to a date.
The following is a list of options for the format_mask parameter. These parameters can be used in many
combinations.
Parameter Explanation
YEAR Year, spelled out
YYYY 4-digit year
YYY
YY Last 3, 2, or 1 digit(s) of year.
Y
IYY
IY Last 3, 2, or 1 digit(s) of ISO year.
I
IYYY 4-digit year based on the ISO standard
Accepts a 2-digit year and returns a 4-digit year.
RRRR A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.
Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).
MM Month (01-12; JAN = 01).
MON Abbreviated name of month.
MONTH Name of month, padded with blanks to length of 9 characters.
RM Roman numeral month (I-XII; JAN = I).
Week of year (1-53) where week 1 starts on the first day of the year
WW
and continues to the seventh day of the year.
Week of month (1-5) where week 1 starts on the first day of the
W
month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.
D Day of week (1-7).
DAY Name of day.
DD Day of month (1-31).
DDD Day of year (1-366).
DY Abbreviated name of day.
J Julian day; the number of days since January 1, 4712 BC.
HH Hour of day (1-12).
HH12 Hour of day (1-12).
HH24 Hour of day (0-23).
MI Minute (0-59).
SS Second (0-59).
SSSSS Seconds past midnight (0-86399).
Fractional seconds. Use a value from 1 to 9 after FF to indicate the
FF
number of digits in the fractional seconds. For example, 'FF4'.
AM, A.M., PM, or P.M. Meridian indicator
AD or A.D AD indicator
BC or B.C. BC indicator
TZD Daylight savings information. For example, 'PST'
TZH Time zone hour.
TZM Time zone minute.
TZR Time zone region.
For example: