Sei sulla pagina 1di 15

Query to select distinct values without using distinct

1.
SELECT LASTNAME, FIRSTNAME
FROM EMP
GROUP BY LASTNAME, FIRSTNAME
We Have 2 ways To Answer This Question
1.Using UNIQUE
2.Using GROUP BY

How to retrieve 2nd highest sal in each departement from emp


and dept tables using GROUP BY?
Code
1.

SELECT

2.

tab.department_name,

3.

MIN(tab.salary) AS Second_Max_Sal

4.

FROM

5.

6.

SELECT

7.

e.first_name,

8.

e.salary,

9.

d.department_name,

10.

dense_rank() over (partition BY d.department_name ORDER BY


e.salary) AS

11.

12.

rank

FROM

13.

hr.departments d

14.

JOIN hr.employees e USING (department_id)

15.

16.

tab

17.

WHERE

18.

19.

20.

rank BETWEEN 1 AND 2

GROUP BY

tab.department_name

1.

SELECT e.DeptNo, MAX(e.Sal),d.DeptName Salary

2.

FROM Emp e left outer join dept d ON e.DeptNo=d.DeptNo

3.

WHERE e.Sal <

4.

(SELECT MAX(Sal)

5.

FROM Emp

6.

WHERE DeptNo = e.DeptNo)

7.

GROUP BY e.DeptNo,d.DeptName

What are the advantages and disadvantages of View?

sbagai2001
May 26th, 2006
25
41036
Questions by sbagai2001 answers by sbagai2001

SQL
Answer

First
Prev
Next
Last

Editorial / Best Answer


Answered by: Mohan

Jun 2nd, 2006

Hi,
Advantages of views:
1. View the data without storing the data into the object.
2. Restict the view of a table i.e. can hide some of columns in the tables.
3. Join two or more tables and show it as one object to user.
4. Restict the access of a table so that nobody can insert the rows into the table.
Disadvatages:
1. Can not use DML operations on this.
2. When table is dropped view becomes inactive.. it depends on the table objects.
3. It is an object, so it occupies space.
Pls. add , if I miss any of them.
Thanks,
Mohan

Truncate and Delete concept


Hi,
Let us take a scenario such that I issue a delete on table emp and truncate on table emp1 which is exact
replica (in terms of both structure and data) of emp table.Now , I issue a commit on both sessions.
If I do a select * from emp and select * from emp1 now, which one should execute faster and why?

truncate is DDL where as Delete is DML. You cannot use where clause in Truncate and cannot retrieve
data once applied. Delete can use where clause and roll back data if Commit is not used. Delete is
preferred as developer have clarity that which data have to delete. Truncate clears all data which may be
needed for others.
Internally Delete statement will not clears row nums even if record is deleted which will cause
performance issue when deleted data is too much. Truncate clears row num too
Delete would leave "log" statements about every deleted thing and truncate wont. Hence truncate would
be faster.

The student who got >=40 student is passes and


S_NAME MARKS
ramu 60
ramu 40
ramu 50
karan 30
karan 96
karan 46
out put:
ramu "PASS"
karan "FAIL"
Code
1.
SELECT NAME,pass FROM (

2.

SELECT DISTINCT NAME,pass FROM STD WHERE MARKS>=40

3.

UNION

4.

SELECT DISTINCT NAME,fail FROM STD WHERE MARKS<40) GROUP BY NAME


HAVING COUNT(*)=1

5.

UNION

6.

SELECT NAME,fail FROM (

7.

SELECT DISTINCT NAME,pass FROM STD WHERE MARKS>=40

8.

UNION

9.

SELECT DISTINCT NAME,fail FROM STD WHERE MARKS<40) GROUP BY NAME


HAVING COUNT(*)<>1

Write a query to display alternate records from the employee


table?

kowmudiswarna

Oracle SQL Interview Questions

mradul

Dec 22nd, 2014

I use this for even records !


Gaurav gg

Aug 20th, 2014

SELECT * FROM tab_name WHERE mod(primary_key_col,2)=0; -- for even records


SELECT * FROM employees WHERE mod(employee_id,2)=0; -- for even records
SELECT * FROM employees WHERE mod(employee_id,2)=1; -- for odd records

What is difference between a formal and an actual parameter?


The variables declared in the procedure and which are passed, as arguments are called actual, the
parameters in the procedure declaration. Actual parameters contain the values that are passed to a
procedure and receive results. Formal parameters are the placeholders for the values of actual
parameters

How can i disable a trigger trough sql prompt.


