To delete duplicate rows in Oracle SQL [duplicate] - sql

This question already has answers here:
Removing duplicate rows from table in Oracle
(24 answers)
Closed 3 years ago.
I've used this code to select the duplicate rows, it went right,
SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT>1;
But it doesn't help me in deleting the same selected rows....
DELETE emp
WHERE name IN ( SELECT name, COUNT(name)
FROM emp
GROUP BY name
HAVING COUNT >1);

Remove the second column from the inner SELECT, something like this:
DELETE
FROM emp
WHERE name IN( SELECT name FROM emp GROPU BY name HAVING COUNT(name) > 1)

Related

SHOW TOP 1 in ORACLE [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 months ago.
Here's the query
SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE
WHERE ROWNUM = 1;
I've been trying to use rownum function but I keep getting the error - "ORA-00933: SQL command not properly ended".
You can try like this:
SELECT *
FROM (SELECT concat(FIRSTNAME,LASTNAME) AS NAME, HIREDATE from EMPLOYEE ORDER BY HIREDATE )
WHERE ROWNUM = 1;

Delete duplicate values [duplicate]

This question already has answers here:
Oracle - deleting duplicates
(2 answers)
How to delete duplicate records from a table in oracle
(5 answers)
Removing duplicate rows from table in Oracle
(24 answers)
Closed 2 years ago.
I hava a table emp, I wish to delete the duplicate values.
The subquery is good.
SELECT ename
FROM emp
GROUP BY ename
HAVING Count(empno) > 1
But when I do the delete operation its deleting all the lines. I don't understand what mistake I'm doing.
DELETE FROM emp
WHERE ename IN (SELECT ename
FROM emp
GROUP BY ename
HAVING Count(empno) > 1);
It deletes all ENAMEs, not only duplicates (i.e. it doesn't leave one row). Try something like
Table contents at beginning:
SQL> select * From temp order by id;
ID ENAME
---------- ----------
1 Little
2 Little --> duplicate ENAME
3 Foot
Deleting:
SQL> delete from temp a
2 where a.rowid > (select min(b.rowid)
3 from temp b
4 where b.ename = a.ename
5 );
1 row deleted.
Result;
SQL> select * From temp order by id;
ID ENAME
---------- ----------
1 Little
3 Foot
SQL>

SQL query where Employee works in more than one Department

I have employee table with emp id (emp_id) and department (dep_id) fields. An employee could be working in more than one Department. I want to write a sql query to display unique emp_ids who work in more than one department.
Pl help me to write sql query.
Thx
Answered here: SQL query for finding records where count > 1
You need to use count, group by and having like this.
select emp_id, count(dep_id)
from employee_department
group by emp_id
having count(dep_id)>1
Query
SELECT COUNT(*)
FROM
(
SELECT id_employee, COUNT(*) AS CNT
FROM Department_Employee
GROUP BY id_employee
) AS T
WHERE CNT > 1

Select 2 lowest values from a query in SQL [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 7 years ago.
I am trying get information of 2 lowest salary holders from default "scott" table.
This is the query I am trying:
SELECT TOP 2 * FROM emp ORDER BY sal ASC
But I'm getting this error:
ORA-00923: FROM keyword not found where expected
Screenshot:
In the most recent versions of Oracle, you can use the ANSI standard:
SELECT emp.*
FROM emp
ORDER BY sal ASC
FETCH FIRST 2 ROWS ONLY;
In older versions:
SELECT e.*
FROM (SELECT emp.*
FROM emp
ORDER BY sal ASC
) e
WHERE rownum <= 2;
You can use ROWNUM in Oracle to get two first rows of your query results.
SELECT EMP1.* FROM (SELECT * FROM EMP ORDER BY SAL ASC) EMP1 WHERE ROWNUM < 3;
TOP X is used by SQL Server
Fetch first is used by Oracle and DB2
(rownum is also available in Oracle)
Limit is used by mysql
Many can also use a windowing function (ROW_NUMBER()) in a sub query.
You want
select * from emp ORDER BY sal ASC
fetch first 2 rows only

Trying to get the result in single row using sql? [duplicate]

This question already has answers here:
SQL Query to concatenate column values from multiple rows in Oracle
(10 answers)
Closed 1 year ago.
I'm trying to display all the employee id's
i need the result like
emp_id
10,11,12,13,14,15..,...
when tried
SELECT LISTAGG(emp_id, ',') WITHIN GROUP (ORDER BY emp_id) AS ID FROM employees GROUP BY emp_id;
I'm getting error
ORA-00923: FROM keyword not found where expected
where is the problem here?
Use LISTAGG function. Refer here for more in detail. Try like this,
SELECT listagg(emp_id,',') WITHIN GROUP(ORDER BY emp_id) t
FROM employees;
For SQL try
SELECT GROUP_CONCAT(emp_id) as emp_id FROM employees;
Working demo at http://sqlfiddle.com/#!2/90dd2/1
For Oracle
SELECT LISTAGG(emp_id,',') WITHIN GROUP(ORDER BY emp_id) as emp_id
FROM employees;
demo at http://sqlfiddle.com/#!4/af004/9
Try this
SELECT CONCAT(emp_id,',') FROM employees;
try this
declare #empid nvarchar(500)=''
select #empid=#empid+Emp_id + ',' from tblemployee
select substring(#empid,0,len(#empid)-1)
Try this:
SELECT GROUP_CONCAT(emp_id) as emp_id FROM employees;