0% found this document useful (0 votes)
15 views7 pages

The SQL Xii-1

The document provides an overview of SQL's GROUP BY statement, which is used to group rows with identical values for summarization, often in conjunction with aggregate functions. It also covers various types of SQL joins, including EQUI JOIN, Natural JOIN, and CARTESIAN JOIN, with examples and syntax for each. Additionally, it includes SQL exercises demonstrating the use of GROUP BY and HAVING clauses with sample data.

Uploaded by

srianshrey
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)
15 views7 pages

The SQL Xii-1

The document provides an overview of SQL's GROUP BY statement, which is used to group rows with identical values for summarization, often in conjunction with aggregate functions. It also covers various types of SQL joins, including EQUI JOIN, Natural JOIN, and CARTESIAN JOIN, with examples and syntax for each. Additionally, it includes SQL exercises demonstrating the use of GROUP BY and HAVING clauses with sample data.

Uploaded by

srianshrey
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/ 7

The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary
rows.
The GROUP BY statement in SQL is used to arrange identical data into groups
based on specified columns. If a particular column has the same values in multiple
rows, the GROUP BY clause will group these rows together. It’s commonly used
with aggregate functions to calculate totals or averages per group.

The GROUP BY statement is often used with aggregate functions (COUNT(),


MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
===
SELECT COUNT(empidID), city
FROM emp
GROUP BY city;
====
SELECT COUNT(empidID), city
FROM emp
GROUP BY city order by city;
===
HAVING CLAUSE
This is where the HAVING clause comes into use. We can use the HAVING clause to
place conditions to decide which group will be part of the final result set. Also, we
can not use aggregate functions like SUM(), COUNT(), etc. with the WHERE clause.
SELECT NAME, SUM(sal) FROM Emp
GROUP BY name
HAVING SUM(sal)>50000;
===

1. EQUI JOIN :

EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative
tables. EQUI JOIN also create JOIN by using JOIN with ON and then providing the
names of the columns with their relative tables to check equality using equal sign
(=).

SELECT student.name, student.id, record.class, record.city


FROM student, record
WHERE student.city = record.city;

2> Natural join is an SQL join operation that creates a join on the base of the
common columns in the tables. To perform natural join there must be one
common attribute(Column) between two tables.Natural Join joins two tables
based on the same attribute name and datatypes. The resulting table will contain
all the attributes of both the table but keep only one copy of each common
column.example
select empid,ename,deptname from emp;
select deptname,manager from dept;
Query join:SELECT *
FROM employee
NATURAL JOIN dept;
output: empid,ename,deptname,manager

3. CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a


CARTESIAN JOIN there is a join for each row of one table to every row of another
table. This usually happens when the matching column or WHERE condition is not
specified.
In the absence of a WHERE condition the CARTESIAN JOIN will behave like a
CARTESIAN PRODUCT . i.e., the number of rows in the result-set is the product of
the number of rows of the two tables.
In the presence of WHERE condition this JOIN will function like a INNER JOIN.
Generally speaking, Cross join is similar to an inner join where the join-condition
will always evaluate to True
Syntax:

SELECT table1.column1 , table1.column2, table2.column1...


FROM table1
CROSS JOIN table2;

1-SELECT * FROM CUSTOMERS


WHERE AGE < 25;
2-SELECT AGE, COUNT(*) AS num_customers
FROM CUSTOMERS
GROUP BY AGE
HAVING COUNT(*) > 2;
3-SELECT ADDRESS
FROM CUSTOMERS
GROUP BY ADDRESS
HAVING AVG(SALARY) > 5240;
4-SELECT ADDRESS
FROM CUSTOMERS
GROUP BY ADDRESS
HAVING MAX(SALARY) > 5240;

SQL Exercise: GROUP BY and HAVING – Class 12


-- Table: STUDENT_MARKS
ID Name Class Subject Marks
1 Anjali 12 Math 88
2 Rohan 12 Science 76
3 Sneha 12 Math 92
4 Aryan 12 Science 65
5 Tanya 12 Math 95
6 Rishi 12 Science 82
7 Priya 12 Math 74

-- Q1. Average marks obtained in each subject


SELECT Subject, AVG(Marks) AS Average_Marks
FROM STUDENT_MARKS
GROUP BY Subject;

-- Q2. Subjects where average marks are greater than 80


SELECT Subject, AVG(Marks) AS Average_Marks
FROM STUDENT_MARKS
GROUP BY Subject
HAVING AVG(Marks) > 80;

-- Q3. Subject and number of students per subject


SELECT Subject, COUNT(*) AS No_of_Students
FROM STUDENT_MARKS
GROUP BY Subject;

-- Q4. Maximum and minimum marks in each subject


SELECT Subject, MAX(Marks) AS Max_Marks, MIN(Marks) AS Min_Marks
FROM STUDENT_MARKS
GROUP BY Subject;

-- Q5. Subjects with more than 3 students


SELECT Subject, COUNT(*) AS Total_Students
FROM STUDENT_MARKS
GROUP BY Subject
HAVING COUNT(*) > 3;

ID Salesperson Region Product Amount


1 Ravi North Pen 150
2 Anya South Pencil 100
3 Ravi North Pencil 200
4 Rahul East Pen 300
5 Priya South Notebook 500
6 Anya South Pen 250
7 Ravi North Notebook 450

Q1. Write an SQL query to display the total sales amount done by each salesperson.

sql
CopyEdit
SELECT Salesperson, SUM(Amount) AS Total_Sales
FROM SALES
GROUP BY Salesperson;

Q2. Display salespersons who have made total sales of more than 400.

sql
CopyEdit
SELECT Salesperson, SUM(Amount) AS Total_Sales
FROM SALES
GROUP BY Salesperson
HAVING SUM(Amount) > 400;
Q3. Show the total sales made in each region.

sql
CopyEdit
SELECT Region, SUM(Amount) AS Regional_Sales
FROM SALES
GROUP BY Region;

Q4. Find out which product has the highest total sales amount.

sql
CopyEdit
SELECT Product, SUM(Amount) AS Product_Sales
FROM SALES
GROUP BY Product
ORDER BY Product_Sales DESC
LIMIT 1;

Q5. Display the regions where total sales are less than 600.

sql
CopyEdit
SELECT Region, SUM(Amount) AS Regional_Sales
FROM SALES
GROUP BY Region
HAVING SUM(Amount) < 600;

Tables: EMPLOYEE and DEPARTMENT

-- Table: EMPLOYEE
-- Columns: EmpID, EmpName, DeptID, Salary
-- Sample Data:
-- (101, 'Ravi', 10, 50000)
-- (102, 'Sneha', 20, 60000)
-- (103, 'Amit', 10, 45000)
-- (104, 'Neha', 30, 70000)
-- (105, 'Karan', 20, 30000)

-- Table: DEPARTMENT
-- Columns: DeptID, DeptName, Budget
-- Sample Data:
-- (10, 'HR', 120000)
-- (20, 'IT', 150000)
-- (30, 'Finance', 180000)

-- Q1. Equi Join: Display employee name and department name.


SELECT E.EmpName, D.DeptName
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.DeptID = D.DeptID;

-- Q2. Equi Join: Display all employee details with department name and budget.
SELECT E.*, D.DeptName, D.Budget
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.DeptID = D.DeptID;

-- Q3. Non-Equi Join: Display employee name and department name where salary
is less than department budget.
SELECT E.EmpName, D.DeptName
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.Salary < D.Budget;

-- Q4. Non-Equi Join: Show all combinations of employees and departments where
salary is greater than department budget.
SELECT E.EmpName, E.Salary, D.DeptName, D.Budget
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.Salary > D.Budget;

You might also like