Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

sql querry

This is a discussion on sql querry within the SQL forums, part of the Databases category; 1) I want to know the sql querries for the following a. To find second largest salary of an employee in a table. b. To find minimum average salary. c. ...

Go Back   Geeks Talk > Databases > SQL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 08-19-2009
Junior Member
 
Join Date: Aug 2009
Location: bangalore
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
sumangala rk is on a distinguished road
sql querry

1) I want to know the sql querries for the following

a. To find second largest salary of an employee in a table.

b. To find minimum average salary.

c. Querry to fetch current date in SQL SERVER.

2) wat is the difference between GROUP BY and HAVING clauses.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-20-2009
Contributing Member
 
Join Date: Dec 2008
Location: bangalore
Posts: 76
Thanks: 0
Thanked 18 Times in 18 Posts
ecearund is on a distinguished road
Re: sql querry

@sumangala,

To find second largest salary of an employee in a table.

Code:
SELECT * FROM
( SELECT e.deptno , e.ename , e.sal , dense_rank() OVER ( ORDER BY max(e.sal) DESC) Highest_Salary 
FROM emp e, dept t 
where e.deptno=t.deptno
group by e.deptno,e.ename,e.sal) 
where Highest_Salary=2;
To find minimum average salary
Code:
select min(avg(sa)) from emp group by  sal;
Querry to fetch current date in SQL SERVER.
I have no experience in SQL SERVER
I believe the following one get the system date and time .
select getdate()
In Oracle SQL.
(Only date) :
Select sysdate from dual;
(date with timestamp):
Select systimestamp from dual;

Difference between GROUP BY and HAVING clauses
Group by Clause:
Suppose if aggregate functions are included in the select list, finds a summary value for each group.To group the result-set by one or more columns.
Having Clause:
Since where keyword cant be used with aggregate functions, we go for having clause.

Last edited by ecearund; 08-20-2009 at 08:14 AM.
Reply With Quote
The Following User Says Thank You to ecearund For This Useful Post:
  #3 (permalink)  
Old 08-23-2009
Junior Member
 
Join Date: Aug 2009
Location: coimbatore
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
prabhakaranspn is on a distinguished road
Re: sql querry

to find the second largest salary we can have query as follows

select max(salary) from emp where salary < (select max(salary) from emp)

Last edited by prabhakaranspn; 08-23-2009 at 01:33 PM.
Reply With Quote
The Following User Says Thank You to prabhakaranspn For This Useful Post:
  #4 (permalink)  
Old 08-24-2009
Expert Member
 
Join Date: Jun 2007
Location: Australia
Posts: 260
Thanks: 7
Thanked 15 Times in 15 Posts
Sushma Mosali is on a distinguished road
Re: sql querry

Hi difference between group by and having classes the group by and having clauses are used together. The having clause is used as a final filter (rather than as a conditional filter) on the aggregate column values in the result set of a select statement. In other words, the query has to be grouped before the having clause can be applied. For example, consider the following statement, which displays the count of students in various classes (classes of students = 1, 2, 3, 4, corresponding to freshman, sophomore, and so on to get current data there are three ways to retrieve the current datetime in sql server. Current_timestamp, getdate(), {fn now()} current_timestamp current_timestamp is a nondeterministic function. Views and expressions that reference this column cannot be indexed. Current_timestamp can be used to print the current date and time every time that the report is produced. Getdate() getdate is a nondeterministic function. Views and expressions that reference this column cannot be indexed. Getdate can be used to print the current date and time every time that the report is produced. {fn now()} the {fn now()} is an odbc canonical function which can be used in t-sql since the ole db provider for sql server supports them. {fn now()} can be used to print the current date and time every time that the report is produced. If you run following script in query analyzer. I will give you same results. If you see execution plan there is no performance difference. It is same for all the three select statement. Select current_timestamp go select {fn now()} go select getdate() go thanks sushma
Reply With Quote
The Following User Says Thank You to Sushma Mosali For This Useful Post:
  #5 (permalink)  
Old 09-07-2009
Junior Member
 
Join Date: Feb 2009
Location: ongole
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
chiru0106 is on a distinguished road
Re: sql querry

To find the second highest salary of an employee



select * from emp where sal=(select max(sal) from select * from emp minus select * from emp where sal=(select mxa(sal) from emp));

Last edited by chiru0106; 09-07-2009 at 08:35 AM.
Reply With Quote
  #6 (permalink)  
Old 09-10-2009
Junior Member
 
Join Date: Sep 2009
Location: haridwar
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
snlsth is on a distinguished road
Re: sql querry

to find the minimum avg salary the query is
select min(avg(sa)) from emp group by sal;

2. the diffrence between group by and having is :-
group by is not to produced a desired sequence, but to collect the like things together.whereas having function is very much realated to the where caluseexcept that its logic s related to only only group functions
Reply With Quote
  #7 (permalink)  
Old 09-11-2009
Junior Member
 
Join Date: Sep 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
venkatareddy123 is on a distinguished road
Re: sql querry

Hi ,

Please use this to find the second highest Salary

1.select max (salary) from emp where Sal <(select max(sal) from emp)

2.To find the System date use
select sysdate from dual
Reply With Quote
  #8 (permalink)  
Old 09-30-2009
Junior Member
 
Join Date: Aug 2009
Location: jamshedpur
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ujjal.maity is on a distinguished road
Re: sql querry

Quote:
Originally Posted by sumangala rk View Post
1) I want to know the sql querries for the following

a. To find second largest salary of an employee in a table.

b. To find minimum average salary.

c. Querry to fetch current date in SQL SERVER.

2) wat is the difference between GROUP BY and HAVING clauses.
to find minimum u can use min(Average salary)
Reply With Quote
  #9 (permalink)  
Old 10-09-2009
Junior Member
 
Join Date: Sep 2009
Location: Chennai
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
slvganesh90 is on a distinguished road
Re: sql querry

Hi, i'm ganesh form vels college of sceince.. Thanks for all. I've a doubt.. I created a table with the following query.. Create table family (name varchar2(20), age number(3)); desc family; name varchar2(20)
age number(3) now i want to add one more fields called gender.. I use the query alter table add gender varchar2(1); now desc family displays name varchar2(20)
age number(3)
gender varchar2(1) but i want to save the gender field in second position. I.e exactly after the name field.. Can any one help me.. Thank in advance..
Reply With Quote
Reply

  Geeks Talk > Databases > SQL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
sql querry sumangala rk Job Offers 0 08-19-2009 11:45 AM


All times are GMT -4. The time now is 09:41 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved