<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=RDBMS-PostgreSQL%2FC2%2FAggregation-facilities-in-SQL%2FEnglish-timed</id>
		<title>RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=RDBMS-PostgreSQL%2FC2%2FAggregation-facilities-in-SQL%2FEnglish-timed"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;action=history"/>
		<updated>2026-04-17T13:10:45Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;diff=45707&amp;oldid=prev</id>
		<title>Sandhya.np14 at 06:48, 6 February 2019</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;diff=45707&amp;oldid=prev"/>
				<updated>2019-02-06T06:48:49Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;amp;diff=45707&amp;amp;oldid=45662&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Sandhya.np14</name></author>	</entry>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;diff=45662&amp;oldid=prev</id>
		<title>PoojaMoolya: Created page with &quot;{| border=1 | '''Time'''  | '''Narration'''   |-  |00:01 | Welcome to the''' Spoken Tutorial''' on '''Aggregation facilities in SQL.'''   |-  |00:07 | In this tutorial we will...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=RDBMS-PostgreSQL/C2/Aggregation-facilities-in-SQL/English-timed&amp;diff=45662&amp;oldid=prev"/>
				<updated>2019-01-30T11:07:23Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{| border=1 | &amp;#039;&amp;#039;&amp;#039;Time&amp;#039;&amp;#039;&amp;#039;  | &amp;#039;&amp;#039;&amp;#039;Narration&amp;#039;&amp;#039;&amp;#039;   |-  |00:01 | Welcome to the&amp;#039;&amp;#039;&amp;#039; Spoken Tutorial&amp;#039;&amp;#039;&amp;#039; on &amp;#039;&amp;#039;&amp;#039;Aggregation facilities in SQL.&amp;#039;&amp;#039;&amp;#039;   |-  |00:07 | In this tutorial we will...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
| '''Time''' &lt;br /&gt;
| '''Narration''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:01&lt;br /&gt;
| Welcome to the''' Spoken Tutorial''' on '''Aggregation facilities in SQL.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:07&lt;br /&gt;
| In this tutorial we will learn about &lt;br /&gt;
&lt;br /&gt;
'''Group by ''' ,  '''Having '''and  '''Order by clause''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:16&lt;br /&gt;
| To record this tutorial, I am using  '''Ubuntu Linux 14.04''' operating system &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:23&lt;br /&gt;
| '''PostgreSQL 9.3.x''' &lt;br /&gt;
&lt;br /&gt;
'''pgAdmin 1.18'''&lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:31&lt;br /&gt;
| To follow this tutorial, you should have basic understanding of &lt;br /&gt;
&lt;br /&gt;
'''SELECT statement''' and  '''Aggregate functions.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:41&lt;br /&gt;
| For more details, refer to the previous '''RDBMS PostgreSQL''' tutorials on this website. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:48&lt;br /&gt;
| '''Aggregate functions''' perform calculations on a set of values and return a single value. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|00:55&lt;br /&gt;
|We learnt about these '''functions '''in the earlier tutorials. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|01:00&lt;br /&gt;
|'''Aggregate functions''' are mostly used with the '''Group By clause '''of the '''SELECT statement'''. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 01:06&lt;br /&gt;
|  '''Group by clause '''is used to collect identical data in groups. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|01:12&lt;br /&gt;
| It returns one record for each group. &lt;br /&gt;
&lt;br /&gt;
'''Group by''' can be done with one or more columns. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|  01:21&lt;br /&gt;
| The syntax for group by clause is: &lt;br /&gt;
&lt;br /&gt;
'''SELECT column1, aggregate-function of column2''' &lt;br /&gt;
&lt;br /&gt;
''FROM table-name''' &lt;br /&gt;
&lt;br /&gt;
'''[WHERE condition]''' &lt;br /&gt;
&lt;br /&gt;
'''GROUP BY column1 , column2''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 01:37&lt;br /&gt;
| Let’s open the '''SQL Editor window '''in''' pgAdmin '''for demonstration. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 01:43&lt;br /&gt;
| Type the code as shown here and execute the query. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 01:50&lt;br /&gt;
| We can see the student details from various departments and cities. &lt;br /&gt;
 &lt;br /&gt;
How can we group these records based on a column? &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 02:00&lt;br /&gt;
| Say, we want to display how many students are there in each department. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 02:06&lt;br /&gt;
| Let us write a query. &lt;br /&gt;
&lt;br /&gt;
First clear the''' SQL editor window.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 02:11&lt;br /&gt;
| Now type the code as shown here. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 02:15&lt;br /&gt;
| This query will group the records in the '''students''' table with the '''deptid''' column. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:21&lt;br /&gt;
|After grouping, the '''aggregate function Count '''will count the number of records under each department. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:28&lt;br /&gt;
|The grouping is based on rows with the same value in the specified column i.e '''deptid''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:36&lt;br /&gt;
|Note that the table is not physically rearranged. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:40&lt;br /&gt;
| Let’s execute the code and see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:44&lt;br /&gt;
|The number of students in each department is displayed  as output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 02:49&lt;br /&gt;
| Next, let us see how to add '''where''' condition in the '''Group by clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:54&lt;br /&gt;
| We will change the same code to add a '''condition.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|02:57&lt;br /&gt;
|Here a '''where condition '''is specified, to display the total number of students in the department ''''CS'.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|03:04&lt;br /&gt;
|The''' Where clause '''used in a query containing a '''Group By clause''', eliminates the rows before grouping. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:11&lt;br /&gt;
| Execute the code now to see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:14&lt;br /&gt;
| Let us see a few more examples of '''Group by clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:18&lt;br /&gt;
| Type the code as shown here. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:21&lt;br /&gt;
| I want to group the '''students table '''to find out the number of male and female students. &lt;br /&gt;
&lt;br /&gt;
So, we have to group the rows with the column '''gender'''. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:31&lt;br /&gt;
| Execute the query. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:33&lt;br /&gt;
| We can see the output as expected. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:36&lt;br /&gt;
| Now I want to find out the maximum '''CGPA''' score of students from each '''city'''. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:43&lt;br /&gt;
| For this, I'll type the query as shown. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:48&lt;br /&gt;
| Now execute the code to see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:52&lt;br /&gt;
| Likewise, we can make use of any '''aggregate function '''in the '''SELECT statement '''with '''Group by clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 03:59&lt;br /&gt;
| Next we will see how to use more than one column in '''Group by clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 04:05&lt;br /&gt;
| Type the code as shown here. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|04:08&lt;br /&gt;
| Here, after''' group by''' you can see two columns '''deptid''' and '''city'''. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|04:14&lt;br /&gt;
|First it will group the records with '''department''' and then group with '''city,''' for each '''department'''. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|04:21&lt;br /&gt;
|The list of column names in the''' SELECT clause '''must appear in the '''Group by clause '''also'''.''' &lt;br /&gt;
&lt;br /&gt;
Let us see execute and see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 04:32&lt;br /&gt;
| Next we will learn about the '''Having clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 04:36&lt;br /&gt;
| '''HAVING clause''' is used to retrieve rows for the specified condition from a grouped result. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|04:43&lt;br /&gt;
| Use the '''WHERE clause '''to exclude rows that you don't want to group, whereas &lt;br /&gt;
&lt;br /&gt;
Use the''' HAVING clause''' to filter rows after they have been grouped.&lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 04:54&lt;br /&gt;
| The syntax is &lt;br /&gt;
&lt;br /&gt;
'''SELECT column1, aggregate-function(column2) ''' &lt;br /&gt;
&lt;br /&gt;
'''FROM table-name''' &lt;br /&gt;
&lt;br /&gt;
'''[WHERE condition]''' &lt;br /&gt;
&lt;br /&gt;
'''GROUP BY column1 , column2''' &lt;br /&gt;
&lt;br /&gt;
'''HAVING Condition''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:09&lt;br /&gt;
| Let us see an example for this. &lt;br /&gt;
&lt;br /&gt;
Switch to the '''SQL Editor window.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:15&lt;br /&gt;
| We will add the condition ''''having city= 'Chennai'.''' &lt;br /&gt;
&lt;br /&gt;
The '''HAVING''' clause comes after the '''Group By''' clause. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|05:24&lt;br /&gt;
|Note that you can apply '''HAVING clause''' only to  columns that appear in the '''Group By clause'''  or an '''aggregate function.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:32&lt;br /&gt;
| Execute and see the result. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:35&lt;br /&gt;
| We can see two rows as output. &lt;br /&gt;
&lt;br /&gt;
There are 3 students from '''CS''' department and 1 student from '''EE''' department. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|05:45&lt;br /&gt;
|The condition specifies that the students are from '''Chennai.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:50&lt;br /&gt;
| Let’s see another example. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 05:53&lt;br /&gt;
| Here, we are using '''aggregate function count''' in the '''having''' '''clause'''. &lt;br /&gt;
&lt;br /&gt;
That is, the count should be greater than 1. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:02&lt;br /&gt;
|Here the student records are grouped by '''department''' and then by '''city'''. &lt;br /&gt;
 &lt;br /&gt;
The '''having''' condition is applied to the filtered rows. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 06:11&lt;br /&gt;
| Once again, execute to see the result. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 06:16&lt;br /&gt;
| Next we will see the '''Order by clause.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 06:19&lt;br /&gt;
|  '''Order By clause''' is used to sort the records in ascending or descending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:25&lt;br /&gt;
| This '''clause''' is always used at the end of the '''SELECT statement.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:30&lt;br /&gt;
| The syntax is shown here&lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:34&lt;br /&gt;
|  Let us switch back to '''SQL Editor window''' for demonstration. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 06:38&lt;br /&gt;
| Type the code as shown on the screen. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:42&lt;br /&gt;
|I have added the''' Order by''' '''clause''' to the end of the''' SELECT statement.''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 06:47&lt;br /&gt;
| Now execute the query. &lt;br /&gt;
&lt;br /&gt;
We see that the total number of students in each department is sorted in ascending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|06:56&lt;br /&gt;
|By default, the sorting is always done in ascending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|07:01&lt;br /&gt;
| Type '''DESC''' at the end of the query to sort in descending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|07:07&lt;br /&gt;
| Execute the query to see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 07:11&lt;br /&gt;
| We can also sort result set on multiple columns. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 07:16&lt;br /&gt;
| Type the code. &lt;br /&gt;
&lt;br /&gt;
This query will sort the '''students table '''by '''city''' in ascending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|07:23&lt;br /&gt;
|Use a comma to separate the columns. &lt;br /&gt;
&lt;br /&gt;
Then within each '''city, '''it will sort by the student names. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|07:31&lt;br /&gt;
|Columns specified in the '''ORDER BY''' clause must be one of the columns selected in the '''SELECT''' column list. &lt;br /&gt;
 &lt;br /&gt;
|- &lt;br /&gt;
| 07:38&lt;br /&gt;
| Let us see the output. Here the city is sorted in ascending order and within each city the student name is sorted in ascending order.&lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 07:50&lt;br /&gt;
| Let us modify the same query to see '''city''' in descending order and the '''student name '''in ascending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 07:58&lt;br /&gt;
| Execute to see the output. &lt;br /&gt;
 &lt;br /&gt;
|- &lt;br /&gt;
| 08:03&lt;br /&gt;
| We will see another example of '''order by''' in '''Group by clause.''' &lt;br /&gt;
&lt;br /&gt;
Execute the query to see the output. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:11&lt;br /&gt;
| Here we can see the sorted result of grouped rows. &lt;br /&gt;
&lt;br /&gt;
'''Dept id '''is sorted in ascending order and within each department, the '''city''' is sorted in ascending order. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:25&lt;br /&gt;
| With this we come to the end of this tutorial. &lt;br /&gt;
&lt;br /&gt;
Let us summarize. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:32&lt;br /&gt;
| In this tutorial, we have learnt about''''' the '''clauses'''''' ''' &lt;br /&gt;
&lt;br /&gt;
'''Group by ''' &lt;br /&gt;
&lt;br /&gt;
'''Having''' &lt;br /&gt;
&lt;br /&gt;
'''Order by''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:40&lt;br /&gt;
| As an assignment-  Write a '''Select statement '''to display the number of students with the same '''CGPA''' &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:47&lt;br /&gt;
|  Hint: Group the rows by '''CGPA''' column &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:51&lt;br /&gt;
| The video at the following link summarises the Spoken Tutorial project. &lt;br /&gt;
&lt;br /&gt;
Please download and watch it. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 08:58&lt;br /&gt;
| The''' Spoken Tutorial Project''' Team &lt;br /&gt;
&lt;br /&gt;
conducts workshops and  gives certificates &lt;br /&gt;
&lt;br /&gt;
For more details, please write to us. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
|09:07&lt;br /&gt;
| Please post your timed queries in this forum. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 09:11&lt;br /&gt;
| Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. &lt;br /&gt;
&lt;br /&gt;
More information on this mission is available at this link. &lt;br /&gt;
&lt;br /&gt;
|- &lt;br /&gt;
| 09:22&lt;br /&gt;
| This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. &lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>PoojaMoolya</name></author>	</entry>

	</feed>