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

DeptNo. JOIN hr. hr. ) 16.DeptName What are the advantages and disadvantages of View? . (SELECT MAX(Sal) 5. WHERE DeptNo = e. rank BETWEEN 1 AND 2 GROUP BY tab. FROM Emp 6. GROUP BY e.employees e USING (department_id) 15. 20. FROM Emp e left outer join dept d ON e.departments d 14. WHERE e.DeptName Salary 2.d.DeptNo 3.DeptNo.13. MAX(e.department_name 1.DeptNo) 7. WHERE 18.d.DeptNo=d.Sal < 4. tab 17. SELECT e.Sal). 19.

add . if I miss any of them. When table is dropped view becomes inactive. 2. which one should execute faster and why? . View the data without storing the data into the object. I issue a commit on both sessions. 2. 4. Disadvatages: 1.    sbagai2001 May 26th..Now . 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. it depends on the table objects.e. 2006 25 41036 Questions by sbagai2001 answers by sbagai2001 SQL Answer     First Prev Next Last Editorial / Best Answer Answered by: Mohan  Jun 2nd. Can not use DML operations on this. If I do a select * from emp and select * from emp1 now. can hide some of columns in the tables. Pls. Restict the view of a table i. so it occupies space. Thanks. It is an object. Restict the access of a table so that nobody can insert the rows into the table. 2006 Hi. Mohan Truncate and Delete concept Hi. 3. Join two or more tables and show it as one object to user. 3. Advantages of views: 1.

