0% found this document useful (0 votes)
79 views27 pages

12 Grouping Records, Joins in SQL

The document provides an overview of SQL functions, specifically focusing on single row (scalar) and multiple row (aggregate) functions, as well as the GROUP BY clause for grouping records in MySQL. It explains the use of the HAVING clause for placing conditions on groups, the concept of nested grouping, and different types of joins, including equi-joins and natural joins. Additionally, it highlights the importance of using table aliases for simplifying queries.

Uploaded by

sathyaa.m1720
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)
79 views27 pages

12 Grouping Records, Joins in SQL

The document provides an overview of SQL functions, specifically focusing on single row (scalar) and multiple row (aggregate) functions, as well as the GROUP BY clause for grouping records in MySQL. It explains the use of the HAVING clause for placing conditions on groups, the concept of nested grouping, and different types of joins, including equi-joins and natural joins. Additionally, it highlights the importance of using table aliases for simplifying queries.

Uploaded by

sathyaa.m1720
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/ 27

Grouping records,

joins in MySQL
CBSE
CLASS 12

KRISH INFO TECH


Single Row (Scaler) functions

• Work with a single row at a time. A single row function


returns a result for every row of a queried table.
Types of sql
functions Multiple Row (Group / Aggregate) functions

• Work with data of multiple rows at a time and return


aggregated value.

KRISH INFO TECH


The difference between these two types of
functions is in the number of rows they act
upon.

KRISH INFO TECH


Grouping result – group by
The GROUP BY clause combines all those records that have identical values in a particular field or a group of
fields.
This grouping results into one summary record per group if group functions are used with it.

The GROUP BY clause is used in SELECT statements to divide the table into two groups.

Grouping can be done by a column name, or with aggregate functions in which case the aggregate
produces a value for each group.
KRISH INFO TECH
example

KRISH INFO TECH


example

KRISH INFO TECH


Nested groups – grouping on multiple columns

This can be done by specifying in


With GROUP BY clause, you can create GROUP BY expression where the first
The last field determines the lowest
groups within groups. Such type of field determines the highest group
level of grouping.
grouping is called nested grouping. level the second field determines the
second group level and so on.

KRISH INFO TECH


example

KRISH INFO TECH


example

KRISH INFO TECH


example

KRISH INFO TECH


The HAVING clause places conditions on
placing groups in contrast to WHERE clause that
places conditions on individual rows.
conditions on
groups – While WHERE conditions cannot include
having clause aggregate functions, having conditions can
do so.
KRISH INFO TECH
KRISH INFO TECH

example
KRISH INFO TECH

example
KRISH INFO TECH

example
If you include a non-group expression in the select
list of a query with GROUP BY, MySQL will not
produce any error.
Non-group Rather it will pick value of the specified non group
expressions field from the first row of the group.
with group by
But we do not recommend this practice because it
will produce ambiguous results.
KRISH INFO TECH
KRISH INFO TECH

example
joins
A joint is a query that combines rows from two or more tables.

In a join query, more than one table are listed in FROM clause.

The function of combining data from multiple tables is called joining.


KRISH INFO TECH
example

KRISH INFO TECH


example

KRISH INFO TECH


•In unrestricted joint or cartesian product of 2
tables, all possible concatenations are formed
of all rows of both the tables.
•Unrestricted joints happens when no
Cartesian particular rows and columns are selected.
product •it returns n1 X n2 rows where n1 is the
number of rows in the first table and n2 is
the number of rows in the second table.
KRISH INFO TECH
example

KRISH INFO TECH


• A table alias is a temporary label given along with
the table name in FROM clause.
Table alias • To cut down on the amount of typing required in
your queries you can use aliases for table names
in the SELECT and WHERE clauses.

KRISH INFO TECH


example

KRISH INFO TECH


Equi-Join

• The join in which columns are compared for equality is called equi-join
• A non equi-join is a query that specifies some relationship other than equality between
the columns.

Equi-join and
Natural Join
natural join
• The join in which only one of the identical column exists is called natural join

The Equi-join and Natural Join are equivalent except that duplicate
columns are eliminated in the natural join that would otherwise appear in
the equi-join.
KRISH INFO TECH
Without ORDER BY

Order by

With ORDER BY
KRISH INFO TECH
KRISH INFO TECH
Follow and subscribe

https://www.youtube.com/@Krish_Info_Tech

sites.google.com/view/krishinfotech/home

@computer_tricks_nd_tips

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy