SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL group by
In SQL, The Group By statement is used for organizing similar data into groups. The data is further organized with the help of equivalent function. It means, if different rows in a precise column have the same values, it will arrange those rows in a group.
- The SELECT statement is used with the GROUP BY clause in the SQL query.
- WHERE clause is placed before the GROUP BY clause in SQL.
- ORDER BY clause is placed after the GROUP BY clause in SQL.
Syntax:
FROM table_name
WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;
function_name: Table name.
Condition: which we used.
Sample Table:
Employee
S.no | Name | AGE | Salary |
---|---|---|---|
1 | John | 24 | 25000 |
2 | Nick | 22 | 22000 |
3 | Amara | 25 | 15000 |
4 | Nick | 22 | 22000 |
5 | John | 24 | 25000 |
Student
SUBJECT | YEAR | NAME |
---|---|---|
C language | 2 | John |
C language | 2 | Ginny |
C language | 2 | Jasmeen |
C language | 3 | Nick |
C language | 3 | Amara |
Java | 1 | Sifa |
Java | 1 | dolly |
Example:
Group By single column: Group By single column is used to place all the rows with the same value. These values are of that specified column in one group. It signifies that all rows will put an equal amount through a single column, which is of one appropriate column in one group.
Consider the below query:
GROUP BY NAME;
The output of the query is:
NAME | SALARY |
---|---|
John | 50000 |
Nick | 44000 |
Amara | 15000 |
In the output, the rows which hold duplicate NAME are grouped under a similar NAME, and their corresponding SALARY is the sum of the SALARY of the duplicate rows.
- Groups based on several columns: A group of some columns are GROUP BY column 1, column2, etc. Here, we are placing all rows in a group with the similar values of both column 1 and column 2.
Consider the below query:
FROM Student
Group BY SUBJECT, YEAR;
Output:
SUBJECT | YEAR | Count |
---|---|---|
C language | 2 | 3 |
C language | 3 | 2 |
Java | 1 | 2 |
In the above output, the student with similar SUBJECT and YEAR are grouped in the same place. The students who have only one thing in common belongs to different groups. For example, if the NAME is same and the YEAR is different.
Now, we have to group the table according to more than one column or two columns.
HAVING Clause
WHERE clause is used for deciding purpose. It is used to place conditions on the columns to determine the part of the last result-set of the group. Here, we are not required to use the combined functions like COUNT (), SUM (), etc. with the WHERE clause. After that, we need to use a HAVING clause.
Having clause Syntax:
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING condition
ORDER BY column1, column2;
function_name: Mainly used for name of the function, SUM(), AVG().
table_name: Used for name of the table.
condition: Condition used.
Example:
GROUP BY NAME
HAVING SUM(SALARY)>23000;
Output:
Name | SUM(SALARY) |
---|---|
John | 50000 |
According to the above output, only one name in the NAME column has been listed in the result because there is only one data in the database whose sum of salary is more than 50000.
It should be placed on groups, not on the columns.
Points:
- The GROUP BY Clause is used to group the rows, which have the same values.
- The SELECT statement in SQL is used with the GROUP BY clause.
- In the Group BY clause, the SELECT statement can use constants, aggregate functions, expressions, and column names.
- The GROUP BY Clause is called when the HAVING clause is used to reduce the results.