Group by Function
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
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.
2) EQUI-JOIN
3) NATURAL JOIN
Cross Join (Cartesian product) •
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
▫ CROSS
▫ NATURAL
▫ ON
▫ USING
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