Library tutorials & articles

SQL In Simple English

Show me more!

Show me something new in SQL..
Ok here are 2 new things that I have used only a few times in my programs. But they maybe useful to you,so I shall talk about them. There are 2 keywords called GROUP BY and HAVING.

Both these are used in conjunction with the aggregate statements like SUM , AVG , etc..

For all the examples in this article we would be using a sample database table which is shown below

Table Name : companies

name profit
Sega
25000
Microsoft
50000
Sega
10000

So what is GROUP BY? When do I use it?
The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible.

SELECT name, SUM(profit) FROM companies
Returns a recordset with 3 records. Each record has 2 values. The first record would have the value 'Sega' and '85000'. The second record would have the values ' Microsoft' and '85000'. And the third record would have the values 'Sega' and '85000'.
Thus it is clear that this is not what was required. There is no sense in getting the sum of all the profits of all the companies along with each company name. What would be acceptable is the sum of all the profits of the respective companies along with that company's name. Read the next statement..

SELECT name, SUM(profit) FROM companies GROUP BY name
Returns a recordset with 2 records. Each record has 2 values. The first record would have the value 'Sega' and '35000'. The second record would have the values 'Microsoft' and '50000'.

And what is the HAVING keyword?
The HAVING keyword has been added to SQL because a WHERE keyword can not be used against aggregate functions (like SUM). Without the HAVING keyword it would not be possible to test for function result conditions.

SELECT name, SUM(profit) FROM companies GROUP BY name HAVING SUM(profit)>40000
Returns a recordset with 1 record. This record would have 2 values, namely 'Microsoft' and '50000'. Since the sum of the profits of the company by the name 'Sega' is only 35000 (which is lesser than 40000 as required in the Query)

SELECT Company "Firm", Amount "Profit" FROM Sales
Alias - Returns the 2 cols with the heading as "Firm" and "Profit" instead of "Company" & "Amount"

Do I have to work with the same column names that exist in the database tables, within my program?
Yes and No. As far as the Query is concerned, you have to include the column names that exist in the Database table in your Query. But you can also include an alias with which you can carry on further work with the returned results by the Database. Let me show you an example of using an alias

SELECT name "firm", profit "riches" FROM companies
Would return a recordset consisting of 3 records each with 2 values. Basically all the 3 records from the sample database would be returned , but the column names would be changed to those that were mentioned in the SQL statement. Thus name would be changed to firm. And profit would change to riches.

Comments

  1. 10 Jun 2003 at 14:57

    Hi I would first like to say your site has been really helpful to me.


    When using ASP to display database information in a web page you might use


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT * FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]


    Then if you wanted to display the recordset you could use


    [courier new]
    <% onIndex=("Number") %>
    [/courier new]


    but what I can't work out is if your select statement uses COUNT how do you display the result


    [courier new]
    Set adoConnect = Server.CreateObject("ADODB.Connection")
    adoConnect.Open "Reports"
    Set onIndex = Server.CreateObject("ADODB.Recordset")


    strSQL = "SELECT COUNT(*) FROM index WHERE number=10"


    onIndex.Open strSQL, adoConnect
    [/courier new]

  2. 06 Mar 2002 at 09:17

    sorry probably it has been too long  and may be you've got the answer but try this
    select  * from table where date> #2/2/1979#

  3. 26 Feb 2002 at 12:35

    hi..
    check this ... should work


    select * from tablename where DateOfBirth<TODATE('10/9/1976','mm/dd/yyyy')


    try using TO_DATE with the date you want.. i have used this before in this manner..


    hope it helps


    bye

  4. 26 Feb 2002 at 06:47

    Thanks, very helpful.


    What about dates though?  What is the code for finding dates using the SELECT statement eg 'SELECT from * WHERE DateOfBirth < 10/9/76 10:01:10', I can't get it to work.

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL In Simple English.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Stack Overflow: Podcast #31

    This is the thirty-first episode of the StackOverflow podcast, where&#10;Joel and Jeff discuss.. stuff!&#10;&#10;Based on some comments from Podcast #30, we now know that &ldquo;Learning about NP-Completeness from Jeff is like learning about irony from Alanis Morissette&rdquo;. It&rsquo;s funny b...

Related jobs

Events coming up

  • Jun 16

    Code Generation 2009

    Cambridge, United Kingdom

    A developer event with a practical focus on helping people get to grips with code generation tools and technologies.