Reasons for the succes of MENARD PRESSUREMETER.pdfmajdiamz
Ad
DBMS: Week 07 - Advanced SQL Queries in MySQL
1. International Islamic University H-10, Islamabad, Pakistan
Database Managements Systems
Week 07
Advanced SQL Queries
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chisht
i
2. Understand and apply aggregate functions in SQL
Use GROUP BY to organize data into groups
Apply HAVING to filter grouped data
Learning Objectives
3. Definition: Functions that perform calculations on a set of values and return a
single value
Common Aggregate Functions:
COUNT() : Number of rows
MAX() : Highest value
MIN() : Lowest value
SUM() : Total of numeric values
AVG() : Average of numeric values
Aggregate Functions Overview
4. First make a table
Aggregate Functions Overview
DROP database if exists week_07_db;
create database week_07_db;
use week_07_db;
CREATE TABLE Employee (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10,2)
);
6. SUM() Example:
AVG() Example:
Use Case:
Calculate total and average salaries for employee data
Using SUM() and AVG()
SELECT SUM(salary) FROM Employee;
SELECT AVG(salary) FROM Employee;
7. COUNT() Example:
Using COUNT()
SELECT COUNT(*) FROM Employee
WHERE Department='IT';
SELECT COUNT(*) AS 'Marketing' FROM Employee
WHERE Department='Marketing';
SELECT COUNT(*) AS 'Total Emp' FROM Employee;
8. MAX() Example:
MIN() Example:
Using MAX() and MIN() Functions
SELECT MAX(salary) FROM Employee;
SELECT MAX(salary) FROM Employee
WHERE Department='Marketing';
SELECT MIN(salary) FROM Employee;
SELECT MIN(salary) FROM Employee
WHERE Department='Marketing';
9. ASC: Ascending Order
DESC: Descending
Order
ORDER BY: Column Name(s)
HAVING: Condition(s)
GROUP BY : Column Name(s)
WHERE: Condition(s)
JOIN: table ON
table.column
FROM: Name of Table(s)
DISTINCT: Name of Table
SELECT: retrieve data from one or more tables in a
database.
Data Query Language (DQL) Command
LIMIT: Number
10. Purpose: Groups rows that have the same values into summary rows
Syntax: SELECT column, COUNT(*) FROM table_name GROUP BY
column;
Example:
Using GROUP BY Command for Organizing Data
SELECT department, COUNT(*) FROM Employee
GROUP BY department;
SELECT department, COUNT(*) FROM Employee
GROUP BY department ORDER BY department ASC;
SELECT department, COUNT(*)
FROM Employee
GROUP BY department
ORDER BY COUNT(*) DESC, department ASC;
11. Purpose: Filters grouped records
Syntax:
SELECT column, COUNT(*) FROM table_name
GROUP BY column HAVING COUNT(*) > 10;
Example:
Difference from WHERE:
WHERE filters rows before grouping. HAVING filters after grouping
Filtering Groups with HAVING
SELECT department, COUNT(*) FROM Employee
GROUP BY department HAVING COUNT(*) > 2;
SELECT department, COUNT(*) FROM Employee
GROUP BY department HAVING COUNT(*) > 1
ORDER BY department ASC;
12. Example:
SELECT category, AVG(price) FROM Products GROUP BY category;
Use Case:
Calculate average salary in each department.
Combining GROUP BY and Aggregate Functions
SELECT department, AVG(salary) FROM
Employee
GROUP BY department
ORDER BY AVG(salary) DESC;
13. Definition: Query within another query
Example:
Use Case: Retrieve employees earning more than the average salary
Use Case: Retrieve employees earning less than or equal to the average salary
Nested Queries (Subqueries)
SELECT * FROM Employee WHERE salary >
(SELECT AVG(salary) FROM Employee);
SELECT * FROM Employee WHERE salary <=
(SELECT AVG(salary) FROM Employee);
14. SELECT Subquery Example:
SELECT name, (SELECT AVG(salary) FROM Employees) AS avg_salary
FROM Employees;
FROM Subquery Example:
SELECT * FROM (SELECT * FROM Orders WHERE status = 'Pending')
AS PendingOrders;
Subqueries in SELECT and FROM
15. Calculate the total number of students in each major
Retrieve departments where more than 10 employees work
Find the highest salary in each department
List all orders with prices above the average order price
Practical Exercises
16. Incorrect Grouping: Grouping on columns not in SELECT
HAVING Misuse: Using HAVING without GROUP BY
Nested Query Errors: Incorrect subquery syntax
Common Errors in Advanced SQL Queries
17. Break down complex queries step by step
Use aliases for subqueries to improve readability
Test subqueries individually before integrating them
Best Practices for Advanced Queries
18. Aggregate functions allow data summarization
GROUP BY organizes data, and HAVING filters grouped results
Subqueries enable more dynamic and complex data retrieval
Best Practices for Advanced Queries