0% found this document useful (0 votes)
26 views4 pages

Chapter 13 12a

Class 12 cs ch 13 notes

Uploaded by

parthrajsharma12
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)
26 views4 pages

Chapter 13 12a

Class 12 cs ch 13 notes

Uploaded by

parthrajsharma12
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/ 4

CHAPTER- 13

GROUPING RECORDS, JOINS IN SQL


13.1 Introduction

This chapter covers advanced SQL techniques for organizing and retrieving data from relational
databases. Students will learn how to group records, apply aggregate functions, and join tables to
extract meaningful insights from multiple datasets.

13.2 Types of SQL Functions

SQL functions are special commands used to perform calculations or operations on data. They can be
categorized as:

1. Aggregate Functions: Operate on multiple rows of a table to return a single summary value.

o SUM(): Calculates the total sum of a numeric column.

o AVG(): Returns the average value of a numeric column.

o MAX(): Finds the maximum value in a column.

o MIN(): Finds the minimum value in a column.

o COUNT(): Counts the number of rows or non-NULL values in a column.

2. Scalar Functions: Operate on individual values and return a single result per row.

o UPPER() / LOWER(): Converts text to uppercase or lowercase.

o LENGTH(): Returns the length of a string.

o ROUND(): Rounds a numeric value to a specified number of decimal places.

13.3 Grouping Result - GROUP BY

The GROUP BY clause in SQL is used to arrange identical data into groups. It’s typically used with
aggregate functions to provide summaries for groups of data.

 Syntax:

SELECT column1, AGGREGATE_FUNCTION(column2)

FROM table_name

GROUP BY column1;
Example:

SELECT department, AVG(salary)

FROM employees

GROUP BY department;

This example groups employees by department and calculates the average salary for each
department.

13.3.1 Nested Groups - Grouping on Multiple Columns

Grouping can also be done on multiple columns, allowing for nested grouping.

 Example:

SELECT department, job_title, COUNT(*)

FROM employees

GROUP BY department, job_title;

This example groups records by both department and job title.

13.3.2 Placing Conditions on Groups - HAVING Clause

The HAVING clause filters groups after the GROUP BY operation, which is different from the WHERE
clause (which filters rows before grouping).

 Syntax:

SELECT column1, AGGREGATE_FUNCTION(column2)

FROM table_name

GROUP BY column1

HAVING condition;

Example:

SELECT department, COUNT(*)

FROM employees

GROUP BY department

HAVING COUNT(*) > 10;

This query returns departments with more than 10 employees.


13.3.3 Non-Group Expressions with GROUP BY

When using GROUP BY, only grouped columns and aggregate functions should be included in the
SELECT statement. Any non-aggregated column in the SELECT must also be in the GROUP BY clause.

Note: Failing to follow this rule will result in an SQL error.

13.4 Joins

Joins are SQL operations that allow combining data from two or more tables based on a related
column.

13.4.1 Cartesian Product

The Cartesian Product (or Cross Join) occurs when every row of one table is paired with every row of
another table.

 Syntax:

SELECT * FROM table1, table2;

 Example:

SELECT * FROM employees, departments;

If employees has 10 rows and departments has 5 rows, the result will have 50 rows.

13.4.2 Table Aliases

Aliases simplify table references in complex queries and are assigned using the AS keyword.

 Syntax:

SELECT alias.column_name

FROM table_name AS alias;

 Example:

SELECT e.name, d.department_name

FROM employees AS e, departments AS d

WHERE e.department_id = d.department_id;

Here, e and d are aliases for the employees and departments tables, respectively.
13.4.3 Equi-Join and Natural Join

These joins are used to retrieve related data based on a common column.

1. Equi-Join: A join that uses equality conditions (=) between columns in two tables.

o Syntax:

SELECT column_list

FROM table1, table2

WHERE table1.common_column = table2.common_column;

o Example:

SELECT employees.name, departments.department_name

FROM employees, departments

WHERE employees.department_id = departments.department_id;

2. Natural Join: An implicit join that automatically matches columns with the same name and
compatible data types.

o Syntax:

SELECT * FROM table1 NATURAL JOIN table2;

o Example:

SELECT * FROM employees NATURAL JOIN departments;

In this case, the join automatically matches columns with the same name in both tables.

13.4.4 Additional Search Conditions in Joins

SQL allows additional conditions in joins to filter data further, such as using comparison operators (>,
<) or other columns.

 Example:

SELECT employees.name, departments.department_name

FROM employees, departments

WHERE employees.department_id = departments.department_id

AND employees.salary > 50000;

This query retrieves the names and departments of employees with a salary greater than 50,000.

You might also like