The SQL Xii-1
The SQL Xii-1
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.
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
(=).
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
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;
-- 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)
-- 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;