0% found this document useful (0 votes)
30 views

GROUP BY Clause in SQL - 1keydata

Uploaded by

vickroid007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

GROUP BY Clause in SQL - 1keydata

Uploaded by

vickroid007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

3/2/2016 GROUP BY Clause in SQL | 1Keydata

SQL GROUP BY

SQL Data Warehousing CSS PHP HTML Database Normalization

SQL Tutorial Google Web Hosting


Build Your Online Presence With Google Sites. Free 30­Day Trial!
Basic SQL Commands
SQL SELECT
SQL DISTINCT
SQL WHERE SQL > SQL Commands > Group By
SQL AND OR
Now we return to the aggregate functions. Remember we used the SUM keyword to calculate the total sales for all stores?
SQL IN
calculate the total sales for each store? Well, we need to do two things: First, we need to make sure we select the store na
SQL BETWEEN
sales. Second, we need to make sure that all the sales figures are grouped by stores. The corresponding SQL syntax is,
SQL Wildcard
SQL LIKE SELECT "column_name1", SUM("column_name2")
SQL ORDER BY FROM "table_name"
SQL GROUP BY GROUP BY "column_name1";
SQL HAVING
SQL ALIAS Let's illustrate using the following table,
SQL AS
SQL SELECT UNIQUE
Table Store_Information
SQL JOIN
Store_Name Sales Txn_Date
SQL INNER JOIN
Los Angeles 1500 Jan-05-1999
SQL OUTER JOIN
San Diego 250 Jan-07-1999
SQL LEFT OUTER JOIN
SQL CROSS JOIN Los Angeles 300 Jan-08-1999
SQL INSERT INTO Boston 700 Jan-08-1999
SQL INSERT INTO SELECT
We want to find total sales for each store. To do so, we would key in,
SQL UPDATE
SQL DELETE FROM
SELECT Store_Name, SUM(Sales)
Advanced SQL FROM Store_Information
SQL UNION GROUP BY Store_Name;
SQL UNION ALL
SQL Inline View Result:
SQL INTERSECT
SQL MINUS Store_Name SUM(Sales)
SQL LIMIT Los Angeles 1800
SQL TOP San Diego 250
SQL Subquery Boston 700
SQL EXISTS
SQL CASE
GROUP BY Multiple Columns
SQL DECODE
SQL AUTO INCREMENT In this example, there is only one column associated with GROUP BY. It is possible to have two columns or more associate
SQL IDENTITY
SEQUENCE & NEXTVAL The GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithmet
in the SELECT statement. Such operators include COUNT, SUM, MAX, MIN, and AVG. When that happens, we need to
SQL Functions
selected columns, i.e., all columns except the one(s) operated on by the arithmetic operator. As such, it is important to not
SQL Average
two columns or more associated with GROUP BY. The general syntax is as follows:
SQL COUNT
SQL MAX
SELECT "column_name1", "column_name2", ... "column_nameN", Function("column_nameN+1")
SQL MIN FROM "table_name"
SQL SUM GROUP BY "column_name1", "column_name2", ... "column_nameN";
SQL ROUND

String Functions GROUP BY Month / Date / Week


SQL CAST
SQL CONVERT A common use of the GROUP BY function is on a time period, which can be month, week, day, or even hour. This type of q
SQL CONCATENATE combined with the ORDER BY keyword to provide a query result that shows a time series.
SQL SUBSTRING
For example, to find total daily sales from Store_Information, we use the following SQL:
SQL INSTR
SQL TRIM
SELECT Txn_Date, SUM(Sales)
http://www.1keydata.com/sql/sqlgroupby.html 1/2
3/2/2016 GROUP BY Clause in SQL | 1Keydata
SELECT Txn_Date, SUM(Sales)
SQL LENGTH FROM Store_Information
SQL REPLACE GROUP BY Txn_Date;
SQL TO_DATE

Date Functions Result:


Txn_Date SUM(Sales)
Jan-05-1999 1500
Jan-07-1999 250
Jan-08-1999 1000

Exercises

For these exercises, assume we have a table called Region_Sales with the following data:

Table Region_Sales

Region Year Orders Total_Sales


West 2013 1560 325000
West 2014 1820 380000
North 2013 790 148000
North 2014 995 185000
East 2013 1760 375000
East 2014 2220 450000
South 2013 1790 388000
South 2014 1695 360000

1. Which of the following order is correct for a SQL statement?


a) SELECT...FROM...WHERE...GROUP BY...ORDER BY
b) SELECT...FROM...ORDER BY...WHERE...GROUP BY
c) SELECT...FROM...WHERE...ORDER BY...GROUP BY
d) SELECT...WHERE...FROM...GROUP BY...ORDER BY

2. Write a SQL statement that calculates the total dollar sales amount for each region. What is the result?

3. Write a SQL statement that calculates the average annual dollar sales amount for just the East region and the West regi
result?

Show Answers

Next: SQL HAVING

Zendesk ­ Official Site


Zendesk Makes Communicating With Customers Easy + Efficient. Try Now

Copyright © 2016 1keydata.com All Rights Reserved Privacy Policy About Contact

http://www.1keydata.com/sql/sqlgroupby.html 2/2

You might also like