to enalbe or disable specific trigger
Alter trigger triggername enable/disable
To enalbe or disable all triggers on a table
Alter table tablename disable / enable all triggers

How to find Nth largest or Nth smallest data from oracle table, for
ex..5th highest salary from employees
To get the nth largest unitprice from products table:
SELECT distinct p1.unitprice
from products p1
where
(select count(*)+1 from products p2 where
p2.unitprice > p1.unitprice) = {n}
order by p1.unitprice desc

To get the nth smallest unitprice from products table:


SELECT distinct p1.unitprice
from products p1
where
(select count(*)+1 from products p2 where
p2.unitprice < p1.unitprice) = {n}
order by p1.unitprice asc

try this query


1.this is for nth highest no
select distinct(a.sal) from emp a where &n=(select count(distinct(b.sal)) from emp b where a.sal<=b.sal);
2.this is for nth lowest no
select distinct(a.sal) from emp a where &n=(select count(distinct(b.sal)) from emp b where a.sal>=b.sal)

What is Cartesian product in the SQL?


Cartesian Products: If two tables in a join query have no join condition, Oracle returns their Cartesian product.
Oracle combines each row of one table with each row of the other. A Cartesian product always generates many rows
and is rarely useful. A Cartesian product is formed when: A join condition is omittedA join condition is
invalid All rows in the first table are joined to all rows in the second table

To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

How can a TRIGGER be activated on demand?


Code
1.

create or replace trigger prac_trig_emp1

2.

before insert or update OR DELETE

3.

ON emp1

4.

REFERENCING NEW AS NEW OLD AS OLD

5.

FOR EACH ROW DISABLE

6.

BEGIN

7.

IF INSERTING THEN

8.

INSERT INTO emp1_aud (a) VALUES (Insert)

9.

ELSIF DELETING THEN

10.

INSERT INTO emp1_aud (a) VALUES (Delete)

11.

ELSIF UPDATING THEN

12.

INSERT INTO emp1_aud (a) VALUES (Update)

13.

END IF

14.

END prac_trig_emp1

15.

16.

17.

ALTER TRIGGER prac_trig_emp1 enable

18.

ALTER TRIGGER prac_trig_emp1 disable

Is it possible to update Views? If yes, How, If Not, Why?


This is rahul.As per discussion on views. I want to share some thing. There are two types of views namely
Simple and complex views.
U can perform DML operation on simple views which is based on single table and that view doesn't
contain any single row function and any group by clause and it has to satiesfy integrity constraint also.
U can't perform DML operation on complex views becuase they are based on multiple table. U can achive
this task by using Triggers(Instade of...).
If u have any suggestion on bellow discussion please send mail to rahul_omanwar@hotmail.com.
Thanks and regards,

How do you view the last record added to a table?


select * from test where rowid = ( select max(rowid) from test);
test is table name. Replace test with appropriate table name.

1. to find out column name using table name.


2. Then we can take corresponding column name from above results.
Ex:
Select column_name from all_tab_columns where table_name='countries';
The Output will be:
COLUMN_NAME
------------------------COUNTRY_ID

COUNTRY_NAME
REGION_ID

Then we wil use column names what u need from above list;
Ex:
select * from countries where country_id=91;

Select from table without using column name


How can I select column values from a table without knowing the column name ?
Suppose , select employee_id from employees , now I don't know the column name and I want to select
the column.
Please let me know the answer

What is the exact diff between 1nf,2nf,3nf,4nf and bcnf?

Can we have a commit statement inside a trigger? if no why cant


we?
Yes ,You can Commit inside the trigger.
But for this you have to make this trigger transaction to be a Independent transaction from its parent
transaction, You can do this by using Pragma. Pragma AUTONOMOUS_TRANSACTION allow you to
build the Indepadent(child) Transaction,started by another. Shold be declare in DECLARE section of any
subprogram.

Question is been asked in the interview., I have table Department with


details likeDeptId Dname Dlocation10 Finance Del20 Sales Mum30
Marketing BloreThe output should be in this format 10 20 30 Finance
Sales Marketing Del Mum BloreThe query I need in SQL not using any
transformation.Can anyone help me out in this ?
using decode function u can do that easily....
let me give u an example of 2 rows n 2 column
x y
1 a
2 b

select decode(y,a,1,b,a) x,decode(y,a,2,b,b) y from table;


result will be like this
x y
1 2
a b
select deptno,
max(decode(rn,1,ename))||
max(decode(rn,2,','||ename))||
max(decode(rn,3,','||ename))||
max(decode(rn,4,','||ename))||
max(decode(rn,5,','||ename))||
max(decode(rn,6,','||ename)) ename, loc
from (select emp.deptno, ename, loc,
row_number () over (partition by emp.deptno order by ename) rn
from emp, dept where emp.deptno = dept.deptno)
group by deptno, loc

