SQL Developer : Holding Multiple values in Variable From Query Result - sql

I need help with query for sql developer.The requirement is a select statement which returns ID Column.I need to hold this ids in a variable and fetch the records associated with each id.Thanks for any help.

You can use join statement.
for example:
select id from table_1 join table_2
on table_1.id = table_2.id;

Not possible, as far as I can tell.
But, if it is the same query all over again and you use its result (those ID values you mentioned) in another queries, then you could create a view
create or replace view v_id as
select id
from some_table
where some_conditions;
and then use that view as yet another data source, e.g.
select d.dname, e.ename, e.job, e.sal
from emp e join v_id v on v.id = e.empno
join dept d on d.deptno = e.deptno;
or
select e.ename, e.job, e.sal
from emp e
where e.empno in (select v.id from v_id v);
or any other option you might choose.

Related

showing columns with Oracle SQL

I work with tables that have lots of columns that don't fit on my screen. When I create SQL I am really looking at a few columns worth of information.
What is a better way of moving a column from the right side to the first left most column? That way my results will show the column I am interested in first.
Currently I am using
Select ColumnNumber201, t.*
from TableName t
I have a feeling this will add extra results to my query if I was concerned about counts, because it is a join. I would like avoid joins if possible.
Thanks for your help in advance.
We are using Oracle DB SQL
Well, this is the way to display columns you're interested in - name them. In order to put them to the "front" (i.e. to be the leftmost), do exactly what you are doing.
I don't understand your concern about counts. What "join" are you talking about? There's no join in a statement you posted. This:
Select ColumnNumber201, t.*
from TableName t
selects only from one table.
Even if you join it to another table, it is the join condition and where clause that matters, not number of columns you select (OK, aggregates change things, but that's another story).
For example:
SQL> select count(*) one_column_one_table
2 from (select e.ename
3 from emp e);
ONE_COLUMN_ONE_TABLE
--------------------
14
SQL>
SQL> select count(*) many_columns_one_table
2 from (select e.ename, e.*
3 from emp e);
MANY_COLUMNS_ONE_TABLE
----------------------
14
SQL>
SQL> select count(*) many_columns_two_tables
2 from (select d.dname, e.ename, e.*, d.*
3 from emp e join dept d on e.deptno = d.deptno);
MANY_COLUMNS_TWO_TABLES
-----------------------
14
SQL>
SQL> select count(*) many_columns_two_tables_where
2 from (select d.dname, e.ename, e.*, d.*
3 from emp e join dept d on e.deptno = d.deptno
4 where d.deptno = 20);
MANY_COLUMNS_TWO_TABLES_WHERE
-----------------------------
5
SQL>

How to change this db2 merge query?

can someone help me? i am trying to convert the following merge into another query and i am allowed only to use insert and update once:
MERGE INTO MYEMPLOYEE ME USING EMPLOYEE E ON ME.EMPNO = E.EMPNO
WHEN MATCHED THEN
UPDATE SET ME.SALARY = CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
WHEN NOT MATCHED THEN
INSERT VALUES(E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM);
How can I achieve this? the above merge copies the data if not exists, and if it exists it checks for the salary and selects the higher one and copies that one.
how can I achieve the same thing by only using one insert and one update? Can someone give me hints, please?
Thanks in advance :)
The purpose of the MERGE command is suppose to take into account multiple actions of UPDATE, INSERT, DELETE. MERGE statement explained
If you cannot/unable to use MERGE, then you have to resort to doing each request individually.
UPDATE MYEMPLOYEE ME
SET ME.SALARY = (
SELECT CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
)
WHERE EXISTS(
SELECT 1
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
);
Then do an insert where the employee don't exist in the master table.
INSERT INTO MYEMPLOYEE ME
SELECT *
FROM EMPLOYEE E
LEFT OUTER JOIN MYEMPLOEE ME ON E.EMPNO=ME.EMPNO
WHERE ME.EMPNO IS NULL;
If you need to do in one full sweep you can use the IMPORT command. But then you are dealing with files. You would need to export the EMPLOYEE table (probably with salary already formatted) and then import using the INSERT_REPLACE ability.
After trying I noticed that the INSERT code should actually be like this:
1) Don't use the Name ME twice
2) Select the necessary columns because after the join there are twice the column count
INSERT INTO MYEMPLOYEE (
SELECT E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM
FROM EMPLOYEE E LEFT OUTER JOIN MYEMPLOYEE ME ON E.EMPNO = ME.EMPNO WHERE ME.EMPNO IS NULL
);

SQL Server fetch alias name for query

Please check fiddle: myFiddle
Query:
create table Emp(empId int primary key, EmpName varchar(50),MngrID int)
insert into Emp(empId,EmpName,MngrID)values(1,'A',2)
insert into Emp(empId,EmpName,MngrID)values(2,'B',null)
A has mngr B but A has no mngr, so while fetching the record from query it shows me:
EmpId EmpName MngrName(alias MngrName)
1 A B
2 B null
How to fetch the above data using a query?
For some reason it doesn't work in SQLFiddle, but i ran it in my own instance of SQL Server to verify it does work:
SELECT e1.EmpID, e1.EmpName, e2.EmpName
FROM emp e1 LEFT OUTER JOIN emp e2
ON e1.MngrID = e2.EmpID
Basically, you're doing a 'self join' by declaring two instances of the table (e1 and e2), and then joining the first instance's MngrID to the second instance's EmpID.
You need to LEFT JOIN table to itself:
select A.empID, A.empName, b.empName as mgrName
from emp A left join emp B on A.mngrID = b.empID
http://sqlfiddle.com/#!3/184dc/8
select empId,EmpName,(SELECT EmpName FROM emp WHERE MngrID = amp1.MngrID) AS Manager from emp as amp1

Oracle IN vs Exists difference?

I am confused with oracle IN and EXISTS. I have below requirement.
I need to get all the employees whose names are in-
select * from emp where ename in('smith','brown','john','johnson');
Can i use EXISTS here? Also IN clause has 1000 limitation. Does EXISTS also has any such limitation?
Thanks!
simply put, EXISTS is usually used for checking whether rows that meet a criteria exist in another (or the same) table.
your SQL using EXISTS would look like this:
select *
from emp e
where exists(select * from emp e2 where e.empno = e2.empno and e2.ename in ('smith', 'brown', 'john', 'johnson'))
so you can see it's not what you need here
IN picks the list of matching values. EXISTS returns the boolean values like true or false. Exists is faster than in.
Example
IN
select ename from emp e where mgr in(select empno from emp where ename='KING');
EXISTS
select ename from emp e
where exists (select 1 from emp where e.mgr = empno and ename = 'KING');

SQL query how to print out manager name?

I need to create a query to display employee name and number along with their super's name and super number. Listing should also include employees who don't have any supervisor.
Select e.ename,e.empno,super.ename,super from emp e;
I don't know how to print out the manager/supervisor name, i just need that.
Join the table to itself using an outer join:
select e.ename, e.empno, super.ename, super.empno
from emp e
left join emp super on super.empno = e.super_empno
The left join will still return rows from emp that do not have a super defined.
EDIT
Due to OP's comment, here's how to do it without a join (using a nasty correlated sub-query):
select
ename,
empno,
(select super.ename from emp where empno = e.super_empno) as super_name,
super_empno
from emp e;