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

SQL Grouping and Joining Techniques

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)
10 views10 pages

SQL Grouping and Joining Techniques

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

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 [Link]
import [Link] (Or) import [Link] 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

Common questions

Powered by AI

Using a cursor is advantageous when dealing with large datasets that cannot fit into memory all at once or when sequential processing of each row is necessary. Cursors allow efficient row-by-row processing or manipulation, especially useful for complex calculations or operations that need to be applied individually to rows of data. This approach minimizes memory consumption and allows for interaction with data row by row, which can be crucial in high-performance scenarios .

The GROUP BY clause is essential in SQL for aggregating data based on specific columns. It allows the use of aggregate functions like SUM, AVG, etc., on subsets of data defined by the grouped columns. An error occurs when using columns not included in the group expression because each non-aggregated column in the SELECT statement must be part of the GROUP BY clause to ensure logical and accurate data summarization, as aggregates need a set to calculate their results .

ORDER BY is used to sort the result set of a query in either ascending or descending order. By default, ORDER BY sorts results in ascending order. This contrasts with GROUP BY, which groups rows that have the same values in specified columns into summary rows, like "find the total salary of all employees within a department". ORDER BY is about ordering data, while GROUP BY is about aggregating data .

The choice between NATURAL JOIN and JOIN with ON clause depends on the control needed over the join conditions. NATURAL JOIN automatically matches columns with the same names from both tables, which can simplify queries but potentially lead to unexpected results if there are unintended matches. In contrast, using JOIN with an ON clause provides explicit control over which columns are used for joining, allowing for more precise and predictable results, especially when specific join conditions are required .

JOIN operations are critical for optimizing SQL data retrieval by allowing concise and efficient combining of tables. EQUI-JOINs, like INNER JOINs, precisely fetch related data by joining on specified columns, minimizing redundancy. NATURAL JOINs automate matching and reduce code complexity but may inadvertently include undesired columns if there are multiple common columns. CROSS JOINs, though simple, are inefficient due to their large output size without filtering. Selecting the appropriate JOIN based on data architecture and needs ensures effective data retrieval, optimized query performance, and reduced server load .

A CROSS JOIN, or Cartesian product, returns all possible combinations of rows from two tables, resulting in a large dataset comprising every possible pairing of rows from the involved tables. This is typically used when a specific join condition is not necessary or possible. In contrast, a NATURAL JOIN automatically joins tables based on columns that have the same names and compatible data types, ensuring no duplicate columns in the result. NATURAL JOIN is used when one wants to simplify the join process without explicitly stating the join condition, depending on matching column names .

These functions help manage data retrieval efficiently. fetchall() retrieves all remaining rows of a query result, which is useful for full data extraction when there is certainty about the data size. fetchone() retrieves the next single row of a result set, ideal for processing or verifying data one entry at a time without loading everything into memory. fetchmany() retrieves a specified number of rows, offering a balance between batch size processing and resource management, useful for processing data in chunks .

The HAVING clause is used to filter records that are the result of a GROUP BY clause. It allows the application of conditions on aggregated data, effectively acting as a filter for groups according to the criteria specified within it. For instance, if you want to view departments in a company with more than two employees, you could use: SELECT DEPT, COUNT(*) FROM EMP GROUP BY DEPT HAVING COUNT(*) > 2. This filters out any departments that don't meet the criteria .

NATURAL JOIN matches columns with the same name from both tables; if no such common columns exist, it can result in a Cartesian product (similar to CROSS JOIN), generating a large dataset without meaningful connections between the tables. This issue can be resolved by ensuring that tables have at least one pair of identically named columns or by using JOIN with an ON clause to explicitly define the relationship between columns .

The WHERE clause is used to filter rows based on criteria before any grouping is performed, ensuring that only specific records are considered in the groupings. However, it cannot contain aggregate functions, a limitation that confines its use to basic row-level filtering. For instance, filtering employees who earn above a certain salary before grouping them by department ensures only relevant data is summarized .

You might also like