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
|
|
|
|
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 aliasSELECT 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.
Related articles
Related discussion
-
How to receive data in web server sending from GPRS modem
by AshokSingh (1 replies)
-
how to make smtp in vb.net
by konikula (1 replies)
-
copying access DB to SQL DB
by konikula (1 replies)
-
MS Access in 3 different language
by konikula (2 replies)
-
How do I update a table that has an ID from a table that does not have an ID but has a PIDM?
by konikula (1 replies)
Related podcasts
-
Stack Overflow: Podcast #31
This is the thirty-first episode of the StackOverflow podcast, where Joel and Jeff discuss.. stuff! Based on some comments from Podcast #30, we now know that “Learning about NP-Completeness from Jeff is like learning about irony from Alanis Morissette”. It’s funny b...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Oracle Apps Database Administrator - Standplaats: Utrecht
in AMSTERDAM (€50K-€90K per annum) -
Business Analist BI/Datawarehousing
in Amsterdam (€50K-€90K per annum) -
Oracle Ontwikkelaar
in Amsterdam (€50K-€90K per annum)
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.
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]
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#
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
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.
This thread is for discussions of SQL In Simple English.