CBSERanker

Loading

Class Notes: SQL Clauses, Aggregate Functions, and MySQL Functions

Class Notes: SQL Clauses, Aggregate Functions, and MySQL Functions

1. Sorting Output (ORDER BY Clause)

  • By default, records appear in the order they were entered.
  • ORDER BY sorts records in ascending (ASC) or descending (DESC) order.

Examples:

SELECT * FROM emp ORDER BY name;             -- Ascending (default)  
SELECT * FROM emp ORDER BY salary DESC;      -- Descending  
SELECT * FROM emp ORDER BY dept ASC, salary DESC;  -- Multiple columns  

2. Aggregate Functions

Aggregate functions perform calculations on groups of rows and return a single summary value.

Available Functions:

  1. SUM() – Total of values
  2. AVG() – Average of values
  3. COUNT() – Number of non-NULL values
  4. MAX() – Highest value
  5. MIN() – Lowest value
  6. COUNT(*) – Total rows (including NULLs)

Example Table (emp):

EmpnoNameDeptSalary
1RaviSales24000
2SunnySales35000
3ShobitIT30000
4VikramIT27000
5NitinHR45000
6KrishHRNULL

Examples:

SELECT SUM(salary) FROM emp;                     -- Output: 161000  
SELECT SUM(salary) FROM emp WHERE dept='Sales';  -- Output: 59000  

SELECT AVG(salary) FROM emp;                     -- Output: 32200  
SELECT AVG(salary) FROM emp WHERE dept='Sales';  -- Output: 29500  

SELECT COUNT(name) FROM emp;                     -- Output: 5  
SELECT COUNT(salary) FROM emp WHERE dept='HR';   -- Output: 1  
SELECT COUNT(DISTINCT dept) FROM emp;            -- Output: 3  

SELECT MAX(salary) FROM emp;                     -- Output: 45000  
SELECT MAX(salary) FROM emp WHERE dept='Sales';  -- Output: 35000  

SELECT MIN(salary) FROM emp;                     -- Output: 24000  
SELECT MIN(salary) FROM emp WHERE dept='IT';     -- Output: 27000  

SELECT COUNT(*) FROM emp;                        -- Output: 6 (includes NULL)  
SELECT COUNT(salary) FROM emp;                   -- Output: 5 (excludes NULL)  

Key Notes:

  • COUNT(*) counts all rows (including NULL).
  • COUNT(column) counts non-NULL values only.
  • All aggregate functions ignore NULL values.

3. GROUP BY Clause

  • Divides records into logical groups for aggregate calculations.
  • Only grouped columns or aggregate functions can be in SELECT.

Examples:

SELECT dept, SUM(salary) FROM emp GROUP BY dept;  
SELECT dept, AVG(salary), MAX(salary), COUNT(*) FROM emp GROUP BY dept;  

Error Example:

SELECT ename, job, SUM(sal) FROM emp GROUP BY job;  -- Error: ename not in GROUP BY  

4. HAVING Clause

  • Filters groups after GROUP BY (unlike WHERE, which filters before).
  • Can use aggregate functions in HAVING.

Examples:

SELECT dept, AVG(salary) FROM emp GROUP BY dept HAVING dept IN ('HR', 'Sales');  
SELECT dept, MAX(salary), MIN(salary) FROM emp WHERE salary >= 20000  
GROUP BY dept HAVING COUNT(*) > 2;  

WHERE vs. HAVING:

WHEREHAVING
Filters rows before groupingFilters groups after grouping
Cannot use aggregatesCan use aggregates

5. MySQL Functions

A. String Functions

FunctionDescriptionExample
CHAR()Returns ASCII characterSELECT CHAR(65); → ‘A’
CONCAT()Concatenates stringsSELECT CONCAT(name, ' works in ', dept);
LOWER()/LCASE()Converts to lowercaseSELECT LOWER('INDIA'); → ‘india’
SUBSTRING(S,P,N)Extracts substringSELECT SUBSTRING('LAPTOP',3,3); → ‘PTO’
UPPER()/UCASE()Converts to uppercaseSELECT UPPER('india'); → ‘INDIA’
TRIM()Removes leading/trailing spacesSELECT TRIM(' Apple '); → ‘Apple’
INSTR()Finds substring positionSELECT INSTR('COMPUTER','PUT'); → 4
LENGTH()Returns string lengthSELECT LENGTH('Python'); → 6
LEFT(S,N)Returns left N charactersSELECT LEFT('KV NO1',2); → ‘KV’
RIGHT(S,N)Returns right N charactersSELECT RIGHT('KV NO1',3); → ‘NO1’

B. Numeric Functions

FunctionDescriptionExample
MOD(M,N)Remainder of M/NSELECT MOD(11,5); → 1
POWER(B,P)B raised to power PSELECT POWER(2,5); → 32
ROUND(N,D)Rounds N to D decimal placesSELECT ROUND(11.589,2); → 11.59
SQRT(N)Square root of NSELECT SQRT(144); → 12
TRUNCATE(M,N)Truncates without roundingSELECT TRUNCATE(15.789,2); → 15.78

C. Date & Time Functions

FunctionDescriptionExample
CURDATE()Current dateSELECT CURDATE(); → 2023-11-20
MONTH()Extracts monthSELECT MONTH('2023-08-15'); → 8
YEAR()Extracts yearSELECT YEAR('2023-08-15'); → 2023
DAYNAME()Weekday nameSELECT DAYNAME('2023-12-04'); → ‘Monday’
NOW()Current date & time (fixed)SELECT NOW(); → 2023-11-20 10:30:00
SYSDATE()Current date & time (dynamic)SELECT SYSDATE(); → 2023-11-20 10:30:05

Difference: NOW() vs. SYSDATE()

  • NOW() returns the same timestamp for all calls in a query.
  • SYSDATE() returns the current timestamp at execution time.

Leave a Reply

Your email address will not be published. Required fields are marked *