473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Please solve this query, and help me. :-)

Hi,
Good morning to all, :-)

One interviewer asked me the following question

He has given the following two tables including data.
DEPT TABLE
-------------------
DEPT(DEPTNO, DNAME, LOC)
DEPT(10, 'ACCOUNTING, 'NEW YORK')
DEPT(20, 'RESEARCH', 'DALLAS')
DEPT(30, 'COMPUTERS', 'BANGALORE')

EMP TABLE
------------------
EMP(EMPNO, ENAME, MGRNO, DEPTNO)
EMP(1001, 'ASHOK KUMAR', 1001, 10)
EMP(1002, 'KAVITHA', 1001, 10)
EMP(1003, 'MURTHY', 1003, 20)
EMP(1004, 'MOHAN', 1003, 20)

QUERY:
Expand|Select|Wrap|Line Numbers
  1. SELECT ALL EMPLOYEE NAMES, AND THEIR MANAGER NAMES WORKING IN DEPTNO 10
Can anyone please tell the answer for that query?

Thanks in advance...
Ashok kumar.
Jul 17 '07 #1
12 1797
WayneW
9
Hi,
Good morning to all, :-)

One interviewer asked me the following question

He has given the following two tables including data.
DEPT TABLE
-------------------
DEPT(DEPTNO, DNAME, LOC)
DEPT(10, 'ACCOUNTING, 'NEW YORK')
DEPT(20, 'RESEARCH', 'DALLAS')
DEPT(30, 'COMPUTERS', 'BANGALORE')

EMP TABLE
------------------
EMP(EMPNO, ENAME, MGRNO, DEPTNO)
EMP(1001, 'ASHOK KUMAR', 1001, 10)
EMP(1002, 'KAVITHA', 1001, 10)
EMP(1003, 'MURTHY', 1003, 20)
EMP(1004, 'MOHAN', 1003, 20)

QUERY:
Expand|Select|Wrap|Line Numbers
  1. SELECT ALL EMPLOYEE NAMES, AND THEIR MANAGER NAMES WORKING IN DEPTNO 10
Can anyone please tell the answer for that query?

Thanks in advance...
Ashok kumar.
************************************************** ***********************
SELECT dbo.DEPT_TABLE.DEPTNO, dbo.DEPT_TABLE.DNAME, dbo.DEPT_TABLE.LOC, dbo.EMP_TABLE.EMPNO, dbo.EMP_TABLE.ENAME, dbo.EMP_TABLE.MGRNO, EMP_TABLE_1.ENAME AS Manager

FROM dbo.DEPT_TABLE INNER JOIN
dbo.EMP_TABLE ON dbo.DEPT_TABLE.DEPTNO = dbo.EMP_TABLE.DEPTNO INNER JOIN
dbo.EMP_TABLE AS EMP_TABLE_1 ON dbo.EMP_TABLE.MGRNO = EMP_TABLE_1.EMPNO

WHERE (dbo.DEPT_TABLE.DEPTNO = 10)
Jul 17 '07 #2
Hi,
Good morning to all, :-)

One interviewer asked me the following question

He has given the following two tables including data.
DEPT TABLE
-------------------
DEPT(DEPTNO, DNAME, LOC)
DEPT(10, 'ACCOUNTING, 'NEW YORK')
DEPT(20, 'RESEARCH', 'DALLAS')
DEPT(30, 'COMPUTERS', 'BANGALORE')

EMP TABLE
------------------
EMP(EMPNO, ENAME, MGRNO, DEPTNO)
EMP(1001, 'ASHOK KUMAR', 1001, 10)
EMP(1002, 'KAVITHA', 1001, 10)
EMP(1003, 'MURTHY', 1003, 20)
EMP(1004, 'MOHAN', 1003, 20)

QUERY:
Expand|Select|Wrap|Line Numbers
  1. SELECT ALL EMPLOYEE NAMES, AND THEIR MANAGER NAMES WORKING IN DEPTNO 10
Can anyone please tell the answer for that query?

Thanks in advance...
Ashok kumar.

Hi,

The query for u is 'select ename,mgrno from emp where deptno=10'
Jul 17 '07 #3
WayneW
9
It depends on what you want in the result. If you want the manager name next to the employee, use this script.
Jul 17 '07 #4
hariharanmca
1,977 1GB
Hi,

The query for u is 'select ename,mgrno from emp where deptno=10'
no, its wrong...

read the qrey properly

Expand|Select|Wrap|Line Numbers
  1. SELECT ALL EMPLOYEE NAMES, AND THEIR MANAGER NAMES  WORKING IN DEPTNO 10
you have to select Managers name and employe names

so you have to inner join the table.

(What WayneW posted is correct)
Jul 17 '07 #5
select e.empname,e1.empname from employee e,employee e1 where e.empno=e1.mangno and e.deptno=10
Jul 18 '07 #6
DonlonP
25
You're all wrong!

It's a trick question, you don't need the DEPT table at all. You find the manager's name by looking at which record has an empno that's the same as the mgrno. So the query is:

SELECT
ENAME AS EmployeeName,
(SELECT ENAME FROM EMP WHERE EMPNO = MGRNO AND DEPTNO=10) AS ManagerName
FROM
EMP
WHERE
DEPTNO=10
Jul 18 '07 #7
hariharanmca
1,977 1GB
You're all wrong!

It's a trick question, you don't need the DEPT table at all. You find the manager's name by looking at which record has an empno that's the same as the mgrno. So the query is:

SELECT
ENAME AS EmployeeName,
(SELECT ENAME FROM EMP WHERE EMPNO = MGRNO AND DEPTNO=10) AS ManagerName
FROM
EMP
WHERE
DEPTNO=10
So, you think you are correct?

Just refer post #5. That I mean

You have to inner join the table

Using where class for a field (which having primary key relation and make things to be confuse) in a select qurey, is not good practice

the qrey will be

Expand|Select|Wrap|Line Numbers
  1. SELECT tblemployee.EMPNO, tblemployee.ENAME AS Employee, tblemployee.MGRNO, tblManager.ENAME AS Manager, tblemployee.DEPTNO
  2. FROM tblemployee INNER JOIN tblemployee AS tblManager ON tblemployee.MGRNO=tblManager.EMPNO
  3. WHERE (((tblemployee.DEPTNO)=10))
Jul 19 '07 #8
Thankyou very much Hariharan,
Your explination is really beautiful.
I couldn't able to see your post till Monday(23/July) due to some busy schedule.
bye...
Ashok kumar.

So, you think you are correct?

Just refer post #5. That I mean

You have to inner join the table

Using where class for a field (which having primary key relation and make things to be confuse) in a select qurey, is not good practice

the qrey will be

Expand|Select|Wrap|Line Numbers
  1. SELECT tblemployee.EMPNO, tblemployee.ENAME AS Employee, tblemployee.MGRNO, tblManager.ENAME AS Manager, tblemployee.DEPTNO
  2. FROM tblemployee INNER JOIN tblemployee AS tblManager ON tblemployee.MGRNO=tblManager.EMPNO
  3. WHERE (((tblemployee.DEPTNO)=10))
Jul 23 '07 #9
itsraghz
127 100+
Having the DEPT table involved in the query would make your answer simple and correct!
Jul 23 '07 #10
Hi,
I hope this is what you are looking for


SELECT
E1.ENAME AS EMPLOYEE_NAME,E2.ENAME AS MANAGER_NAME
FROM
EMP E1
JOIN
EMP E2
ON
E1.MGRNO = E2.EMPNO
AND
E2.DEPTNO=10


Cheers,
Nambi
Aug 24 '07 #11
azimmer
200 Expert 100+
Having the DEPT table involved in the query would make your answer simple and correct!
Sorry to come in once it's been solved but: would the right answer be different if the DEPT table were empty (or no deptno=10 row in it)? No, it wouldn't. You don't need that table to answer this question.
Aug 24 '07 #12
Hi,
Good morning to all, :-)

One interviewer asked me the following question

He has given the following two tables including data.
DEPT TABLE
-------------------
DEPT(DEPTNO, DNAME, LOC)
DEPT(10, 'ACCOUNTING, 'NEW YORK')
DEPT(20, 'RESEARCH', 'DALLAS')
DEPT(30, 'COMPUTERS', 'BANGALORE')

EMP TABLE
------------------
EMP(EMPNO, ENAME, MGRNO, DEPTNO)
EMP(1001, 'ASHOK KUMAR', 1001, 10)
EMP(1002, 'KAVITHA', 1001, 10)
EMP(1003, 'MURTHY', 1003, 20)
EMP(1004, 'MOHAN', 1003, 20)

QUERY:
Expand|Select|Wrap|Line Numbers
  1. SELECT ALL EMPLOYEE NAMES, AND THEIR MANAGER NAMES WORKING IN DEPTNO 10
Can anyone please tell the answer for that query?

Thanks in advance...
Ashok kumar.

hello ashok kumar

actually the target columns you asked in all in EMPTABLE, so there is no inner join is requried,
so take the required said columns from single table directly.
Aug 25 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: matty | last post by:
Just a couple of quick comments: In some of the CSS/DOM mailing lists I'm on, people generally refuse to help unless the HTML and CSS will validate. You can check these at...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
3
by: MattB | last post by:
Not sure if this is a good approach or not, but I'd like to hear some informed opinions. I've designed an ECommerce site that interfaces with my company's POS system. I'm trying to make it as...
5
by: Darin L. Miller | last post by:
I'm not too good with advanced SQL queries, so please bear with me on this. I have a query with multiple joins that I am trying to get just the last 10 of each unique record (RecordID)...
4
by: Tonio Tanzi | last post by:
I have the following problem in a Win 2000 Server + SQL Server 2000 environment and I hope somewhat can help me to resolve it (after many days of useless attempts I am desperate). In my database...
6
by: fido19 | last post by:
Once upon a time, there lived a chimpanzee called Luycha Bandor (aka Playboy Chimp). Luycha was unhappily married to Bunty Mona, a short but cute little lady chimp. Luycha was tall and handsome –...
1
by: shapper | last post by:
Hi, On a form I have an input where tags are inserted in a CSV format. Then on my code I convert the CSV string to a List<Tag>. Tag is an object with two properties: TagID and Name So when I...
0
by: xwebmaster | last post by:
Hi Please help me solving this query.. I have 3 tables.. comm_propDB ID | User_ID | mls_num -----|---------------|--------------------- 2 | 2 | 5004 3 | 2 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.