Professional Documents
Culture Documents
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
Table 2:
SELECT * FROM SALES;
2. Write a SQL query to find the products which does not have sales at
all?
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
LEFT OUTER JOIN
SALES S
ON (P.PRODUCT_ID = S.P
Or
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
WHERE P.PRODUCT_ID NOT IN
(SELECT DISTINCT PRODUCT_ID FROM SALES);
or
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
WHERE NOT EXISTS
(SELECT 1 FROM SALES S WHERE S.PRODUCT_ID = P.PRODUCT_ID);
6. Write a query to display only Friday dates from Jan, 2000 to till now?
SELECT C_DATE, TO_CHAR(C_DATE,'DY') FROM (SELECT TO_DATE('01-JAN-2000','DD-
MON-YYYY')+LEVEL-1 C_DATE FROM DUAL CONNECT BY LEVEL <=(SYSDATE -
TO_DATE('01-JAN-2000','DD-MON-YYYY')+1)) WHERE TO_CHAR(C_DATE,'DY') = 'FRI';
gives
————-
neg | pos
————-
-10 | 10
————-
SELECT SUM(CASE WHEN num < 0 THEN num ELSE 0 END) neg,
SUM(CASE WHEN num > 0 THEN num ELSE 0 END)pos
FROM neg_pos;
8. From the given table, find those employees who are more than 21
years of age.
Table Name:- Emp
Name Birth_date
Akash 12-MAR-93
Rahul 23-APR-96
Sagar 14-JAN-92
Mahesh 07-Dec-00
Nitin 14-FEB-97
Kiran 15-OCT-98
13. Display each months start and end date upto last month of the year
3. SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), i) start_date, TRUNC (LAST_DAY
(ADD_MONTHS (SYSDATE, i))) end_date FROM XMLTABLE ('for $i in 0 to xs:int(D) return
$i' PASSING XMLELEMENT (d,FLOOR (MONTHS_BETWEEN (ADD_MONTHS (TRUNC
(SYSDATE, 'YEAR') - 1, 12),SYSDATE)))COLUMNS i INTEGER PATH '.');
(OR)
7. SELECT *FROM employees MINUS SELECT * FROM employees WHERE ROWNUM <
(SELECT COUNT (*) FROM employees);
17. if exp1 and exp2 are not equal it should print first exp if both exp
are same then it will display 'no_match' string.output should be in
this way.
18. Find those employees whose last name ends with n letter. (don’t use
like operator)
9. Selectlast_name from employees where length(Last_name)-length(trim(trailing( ‘n’ from
last_name))=1;
19. Display the output. Date, hour, min, day—
Date hour min day
------------- ---------- -- -- -- ---------
22-feb-11 05:49:42 05 49 TUESDAY
21. use the hire_date column from employees table and prove that yy
works on current century and rr uses internal algorithm. display the
output.
rr-date yy-date
---------------------- --------------------
11-jul-1998 11-jul-2098
19-dec-1999 19-dec-2099
04-feb-1996 04-feb-2096
03-mar-1997 03-mar-2097
11. SQL> selectto_char(hire_date,'dd-mon-yyyy') "rr-date", add_months(to_char(hire_date, 'dd
mon-yyyy'), 1200) “yy-date” from employees;
22. .display the output. (use sysdate function from dual table)
SQL> seventeen of 10 th
26. findout the day for the date ('15-jan-11')-- use dual table.?
15. SQL> Select to_char(to_date('15-jan11','dd-mon-yy'),'day') from dual;
27. prompt the value for date and it should display the day for prompted
date.
16. SQL> select to_char(to_date('&date','dd-mm-yy'),'day') from dual;
28. Display Region and total number of employees from that region in
the following format
East West North South
------- -------- --------- ---------
10 40 34 76
17. SQL>Selectmax(decode(Region,'East',count(region))) “East”,
max(decode(Region,'West',count(Region))) “West”,
min(decode(Region,'North',count(Region))) “North”,
avg(decode(Region,’South’,count(Region))) “South”
from Region group by region_name;
29. replace AA where a in the last_name. display the output..
NAME LAST_NAME
--------------- -------------------------
TAAylor Taylor
TobiAAs Tobias
TuvAAult Tuvault
UrmAAn Urman
Ans :
18. SQL> select replace(last_name,'a','AA') from employees;
30. dispaly the output.first letter from the first_name column and then
aad '_' and then last_name column
then '@focus.com' string.
J_Urman@focus.com
P_Vargas@focus.com
C_Vishney@focus.com
S_Vollman@focus.com
A_Walsh@focus.com
19. SQL> select concat(substr(first_name,1,1), concat('_',concat(last_name,'@focus.com'))) from
employees;
31. display the employee number, hire date, number of years employed,
six-month review date, first Friday after hire date, and last day of the
hire month for all employees who have been employed for more then
15 years.
32. if employee does not report to a manager then print 'no manager'
else print 'reports to manager and manager_name'.
LAST_NAME MGR_NAME
------------------------- --------------------------------------
Urman reports to ==Greenberg
Vargas reports to ==Mourgos
Vishney reports to ==Errazuriz
Vollman reports to ==King
33. Display those employees who are getting more then '$15000,00'. i
have to compare the salary column in this format ('$15000.00') only.
SQL> select salary from employees wheresalary > to_number ('$15000.00','$99999.99');
HIRE_DATE
---------------------------------------------------------------------------
twenty-three-november of nineteen ninety-nine
nineteen-march of nineteen ninety-nine
twenty-four-january of two thousand
twenty-three-february of two thousand
twenty-four-march of two thousand
36. Display employees’s first name and last name and sort them by
last_name.
SQL> select first_name, last_name from employees order by last_name;
37. Display full name of employees and full name his manager.
38. Display a list full name of employees and their salaries . Display
only top 5 employees.
47. Increase the salary range of all jobs from executive department by
20%..
SQL> select e.salary,e.salary*0.20 "incr_sal by 20%",d.department_id from employees e join
departments d on (d.department_id=e.department_id) where department_name='Executive' ;
48. Display a list of departments and their managers along with their
phone_number. Do not display country code of phone_number.
SQL> select case length(phone_number) when 12 then phone_number else
substr(phone_number,8) end from employees;
49. List of the employees and their salaries whose salary is less than the
average salary of the company.
SQL> select last_name, salary from employees where salary<(select avg(salary) from employees);
50. List of the employees and their salaries whose salary is more than
the average salary of the company.
SQL> select last_name, salary from employees where salary>(select avg(salary) from employees);
51. Display the list of employees who have a and e in their name.
SQL> select first_name from employees where first_name like '%a%' and first_name like'%e%';
53. Display the jobs that are found in the Administration and Executive
departments.Also display the no of employees for these jobs. Show
the job with the highest no of employee first.
SQL> select e.job_id ,count(e.employee_id) employee from employees e join departments d
on(d.department_id=e.department_id) where d.department_name
in('Administration','Executive') group by job_id order by employee desc;
54. Show all employees who were hired in the first half of the
month(before the 16th of the month).
SQL> select employee_id,last_name, hire_date from employees where
to_number(to_char(hire_date,'dd'))<16;
56. Create a table named dept using departments table, and truncate dept
and then use the merge statement to get the data back.
step1) Create a table named dept using departments table -
SQL> create table dept as select * from departments;
57. Create two tables emp1 and dept1 and establish a relationship
between both tables.
{we create relationship between table emp1 & dept1 with (primary key & foreign key)
relationship}
step1)
SQL> CREATE table emp1(EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25),
EMAIL VARCHAR2(25), PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE,JOB_ID VARCHAR2(10),SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),MANAGER_ID NUMBER(6), DEPARTMENT_ID
NUMBER(4));
step2)
SQL> CREATE table dept1(DEPARTMENT_ID NUMBER(4),
DEPARTMENT_NAME VARCHAR2(30),MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4));
step3)
SQL> ALTER table dept1 add constraint dept1_pk primary key(department_id);
step4)
SQL> ALTER table emp1 add constraint emp1_fk foreign key(department_id) references
dept1(department_id);
58. Drop the table employees and the get back it using FLASHBACK
statement from recyclebin.
step1)
SQL> DROP table employees;
step2)
SQL> FLASHBACK table employees to before drop;
61. Display City and total no. of employees in that city. List only top 5
cities
SQL> select * from (SELECT L.CITY,COUNT(E.EMPLOYEE_ID) FROM EMPLOYEES E
JOIN DEPARTMENTS D ON(D.DEPARTMENT_ID=E.DEPARTMENT_ID) JOIN
LOCATIONS L ON(L.LOCATION_ID=D.LOCATION_ID) GROUP BY L.CITY ORDER BY
COUNT(E.EMPLOYEE_ID) DESc) where rownum <=5;
65. Display region and total number of employees from that region
SQL> select r.region_name ,count(e.employee_id) from regions r join countries c
on(r.region_id=c.region_id) join locations l on(c.country_id=l.country_id) join departments d
on(l.location_id=d.location_id) join employees e on(d.department_id=e.department_id) group
by r.region_name ;
66. Display City and total no. of employees in that city. List only top 5
cities.
SQL> select * from (select l.city, count(e.employee_id) from locations l left join departments d
on(l.location_id=d.location_id) left join employees e on(d.department_id=e.department_id) group
by l.city order by count(e.employee_id) desc) where rownum <= 5;
Or
67. give the info of those employees whose manager belong to the same
city as of employees?
68. write a sql query such that it will display employees name there
manager name and there manager's manager name. and make sure it
will display all employees name (107 rows) if any employee is not
having any manager then it will display "no manager"?
69. write a query which will display the details of those employees who
belong to the same region as of employee 149?
Select e.employee_id, e.last_name, e. salary, r.region_id, r.region_name from employees e join
departments d on(d.department_id=e.department_id) join locations l
on(l.location_id=d.location_id) join countries c on(c.country_id=l.country_id) join regions r
on(r.region_id=c.region_id) where region=(select r.region from employees e join departments d
on(d.department_id=e.department_id) join locations l on(l.location_id=d.location_id) join
countries c on(c.country_id=l.country_id) join regions r on(r.region_id=c.region_id) where
e.employee_id=149);
70. write a query to count total no of character 'e' in last_name and first
name of the employees table?
Select first_name, regexp_count(first_name,’e’), last_name, regexp_count(last_name,’e’) from
employees;
71. write a query to sort employees table day wise from tuesday to
monday?
SQL> select days from (select mod(to_char(hire_date,'d')+4,7) ,to_char(hire_date,'day') days from
employees order by 1);
72. write a query to see which department_id are not used in employees
table.(write atleast 3 ways to solve the same question)?
SQL>Select department_id from departments minus select department_id from employees;
SQL> select d.department_id from departments d where d.department_id not in(select
department_id from employees where department_id=d.department_id);
73. write a query to dispaly those employees who are having salary less
than the salary of any employee of department 80(write atleast two
query)?
SQL> Select employee_id , salary from employees where salary <any (select salary from employees
where department_id=80);
SQL> Select salary from employees where salary>(select max(salary) from employees where
department_id=80);
74. prompt a two date from a user see how many weeks there between
these two dates?
Select round((to_date(‘&Max_date’)-to_date(‘&min_date’)/7) from dual;
75. write a query to replace only second occurrence of character 'e' with
'#' in last_name of employees table?
Select regexp_replace(last_name,’e’,’#’1,2) last_name from employees;
76. write a query to display total number of primary key present in your
schema?
Select count(constraint_name) from user_constraints where constraint_type=’P’;
79. prompt job_title from user and show the details of employees who
are working in that department?(dont use join).
Select department_id , employee_id, last_name , job_id from employees where job_id=’&Job_title’;l
80. grant any user such that he can only select your employees table
without using your schema name?
Create public synonym local_emp for employees;
83. prompt a number from user and show that numbers salary from each
department?
SQL> select department_id, salary, rank from (select distinct department_id ,salary, dense_rank() over
(partition by department_id order by salary desc) rank from employees order by 3) where rank
=&number;
85. display last_name like but total length should be exactly 50?
***************last_name****************
SQL> select rpad(lpad(last_name,length(last_name)+21,'#'),50,'#') from employees;
89. write a query to display how many employees got hired in each
month of year 1996?
SQL> select to_char(hire_date,'Mon'),count(employee_id) from employees where
to_char(hire_date,'yyyy')=1996 group by to_char(hire_date,'Mon');
90. create a view of employee table such that no one can perform dml
operations on that view?
Create or replace view emp as (select * from employees) with read only;
91. fire some sql statements such that it will raise an dead lock error?
U1 => User1 and U2 => User2 ,
With the help of Given table obtain Output with company name
“PRODUCT’s”:
Company_name P_Group_name P_name
------------------------- ---------------------------- -----------------------
PRODUCTS Softdrink,Colddrink pepsi,Miranda,7up,Sprite
97. List the employees who joined in 1981 with the job same as the
most senior person of the year 1981.
Select * from employees where job in (select job from employees where hire_date in (select
min(hire_date) from employees where to_char(hire_date,’yyyy’)=’1981’));
98. List the employees who salary is equal to the average of maximum
and minimum.
Select * from employees where salary =(select (max(salary)+ min(salary))/2 from employees);
100. List the employees whose first 2 charcter from hire date =last 2
charcter of salary.
select * from employees where substr(hire_date,1,2)= substr(salary,length(salary)-1, length(salary));
101. Employee hired on or before 15 of any month are paid on the last
th
Friday of that month those hired after 15 are paid on the first Friday
th
of the following month.print a list of employee their hire date and the
first pay the date.sort on the hire date.
Select first_name,hire_date,next_day(last_day(hire_date),’FRIDAY’)-7 from employees where
tp_char(hire_date,’dd’) <=15
union
Select first_name,hire_date,next_day(last_day(hire_date),’FRIDAY’) from employees where
tp_char(hire_date,’dd’) >15 ;
102. Find average salary and average total remuneration for each job
type.
Select avg(salary),avg(salary+nvl(commission_pct,0)) from employees
103. Check whether all the employees number are indeed unique
Select employee_id,count(*) from employees group by employee_id;
104. Find out the job that was filled in the falf of 1993 and same job that
was filled during the same period of 1984.
Select * from employees where to_cahr(hire_date,’mm’) <=06 and to_cahr(hire_date,’yyyy’) =1984)
and job in (select job from employees where to_cahr(hire_date,’mm’) <=06 and
to_cahr(hire_date,’yyyy’) <=1983);
105. List the manager who are senior to king and who are junior to
smith
Select * from employees where employee_id in(select manager_id from employees where hire_date
<(select hire_date from employee where first_name=’KING’) and hire_date > ( select hire_date from
employees where first_name=’SMITH’ ) and manager_id is not null;
107. list the employee who are working for department number 10 or 20
with desg as clerk or analyst with a salary is either 3 or 4 digits with
an experience>8yearsbut does not belong to mons of mar,apr,sep and
working gor manager and numner is not ending with 88 and 56.
Select * from employees where department_id in (10,20) and job in (‘CLERK’,’ANALYST’) and
(length(salary) in (3,4)) and ((months_between(sysdate,hire_date))/12)>8 and to_char(hire_date,’mon’)
not in (‘mar’,’sep’,’apr’) and (manager_id not like ‘%88’ and manager_id not like ‘%56’);
===================================================
============================================
Table_name – Employee_Detail
Employee_id First_Name Last_Name Salary Joining Date Department Gender
1 Vikas Jain 27000 2000-07-19 MEC M
2 Nikita Das 22500 1998-09-19 IT F
3 Ashish Kumar 9000 2004-12-19 CE M
4 Navin Sharma 19000 2009-05-19 MEC M
5 Sonali Gupta 17500 2012-02-19 ELE F
109. Display first name and Gender as M/F.(if male then M, if Female
then F).
select First_Name, case when Gender = 'Male' then 'M' when gender = 'female' then 'F'
end from Employee_Detail ;
110. To generate the even/odd numbers from dual. for eg..generate odd
numbers between 1 to 10. or generate even numbers between 1..10.
select * from (select case when mod(rownum,2)=0 then rownum end even from dual connect by
rownum <=&num) where even is not null ;
select * from (select case when mod(rownum,2)=1 then rownum end odd from dual connect by rownum <=&num)
where odd is not null ;
111. To find the sum of negative numbers and the sum of the positive
numbers in two separate columns.
Table_name – Neg_Pos
number
-1
-2
-3
1
2
3
select sum(case when number < 0 then num else 0 end) neg, sum(case when number > 0 then num else 0
end)pos from Neg_Pos ;
113. To find those employees who are more than 21 years of age.
select name from Find_Age where dob < (select add_months(sysdate,-(12*21)) from dual);
114. To find the missing number between the minimum and maximum
number for each id. text column can be ignored.
Test_Number
Id Name Text
1 1 aa
1 3 cc
1 7 gg
1 11 kk
2 1 ll
2 2 mm
2 3 nn
2 6 qq
3 2 bb
3 5 ee
Test_Number_Min_Max
Id Min Max
1 1 11
2 1 5
3 1 2
output :
ID SEQ
12
14
15
16
17
19
1 10
24
25
31
33
34
select r id ,l seq from (select level l from dual connect by level <13), (select level r from dual connect by
level <4), Test_Number_Min_Max where r=id and l>=mn and l<=mx and (r,l) not in (select id,seq from
Test_Number);
115. Beginner question based on the above logic. From the table given
below, all the numbers should be on the first column and the
alphabets on the second column.
ALPA RANK
==============
a1
b2
c4
x5
y6
z8
9g
0f
7e
à select p.project_name , e.first_name from project_details p inner
join employee_details e
on p.employe_id = e.id where p.project_name in(select project_name
from project_details group by project_name having count(1)>1) ;All
the alphabets on column B and all numbers in column A
OUTPUT:
AB
——–
0f
1a
2b
3d
4c
5x
6y
7e
8z
9g
116. Write down the query to fetch Project Name on which more than
one employee are working along with Employee Name
Table_name – EmployeeDetail
Employee_id First_Name Last_Name Salary Joining Date Department Gender
1 Vikas Jain 27000 2000-07-19 MEC M
2 Nikita Das 22500 1998-09-19 IT F
3 Ashish Kumar 9000 2004-12-19 CE M
4 Navin Sharma 19000 2009-05-19 MEC M
5 Sonali Gupta 17500 2012-02-19 ELE F
Project _Details
Project_id Employee_id Project_Name
1 1 Task Track
2 1 CLP
3 1 Survey Management
4 2 HR Management
5 3 Task Track
6 3 GRS
7 3 DDS
8 4 HR Management
9 6 GL Management
117. rite down the query to fetch employee_name & project who has
assign more than one project.
119. Write down the query to fetch Project_name assign to more than
one Employee.
Select project_name,Count(*) from Project_details group by project_name having count(*)>1;
122. Get the first_name, current _date, joining_date and diff between
current_date and joining-date in days.
select first_name, sysdate, joining_date, datediff(dd,joining_date,sysdate) as total_months
from employee_details;
123. Get the first_name, current _date, joining_date and diff between
current_date and joining-date in months.
select first_name, sysdate, joining_date, date, datediff(mm,joining_date,sysdate) as total-
months from employee_details;
127. Create a table named dept using departments table, and truncate
dept and then use the mergestatement to get the data back.
create table dept as select * from departments;
truncate table dept;
128. Show all employees who were hired in the first half of the month
(before the 16th of the month).
select employee_id,last_name, hire_date from employees where to_number(to_char(hire_date,'dd'))<16;
138. Display employee name start with g and end with e and also shows
position of second e but without using like operator.
Select last_name,instr(last_name,’e’,1,2) from employees where substr(last_name,1,1)=’G’ and
substr(last_name,1,1)=’e’ ;
140. Write a program length of name should be same but it end with
character e.
Select first_name, last_name from employees where length(first_name)= length(last_name) and last_name
like %e% ;
153. Create a query that will display the total number of employee hired
in 1995,1996.
Select to_char(hire_date,’yyyy’),count(employee_id) where to_char(hire_date,’yyyy’) in (1995,1996)
group by to_char(hire_date,’yyyy’) order by to_char(hire_date,’yyyy’) ;
154. Display the manager number and salary of the lowest paid
employee for that manager,avg(sal)<6000.sort the output is
ascending order
Select manager_id from employees having avg(salary)<6000 group by manager_id order by manager_id
;
157. Using decode function grade of employee based on the value of the
column job_id as per the following data. AD_PRES -A
ST_MAN -B
IT_PROG - C
SA_REP - D
Other -0
Select job_id,decode(job_id,’ AD_PRES ‘ , ‘A’ ,
‘ST_MAN’, ‘ B ‘ ,
‘ IT_PROG , ‘C’ ,
‘SA_REP’ ,’ D’ ,
‘Other ‘ ,‘0’ ) from employees group by job_id ;
163. Display the details of employee who work in the same department
& have the same manager as John.
Select * from employees where manager_id in (Select manager_id from employees where
first_name=’John’) and department_id in (Select department_id from employees where
first_name=’John’) ;
164. Find the employee who earn the same salary as the salary for
department =90 & department =50
Select job_id,salary from employees where salary in (select salary from employees where department_id
in (90,50) ;
167. Find the employee who earn more than avg(salary) in their
department.
Select last-name,salary,department_id from employees outer where salary > (select avg(salary) from
employees where department_id=outer. department_id ) ;
168. Find the employee who has at least one person reporting to them.
(exist operator)
Select employee_id,last_name from employees outer where exists (select ‘a’ from employees where
employee_id=outer.employee_id ) ;
179. Display each months start and end date upto last month of the year
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), i) start_date, TRUNC (LAST_DAY
(ADD_MONTHS (SYSDATE, i))) end_date FROM XMLTABLE ('for $i in 0 to xs:int(D) return $i'
PASSING XMLELEMENT (d,FLOOR (MONTHS_BETWEEN (ADD_MONTHS (TRUNC
(SYSDATE, 'YEAR') - 1, 12),SYSDATE)))COLUMNS i INTEGER PATH '.');
(OR)
SELECT * FROM employees MINUS SELECT * FROM employees WHERE ROWNUM <(SELECT
COUNT (*) FROM employees);
Table 1:
SELECT * FROM PRODUCTS;
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
Table 2:
SELECT * FROM SALES;
183. Write a SQL query to find the products which have continuous
increase in sales every year?
SELECT PRODUCT_NAME
FROM
(
SELECT P.PRODUCT_NAME,
S.QUANTITY -
LEAD(S.QUANTITY,1,0) OVER (
PARTITION BY P.PRODUCT_ID
ORDER BY S.YEAR DESC
) QUAN_DIFF
FROM PRODUCTS P,
SALES S
WHERE P.PRODUCT_ID = S.PRODUCT_ID
)A
GROUP BY PRODUCT_NAME
HAVING MIN(QUAN_DIFF) >= 0;
184. Write a SQL query to find the products which does not have sales
at all?
SELECT P.PRODUCT_NAME FROM PRODUCTS P LEFT OUTER JOIN SALES S
ON(P.PRODUCT_ID = S.P
Or
SELECT P.PRODUCT_NAME FROM PRODUCTS P WHERE P.PRODUCT_ID NOT IN(SELECT
DISTINCT PRODUCT_ID FROM SALES);
Or
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
WHERE P.PRODUCT_ID NOT IN
(SELECT DISTINCT PRODUCT_ID FROM SALES);
Or
SELECT P.PRODUCT_NAME FROM PRODUCTS P
WHERE NOT EXISTS (SELECT 1 FROM SALES S WHERE S.PRODUCT_ID = P.PRODUCT_ID);
185. Write a SQL query to find the products whose sales decreased in
2012 compared to 2011?
SELECT P.PRODUCT_NAME
FROM PRODUCTS P,
SALES S_2012,
SALES S_2011
WHERE P.PRODUCT_ID = S_2012.PRODUCT_ID
AND S_2012.YEAR = 2012
AND S_2011.YEAR = 2011
AND S_2012.PRODUCT_ID = S_2011.PRODUCT_ID
AND S_2012.QUANTITY < S_2011.QUANTITY;
186. Write a query to select the top product sold in each year?
SELECT PRODUCT_NAME,
YEAR
FROM
(
SELECT P.PRODUCT_NAME,
S.YEAR,
RANK() OVER (
PARTITION BY S.YEAR
ORDER BY S.QUANTITY DESC
) RNK
FROM PRODUCTS P,
SALES S
WHERE P.PRODUCT_ID = S.PRODUCT_ID
)A
WHERE RNK = 1;
188. Write a query to display only Friday dates from Jan, 2000 to till
now?
SELECT C_DATE, TO_CHAR(C_DATE,'DY') FROM (SELECT TO_DATE('01-JAN-2000','DD-
MON-YYYY')+LEVEL-1 C_DATE FROM DUAL CONNECT BY LEVEL <=(SYSDATE -
TO_DATE('01-JAN-2000','DD-MON-YYYY')+1)) WHERE TO_CHAR(C_DATE,'DY') = 'FRI';
189. Write a query to duplicate each row based on the value in the repeat column? The input table data looks like as
below.
Products Repeat
--------- -------
A, 3
B, 5
C, 2
Now in the output data, the product A should be repeated 3 times, B should be repeated 5 times and C should be
repeated 2 times. The output will look like as below
Products Repeat
-------- --------
A 3
A 3
A 3
B 5
B 5
B 5
B 5
B 5
C 2
C 2
The next example outputs the Julian Day; the number of days since 31
December 4713 BC of the given date.
SELECT TO_CHAR( TO_DATE('28-11-2942') , 'fmJ' ) "Julian
Day" FROM dual ;
SELECT TO_CHAR( TO_DATE('28-11-1942') , 'fmBC' ) "BC
Indicator" FROM dual ;
SELECT TO_CHAR( TO_DATE('28-11-1942') , 'fmBC' ) "AD
Indicator" FROM dual ;
SELECT ename, hiredate, TO_CHAR(hiredate, 'fmCC') FROM
EMP ;
SELECT TO_CHAR( TO_DATE('18-09-1972') , 'w' ) "Week of
Month" FROM dual ;
SELECT TO_CHAR( TO_DATE('18-09-1972') , 'ww' ) "Week of
Year" FROM dual ;