pass FROM STD WHERE MARKS>=40 8. UNION .fail FROM STD WHERE MARKS<40) GROUP BY NAME HAVING COUNT(*)=1 5. SELECT NAME.pass FROM STD WHERE MARKS>=40 3. Truncate clears row num too Delete would leave "log" statements about every deleted thing and truncate wont. UNION 6. Delete is preferred as developer have clarity that which data have to delete. 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 all data which may be needed for others. Delete can use where clause and roll back data if Commit is not used. 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 DISTINCT NAME. SELECT DISTINCT NAME. SELECT DISTINCT NAME. You cannot use where clause in Truncate and cannot retrieve data once applied.pass FROM ( 2. SELECT NAME.truncate is DDL where as Delete is DML. UNION 4.fail FROM ( 7. Hence truncate would be faster.

SELECT DISTINCT NAME.2)=0.2)=0. 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 .9. Formal parameters are the placeholders for the values of actual parameters How can i disable a trigger trough sql prompt.for even records SELECT * FROM employees WHERE mod(employee_id. -.for even records SELECT * FROM employees WHERE mod(employee_id. Actual parameters contain the values that are passed to a procedure and receive results. 2014 I use this for even records ! Gaurav gg  Aug 20th. -. as arguments are called actual. 2014 SELECT * FROM tab_name WHERE mod(primary_key_col.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.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. -. the parameters in the procedure declaration.

for ex.unitprice) = {n} order by p1. 2.unitprice > p1.sal) from emp a where &n=(select count(distinct(b.sal<=b.this is for nth lowest no select distinct(a.unitprice) = {n} order by p1..unitprice < p1.unitprice asc try this query 1.sal) .unitprice desc To get the nth smallest unitprice from products table: SELECT distinct p1.How to find Nth largest or Nth smallest data from oracle table.sal)) from emp b where a.this is for nth highest no select distinct(a.sal)) from emp b where a.unitprice from products p1 where (select count(*)+1 from products p2 where p2.unitprice from products p1 where (select count(*)+1 from products p2 where p2.sal) from emp a where &n=(select count(distinct(b.sal).5th highest salary from employees To get the nth largest unitprice from products table: SELECT distinct p1.sal>=b.

ELSIF UPDATING THEN 12. always include a valid join condition in a WHERE clause. FOR EACH ROW DISABLE 6. create or replace trigger prac_trig_emp1 2. BEGIN 7. REFERENCING NEW AS NEW OLD AS OLD 5. ELSIF DELETING THEN 10. A Cartesian product always generates many rows and is rarely useful. INSERT INTO emp1_aud (a) VALUES (Delete) 11. Oracle combines each row of one table with each row of the other. INSERT INTO emp1_aud (a) VALUES (Insert) 9. ON emp1 4. before insert or update OR DELETE 3. INSERT INTO emp1_aud (a) VALUES (Update) . Oracle returns their Cartesian product.What is Cartesian product in the SQL? Cartesian Products: If two tables in a join query have no join condition. IF INSERTING THEN 8. How can a TRIGGER be activated on demand? Code 1.• A Cartesian product is formed when:– A join condition is omitted–A join condition is invalid– All rows in the first table are joined to all rows in the second table • To avoid a Cartesian product.

END IF 14. If u have any suggestion on bellow discussion please send mail to rahul_omanwar@hotmail. 17.. U can't perform DML operation on complex views becuase they are based on multiple table. to find out column name using table name. 2. ALTER TRIGGER prac_trig_emp1 enable 18. ALTER TRIGGER prac_trig_emp1 disable Is it possible to update Views? If yes. test is table name.13. I want to share some thing. If Not. 16. Ex: Select column_name from all_tab_columns where table_name='countries'..com. Thanks and regards. 1. How do you view the last record added to a table? select * from test where rowid = ( select max(rowid) from test). Why? This is rahul. U can achive this task by using Triggers(Instade of.).As per discussion on views. 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. Then we can take corresponding column name from above results. Replace test with appropriate table name. END prac_trig_emp1 15. The Output will be: COLUMN_NAME ------------------------COUNTRY_ID . How.

Can anyone help me out in this ? using decode function u can do that easily. now I don't know the column name and I want to select the column...3nf.. Question is been asked in the interview.started by another. Shold be declare in DECLARE section of any subprogram. let me give u an example of 2 rows n 2 column x y 1 a 2 b . You can do this by using Pragma. select employee_id from employees ..You can Commit inside the trigger.2nf. Ex: select * from countries where country_id=91. Pragma AUTONOMOUS_TRANSACTION allow you to build the Indepadent(child) Transaction.COUNTRY_NAME REGION_ID Then we wil use column names what u need from above list. But for this you have to make this trigger transaction to be a Independent transaction from its parent transaction.4nf and bcnf? Can we have a commit statement inside a trigger? if no why cant we? Yes . Select from table without using column name How can I select column values from a table without knowing the column name ? Suppose . 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. Please let me know the answer What is the exact diff between 1nf.

'||ename))|| max(decode(rn. EXCEPTION WHEN OTHERS THEN RETURN 0.a. 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. dept where emp.2.'||ename))|| max(decode(rn. loc from (select emp.deptno order by ename) rn from emp.'. max(decode(rn. ename.4.'.deptno) group by deptno.'||ename)) ename.'||ename))|| max(decode(rn. I have a column which contains both numeric and alphanumeric data.2.6. In a distributed database.3. users from any location can issue commands to retrieve data and it will not affect the working of the database.select decode(y. Failure of one of the site will not make the whole sysstem useless. database is stored on several computers and they communicates using networks. Is there any function in oracle to check whether a value is numeric or not. .'.1. BEGIN a := p_num.a.deptno. RETURN 1. 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.'. I want to fetch only the numeric data from that column.deptno = dept.1. result will be like this x y 1 2 a b select deptno.5.a) x.'||ename))|| max(decode(rn.b. row_number () over (partition by emp.'.b.ename))|| max(decode(rn.b) y from table.decode(y. and a system crash may lead to whole dataloss. CREATE OR REPLACE FUNCTION Isnumber(p_num VARCHAR2) RETURN NUMBER AS a NUMBER. loc.

manager_id. 5. 3. 7. SELECT employee_id. The WHERE clause is used when you want to specify a condition for columns. 4. Thanks & Regards. 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. department_id FROM employees WHERE manager_id IN (SELECT manager_id FROM employees WHERE first_name = John) AND department_id IN (SELECT department_id . single row functions except group functions and it is written before GROUP BY clause if it is used. 6.END. 2013 NON PAIRWISE COMPARISON SUBQUERY: Code 1. / 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. Krishnakant. 2. What are Pair Wise & Non-Pair Wise Columns? Vinod Tuteja  Jun 6th. 8.

11. WHERE first_name = John) AND first_name != John. FROM employees 10. manager_id. 11. FROM employees 10. 7. SELECT employee_id. (SELECT department_id 9.9. WHERE first_name = John) AND first_name != John. . 4. 3. 6. Vinod Tuteja  Jun 6th. 2. department_id FROM employees WHERE manager_id IN (SELECT manager_id FROM employees WHERE first_name = John) AND department_id IN 8. 5. 2013 NON PAIRWISE COMPARISON SUBQUERY: Code 1.

(' Rs.----------------------------------------------------800 eight hundred 1600 one thousand six hundred 1250 one thousand two hundred fifty If you want to add some text like. 7.PAIRWISE COMPARISON SUBQUERY: Code 1.')) "Sal in Words" from emp . '|| (to_char(to_date(sal. (SELECT manager_id. 'jsp')) from emp. 'Jsp'))|| ' only. manager_id. FROM employees WHERE (manager_id. 3.'J'). SAL (TO_CHAR(TO_DATE(SAL. SELECT employee_id. Display the number value in Words? SQL> select sal. SQL> select sal "Salary ".'j'). department_id 5. department_id 2.'JSP')) --------. department_id) IN 4. (to_char(to_date(sal. Rs. the output like. Three Thousand only.'j'). FROM employees 6. WHERE first_name = John) AND first_name != John.

ThanksSamir Left Outer & Right Outer Join What is the use of Left Outer & Right Outer Join. One Thousand Two Hundred Fifty only. One Thousand Six Hundred only.-----------------------------------------------------800 Rs. INSERT ALL INTO first_table VALUES(value1. 3. I am writing a script to update a value in an Oracle table. Eight Hundred only. 1600 Rs. 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.value2) INTO second_table VALUES(value1. Is there another way to write this since I am bringing data from 3 different tables. 2012 Answer: Code 1. 1250 Rs./ Salary Sal in Words ------.value2) . The script has a 'From' clause which Oracle does not like. 2.

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