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

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;

Related

To delete duplicate rows in Oracle SQL [duplicate]

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)

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

concatenate results of a sql query

I am trying to figure if it is possible to concatenate the results of a SQL query if the output has more than one value
For example: If the below query returns more than one result
select * from employee
Output:
emp1
emp2
emp3
I want the results to show as
emp1, emp2, emp3
You can use "listagg"
Example:
SELECT LISTAGG(columnname, ',') WITHIN GROUP (ORDER BY columname)
Hope it resolves the issue!
SELECT LISTAGG(empname,',') WITHIN GROUP(ORDER BY empname) FROM emp;
You can use the listagg function
select listagg(first_name,',') within group(order by employee_id) from employees;
I am using the employees table of HR schema provided in oracle 11g release 2.
You can use the xmlelement function.
SELECT
RTRIM (XMLAGG (XMLELEMENT (E, EMPLOYEENAME|| ', ')).EXTRACT ('//text()'), ',') ENAMES
FROM
EMPLOYEE;

sort query result without selecting that column but sort by that column?

I have a query, I have to sort the result from the DB2 database. The query will select the columns empname,salary,status. But I have to sort the result using order by empno
But the query is not working.. This is the query.
select empname, salary, status from emp where salary>5000 order by empno
Can you update the query to sort by empno without using it in selecting columns?
Your syntax seems correct to me except dot(.) at the end. After removing dot if doesn't work...
Try something like
SELECT empname, salary, status
FROM (SELECT *
FROM emp
ORDER BY empno)
WHERE salary > 5000
Another syntax that may be easier, depending on how you think about it is using the with keyword. This explicitly creates a named temporary table with the desired ordering, then queries from that. The order of the new query will be the same as the temporary tables ordering.
WITH temp_table AS (SELECT *
FROM emp
ORDER BY empno)
SELECT empname, salary, status
FROM temp_table
WHERE salary > 5000;
The answer by #jaychapani is more concise and functionally does the same thing, but the with syntax is powerful for quite a few other use cases and visually separates the two, which can be helpful especially if you have a long subquery that does other things.
I used below query to solve this problem.
In this case we can sort query result without displaying the column:
WITH temp_table
AS (select distinct(s1.Name),s1.id
from students s1
where marks>75
order by right(s1.Name ,3) asc,s1.id asc
)
SELECT Name
FROM temp_table;
I'm not sure, but the fastest way on DB is something like this:
SELECT empname, salary, status
FROM (
select empname, salary, status, empno
from emp
where salary > 5000
order by empno ASC
)
try this
select empname, salary, status from emp where salary>5000 order by empno asc
make sure that columns and table name really exist.
have a look at: ORDER BY clause
Best Regards