0% found this document useful (0 votes)
6 views10 pages

Group by Function

Uploaded by

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

Group by Function

Uploaded by

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

Group by function:

SELECT SUM(SAL)
FROM EMP GROUP BY DEPT;
SELECT JOB,SUM(SAL) FROM EMP GROUP BY DEPT;
SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),COUNT(*) EMPLOYEE_COUNT FROM EMP;
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression

HAVING with GROUP BY


to filter or restrict some rows from the output produced by GROUP BY then we use HAVING clause.
WHERE is used before the GROUP BY. With WHERE we cannot use aggregate function.
SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT HAVING JOB IN (‘HR’,’SALES’)
SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*) FROM EMP GROUP BY DEPT HAVING COUNT(*)>2 SELECT
DEPT,MAX(SAL),MIN(SAL) FROM EMP WHERE SAL>=2000 GROUP BY DEPT HAVING DEPT IN(‘IT’,’HR’)

Orderby clause:
. To see the output rows in sorted or arranged in ascending or descending order SQL provide ORDER BY
clause. By default output will be ascending order(ASC) to see output in descending order we use DESC clause
with ORDER BY.
Select * from emp order by name; (ascending order)
Select * from emp order by salary desc;
Select * from emp order by dept asc, salary desc;

Joining
A join is a query that combines rows from two of more tables.

In JOIN query more than one table are listed in FROM clause.

MySQL provides various type of Joining :

1) CROSS JOIN or CARTESIAN PRODUCT

2) EQUI-JOIN

3) NATURAL JOIN
Cross Join (Cartesian product) •

It return all possible concatenation of all rows from both table


i.e. one row of First table is joined with all the rows of second table.
• Cartesian product join each row of one table with each row of another table. So if –
• First table have 6 rows and second table have 4 rows then
total number of rows in output will be 6 x 4 = 24.
Equi-join
The join in which columns are compared for equality is called Equi-Join. A non-equi join specifies condition
with non-equality operator. In equi-join we put(*) in the select list therefore the common column will appear
twice in the output.
• To understand the output, lets take 2 table one for employee (contains employee detail with deptno) and
another for department contains deptno and other department details.
Natural Join
• The JOIN in which only one of the identical columns exists in called Natural Join.

It is similar to Equi-join except that duplicate columns are eliminated in Natural join that would otherwise appear in
Equi-Join.

• In natural join we specify the names of column to fetch in place of (*) which is responsible of appearing common
column twice in output
Joining Tables using JOIN clause of SQL SELECT
• Till now we have performed joining using traditional SQL method which is common to most of the RDBMS software
now we will learn MySQL style of joining using JOIN clause

• MySQL support various options with JOIN

▫ CROSS

▫ NATURAL

▫ ON

▫ USING

Cartesian product using JOIN


• Select * from shades JOIN color; ( Or )

• Select * from shades CROSS JOIN color;

Importing mysql.connector
import mysql.connector (Or) import mysql.connector as ms
Creating Cursor
 It is a useful control structure of database connectivity.

 When we fire a query to database, it is executed and resultset (set of records) is sent over he connection in one go.

 We may want to access data one row at a time, but query processing cannot happens as one row at a time, so cursor
help us in performing this task.

Cursor stores all the data as a temporary container of returned data and we can fetch data one row at a time from
Cursor.
Output shows cursor is created and query is fired and stored, but no data is coming. To fetch data we have to use
functions like fetchall(), fetchone(), fetchmany() are used

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