Home » SQL & PL/SQL » SQL & PL/SQL » sql queries (oracle 11g sql developer)
sql queries [message #645015] Mon, 23 November 2015 12:44 Go to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
using the scott scheme please give the syntax for the following 2 queries
1.display the employee number, name, and salary for all employees
who earn more than the average salary and who work in a department with any employee
with a T in their name.
2.Write a query that will display the employee number and name for all employees who work in a department with any employee whose name contains a T.
Re: sql queries [message #645016 is a reply to message #645015] Mon, 23 November 2015 12:56 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
We don't do homework assignments.

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read
Re: sql queries [message #645018 is a reply to message #645015] Mon, 23 November 2015 13:48 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Let me guess, you get the answer to this and you go tell a prospective employer that you have high level sql skills.



Re: sql queries [message #645019 is a reply to message #645018] Mon, 23 November 2015 14:05 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
I have the output of both the queries and do not understand what the t is supposed to mean here.and pabolee this is just me trying to learn on my own without any formal training.
Re: sql queries [message #645020 is a reply to message #645019] Mon, 23 November 2015 14:14 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
EMPNO ENAME SAL
----- ------ ----
7566 JONES 2975
7788 SCOTT 3000
7902 FORD 3000
7698 BLAKE 2850
This is the output for the first query.
Re: sql queries [message #645022 is a reply to message #645020] Mon, 23 November 2015 14:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And what is the query?
Or what are you tries?
Re: sql queries [message #645118 is a reply to message #645015] Thu, 26 November 2015 23:54 Go to previous messageGo to next message
Rishab_le_noob
Messages: 12
Registered: November 2015
Location: Kolkata
Junior Member
Quote:

do not understand what the t is supposed to mean here.


As far as I understand they want names like these
'Margaret'
'Lawton'
'Lt. Dan'
'Tommy Lee'

And not 'Jenny', 'Mama'
So basically
where upper(name) like '%T%'
Re: sql queries [message #645120 is a reply to message #645118] Fri, 27 November 2015 00:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Plain wrong, just read the output OP posted.

Re: sql queries [message #645123 is a reply to message #645120] Fri, 27 November 2015 00:47 Go to previous messageGo to next message
Rishab_le_noob
Messages: 12
Registered: November 2015
Location: Kolkata
Junior Member
@Michel Cadot
The O/P he has posted is the output of his query and not what he desires.
Am pretty sure he got the Output wrong, and that is because he didn't understand why the 'T' is needed.

Quote:


1.display the employee number, name, and salary for all employees
who earn more than the average salary and who work in a department with any employee
with a T in their name.
2.Write a query that will display the employee number and name for all employees who work in a department with any employee whose name contains a T.


He later wrote
Quote:

I have the output of both the queries and do not understand what the t is supposed to mean here.


So some of the final output might have names without T in them but the initial check on Dept will be if they have an employee with a 'T' in their name.
Which I dont think he is doing... anyways he needs to share his query 1st.

[Updated on: Fri, 27 November 2015 00:53]

Report message to a moderator

Re: sql queries [message #645125 is a reply to message #645123] Fri, 27 November 2015 00:54 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Sure? He did not post any query nor he did mention he has such a query.

Quote:
This is the output for the first query.

Seems to me this is the one he wants.

In addition, there is nothing about salary and department in your post, so it can't be a solution not even a beginning of a solution.
In the end, your analyze is wrong, if you carefully read the specification, nothing forbid name with no T to be in the result.

Re: sql queries [message #645126 is a reply to message #645125] Fri, 27 November 2015 01:03 Go to previous messageGo to next message
Rishab_le_noob
Messages: 12
Registered: November 2015
Location: Kolkata
Junior Member
Quote:

So some of the final output might have names without T in them but the initial check on Dept will be if they have an employee with a 'T' in their name.


I had updated my last post with this.
Yes the final query can have names with "T" in them, but the they need departments where there's at least one employee with a 'T' in their name.
I was just trying to put some light on the "T" part.
I even mentioned his comment in quotes to highlight that.

But I can see that my response is kind of confusing. As it focuses on my point and not his.
Will rectify it by rephrasing it.

Re: sql queries [message #645161 is a reply to message #645126] Sat, 28 November 2015 00:47 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
The statements for the queries would be as follows
1.SELECT EMPNO,ENAME,DEPTNO FROM EMP
WHERE DEPTNO IN
(SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

2.SELECT EMPNO,ENAME,SAL FROM EMP
WHERE SAL >
(SELECT AVG(SAL) FROM EMP)
AND DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');
Re: sql queries [message #645162 is a reply to message #645161] Sat, 28 November 2015 00:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Thanks for the feedback, however it is the opposite (query 1 is solution of question 2 and vice versa).
Please read How to use [code] tags and make your code easier to read.

Re: sql queries [message #645163 is a reply to message #645162] Sat, 28 November 2015 00:52 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
yes I know. but we all can understand it just by looking at the statements.
Re: sql queries [message #645165 is a reply to message #645163] Sat, 28 November 2015 00:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

All? Are you sure? If the queries were so simple why did you create this topic? Grin

Re: sql queries [message #645166 is a reply to message #645165] Sat, 28 November 2015 01:00 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
when I said all I meant intelligent people like you who are supposed to find the answer to our queries.
Re: sql queries [message #645168 is a reply to message #645166] Sat, 28 November 2015 01:17 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

But solutions are not for me, they are for people who learn SQL and search for help in the web.
OK, if this is so trivial for you, improve your queries:

1/
SELECT EMPNO,ENAME,SAL 
FROM EMP
WHERE SAL > (SELECT AVG(SAL) FROM EMP)
  AND DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

This query scan 3 times the table EMP:
SQL> set autotrace traceonly explain
SQL> SELECT EMPNO,ENAME,SAL
  2  FROM EMP
  3  WHERE SAL > (SELECT AVG(SAL) FROM EMP)
  4    AND DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

Execution Plan
----------------------------------------------------------
Plan hash value: 828433562

-----------------------------------------------------------------------------
| Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |     1 |    26 |    10  (10)| 00:00:01 |
|*  1 |  HASH JOIN SEMI      |      |     1 |    26 |     7  (15)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL  | EMP  |     1 |    17 |     3   (0)| 00:00:01 |
|   3 |    SORT AGGREGATE    |      |     1 |     4 |            |          |
|   4 |     TABLE ACCESS FULL| EMP  |    14 |    56 |     3   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS FULL  | EMP  |     1 |     9 |     3   (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DEPTNO"="DEPTNO")
   2 - filter("SAL"> (SELECT AVG("SAL") FROM "EMP" "EMP"))
   5 - filter("ENAME" LIKE '%T%')


2/
SELECT EMPNO,ENAME,DEPTNO 
FROM EMP
WHERE DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

This query scans twice the EMP table:
SQL> SELECT EMPNO,ENAME,DEPTNO
  2  FROM EMP
  3  WHERE DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

Execution Plan
----------------------------------------------------------
Plan hash value: 977554918

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     5 |   110 |     7  (15)| 00:00:01 |
|*  1 |  HASH JOIN SEMI    |      |     5 |   110 |     7  (15)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |   182 |     3   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| EMP  |     1 |     9 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("DEPTNO"="DEPTNO")
   3 - filter("ENAME" LIKE '%T%')


Oracle introduces in version 8.1.6 (in 1999) analytic functions to avoid so much scans, can you provide queries that scan only once the table EMP?

[Updated on: Sat, 28 November 2015 01:17]

Report message to a moderator

Re: sql queries [message #645171 is a reply to message #645168] Sat, 28 November 2015 01:55 Go to previous messageGo to next message
sabsac
Messages: 9
Registered: October 2015
Location: I
Junior Member
Ok let me post the answers correctly.The statement for the queries are as follows.
1.1.SELECT EMPNO,ENAME,SAL FROM EMP
WHERE SAL >
(SELECT AVG(SAL) FROM EMP)
AND DEPTNO IN (SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');

2.SELECT EMPNO,ENAME,DEPTNO FROM EMP
WHERE DEPTNO IN
(SELECT DEPTNO FROM EMP WHERE ENAME LIKE '%T%');
Re: sql queries [message #645173 is a reply to message #645171] Sat, 28 November 2015 02:31 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I already post them and I posted them FORMATTED. See the difference between your posts and mine. Which one is easier to read?
Now find the optimized queries.

Previous Topic: SQL Query Suggestion
Next Topic: Closing cursors - but its does not? Error: maximum open cursors exceeded
Goto Forum:
  


Current Time: Thu Apr 25 12:34:01 CDT 2024