How centralized DBMS differs from distributed DBMS


A centralized database has all its data at one place so there may occur problems of data avalability, and a
system crash may lead to whole dataloss. In a distributed database, database is stored on several
computers and they communicates using networks. users from any location can issue commands to
retrieve data and it will not affect the working of the database. Failure of one of the site will not make the
whole sysstem useless.

Is there any function in oracle to check whether a value is


numeric or not. I have a column which contains both numeric
and alphanumeric data. I want to fetch only the numeric data
from that column. Can anyone tell me how to do it?
There does not exists any build in function which checks for a numeric value instead you can write your
own function as follows.
CREATE OR REPLACE FUNCTION Isnumber(p_num VARCHAR2) RETURN NUMBER
AS
a NUMBER;
BEGIN
a := p_num;
RETURN 1;
EXCEPTION WHEN OTHERS THEN
RETURN 0;

END;
/
This function can now be called in a SQL query which will retum 1 if the value passwd is numeric else it
will return 0.
Thanks & Regards,
Krishnakant.

When do you use WHERE clause and when do you use HAVING
clause?
HAVING clause is used when you want to specify a condition for a group function and it is written after
GROUP BY clause. The WHERE clause is used when you want to specify a condition for columns, single
row functions except group functions and it is written before GROUP BY clause if it is used.
What are Pair Wise & Non-Pair Wise Columns?

Vinod Tuteja

Jun 6th, 2013

NON PAIRWISE COMPARISON SUBQUERY:


Code
1.

2.

3.

4.

5.

6.

7.

8.

SELECT employee_id, manager_id, department_id

FROM employees

WHERE manager_id IN

(SELECT manager_id

FROM employees

WHERE first_name = John)

AND department_id IN

(SELECT department_id

9.

FROM employees

10.

11.

WHERE first_name = John)

AND first_name != John;

Vinod Tuteja

Jun 6th, 2013

NON PAIRWISE COMPARISON SUBQUERY:


Code
1.

2.

3.

4.

5.

6.

7.

SELECT employee_id, manager_id, department_id

FROM employees

WHERE manager_id IN

(SELECT manager_id

FROM employees

WHERE first_name = John)

AND department_id IN

8.

(SELECT department_id

9.

FROM employees

10.

11.

WHERE first_name = John)

AND first_name != John;

PAIRWISE COMPARISON SUBQUERY:


Code
1.

SELECT employee_id, manager_id, department_id

2.

3.

FROM employees

WHERE (manager_id, department_id) IN

4.

(SELECT manager_id, department_id

5.

FROM employees

6.

7.

WHERE first_name = John)

AND first_name != John;

Display the number value in Words?


SQL> select sal, (to_char(to_date(sal,'j'), 'jsp'))
from emp;
the output like,
SAL (TO_CHAR(TO_DATE(SAL,'J'),'JSP'))
--------- ----------------------------------------------------800 eight hundred
1600 one thousand six hundred
1250 one thousand two hundred fifty
If you want to add some text like, Rs. Three Thousand only.
SQL> select sal "Salary ",
(' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.'))
"Sal in Words" from emp

/
Salary Sal in Words
------- -----------------------------------------------------800 Rs. Eight Hundred only.
1600 Rs. One Thousand Six Hundred only.
1250 Rs. One Thousand Two Hundred Fifty only.

I am writing a script to update a value in an Oracle table. The


script has a 'From' clause which Oracle does not like. Is there
another way to write this since I am bringing data from 3 different
tables.ThanksSamir

Left Outer & Right Outer Join


What is the use of Left Outer & Right Outer Join, Using Equi Join with Union we can do the outer join then
why we need to go for Outer Join

Insert a record in two tables


How to insert a record in two tables with single insert statement ?

Nazeera Jaffar

Sep 30th, 2012

Answer:
Code
1.

2.

3.

INSERT ALL

INTO first_table VALUES(value1,value2)

INTO second_table VALUES(value1,value2)

4.

5.

SELECT * FROM dual;

Display Middle Record


How to display middle record in a given table?
BELOW example will give the middle row of the table(not sorted).
eg:
SELECT * FROM EMP WHERE ROWNUM <=(SELECT COUNT(1)/2 FROM EMP)
MINUS
SELECT * FROM EMP WHERE ROWNUM <(SELECT COUNT(1)/2 FROM EMP)

Potrebbero piacerti anche