Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Built-in functions & Group By Clause Having Clause.

Similar presentations


Presentation on theme: "SQL Built-in functions & Group By Clause Having Clause."— Presentation transcript:

1 SQL Built-in functions & Group By Clause Having Clause

2 SQL Scalar functions Operate on elements from a single row as inputs, return a single value as output. Scalar functions return a single value based on scalar input arguments. Some scalar functions, such as CURRENT_TIME, do not require any arguments. Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

3 SQL Scalar functions Operate on elements from a single row as inputs, return a single value as output. Scalar functions return a single value based on scalar input arguments. Some scalar functions, such as CURRENT_TIME, do not require any arguments. Useful scalar functions: UPPER()/UCASE() - Converts a field to upper case LOWER()/LCASE() - Converts a field to lower case MID() - Extract characters from a text field LEN() - Returns the length of a text field ROUND() - Rounds a numeric field to the number of decimals specified

4 SQL UPPER() Function The UPPER() Function The UPPER() function converts the value of a field to uppercase. Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 Syntax for SQL Server SELECT UPPER(column_name) FROM table_name;

5 Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 SQL UPPER() Function Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMULTAN 78971.72727 3AliMULTAN 89080.90909 4AhmadLAHORE 76069.09091 5WaqasVEHARI 59053.63636 SELECT RollNo,Name,upper(City), Marks, Per FROM Student;

6 SQL LOWER() Function The LOWER() Function The LOWER() function converts the value of a field to lowercase. Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 Syntax for SQL Server SELECT LOWER(column_name) FROM table_name;

7 Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 SQL LOWER() Function SELECT RollNo,Name,lower(City), Marks, Per FROM Student; Student RollNoNameCityMarksPer 1FARMANdikhan 67861.63636 2Imranmultan 78971.72727 3Alimultan 89080.90909 4Ahmadlahore 76069.09091 5Waqasvehari 59053.63636

8 SQL LEN() Function The LEN() Function The LEN() function returns the length of the value in a text field. SQL LEN() Syntax SELECT LEN(column_name) FROM table_name; Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

9 SQL LEN() Function Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 SELECT RollNo,Name,LEN(Name) as Length FROM Student Result RollNoNameLength 1FARMAN 6 2Imran 5 3Ali 3 4Ahmad 5 5Waqas 5

10 SQL ROUND() Function The ROUND() function is used to round a numeric field to the number of decimals specified. Syntax SELECT ROUND(column_name,decimals) FROM table_name; Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

11 SQL ROUND() Function select RollNo,Name,City,Marks,ROUND(Per,2) as Percentage from students Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.64 2ImranMultan 78971.73 3AliMultan 89080.91 4AhmadLahore 76069.09 5WaqasVehari 59053.64 Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

12 SQL FORMAT() Function Returns a value formatted with the specified format and optional culture. Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general data type conversions, use CAST or CONVERT. Syntax FORMAT(value, format, culture) ParameterDescription valueRequired. The value to be formatted formatRequired. The format pattern cultureOptional. Specifies a culture (from SQL Server 2017)

13 SQL FORMAT() Function SQL Date Format with the FORMAT function Use the FORMAT function to format the date and time data types from a date column (date, datetime, datetime2, smalldatetime, datetimeoffset, etc. data type) in a table or a variable such as GETDATE()GETDATE()  To get DD/MM/YYYY use SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date 18/06/2022  To get MM-DD-YYYY use SELECT FORMAT (getdate(), 'MM- dd-yyyy') as date 06-18-2022  To get MM-DD-YY use SELECT FORMAT (getdate(), 'MM- dd-yy') as date 06-18-22

14  dd - day number from 01-31  MM - month number from 01-12  yy - two digits year number i.e 22  yyyy – Four digits year Number i.e 2022  hh - hour of day from 01-12  mm - minutes of hour from 00-59  ss - seconds of minute from 00-59

15 SQL AGGREGATE FUNCTIONS Aggregate functions perform calculations over multiple rows and they return a single value. Without GROUP By Clause, all rows are arranged as one group. Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

16 SQL AGGREGATE FUNCTIONS Useful aggregate functions: o AVG() - Returns the average value o COUNT() - Returns the number of rows o MAX() - Returns the largest value o MIN() - Returns the smallest value o SUM() - Returns the sum Aggregate functions perform calculations over multiple rows and they return a single value. Without GROUP By Clause, all rows are arranged as one group.

17 The AVG() Function The AVG() function returns the average value of a numeric column. Syntax SELECT AVG(column_name) FROM table_name Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636

18 The AVG() Function select AVG(Marks) as 'Average Marks' from Students Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 Average Marks 741

19 The AVG() Function select AVG(Per) as 'Average Percentage' from Students Student RollNoNameCityMarksPer 1FARMANDIKHAN 67861.63636 2ImranMultan 78971.72727 3AliMultan 89080.90909 4AhmadLahore 76069.09091 5WaqasVehari 59053.63636 Average Percentage 67.399998

20 The COUNT() function returns the number of rows that matches a specified criteria. SQL COUNT(column_name) Syntax The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name; SQL COUNT(*) Syntax The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) FROM table_name; The COUNT() Function

21 SQL COUNT(DISTINCT column_name) Syntax The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT column_name) FROM table_name; Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.

22 The MAX() Function The MAX() function returns the largest value of the selected column. SQL MAX() Syntax SELECT MAX(column_name) FROM table_name; SQL MIN() Function The MIN() Function The MIN() function returns the smallest value of the selected column. SQL MIN() Syntax SELECT MIN(column_name) FROM table_name;

23 SQL SUM() Function The SUM() Function The SUM() function returns the total sum of a numeric column. SQL SUM() Syntax SELECT SUM(column_name) FROM table_name;

24 Employees ENoENameJobTitleSalaryDept ActiveSt atus 1AliLDC 26000CSYes 2IkramLec 56000MSYes 3AhmadAP 89000CSYes 4WaqarAP 98000MSYes 5IjazAssistant 32500MSNo 6ImranLDC 28000MSYes 7HarisLEC 58000MSYes 8AbidLDC 30000MathYes 9JavedAP 91000MathYes 10WajidLec 70000CSNo

25 Employeesn ENoENameJobTitleSalaryDeptActiveStatus 1AliLDC 26000CSYes 2IkramLec 56000MSYes 3AhmadAP 89000CSYes 4WaqarAP 98000MSYes 5Ijaz Assistan t 32500MSNo 6ImranLDC 28000MSYes 7HarisLEC 58000MSYes 8AbidLDC 30000MathYes 9JavedAP 91000MathYes 10WajidLec 70000CSNo DeptTotal Salary CS184600 Math121000 MS273400 JobTitleTotal Salary AP278000 Assistant32500 LDC84500 Lec184000

26 GROUP BY Aggregate functions often need an added GROUP BY statement. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. SELECT column_list FROM table-name [WHERE Clause] [GROUP BY clause] [HAVING clause] [ORDER BY clause] ----------------

27 GROUP BY Aggregate functions often need an added GROUP BY statement. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Syntax SELECT column_name, aggregate_function(Column_name) FROM table_name [WHERE ] GROUP BY column_name [Having Clause]

28 Employees ENoENameJobTitleSalaryDeptActiveStatus 1AliLDC 26000CSYes 2IkramLec 56000MSYes 3AhmadAP 89000CSYes 4WaqarAP 98000MSYes 5Ijaz Assistan t 32500MSNo 6ImranLDC 28000MSYes 7HarisLEC 58000MSYes 8AbidLDC 30000MathYes 9JavedAP 91000MathYes 10WajidLec 70000CSNo DeptTotal Salary CS184600 Math121000 MS273400 JobTitleTotal Salary AP278000 Assistant32500 LDC84500 Lec184000

29 Employees ENoENameJobTitleSalaryDeptActiveStatus 1AliLDC 26000CSYes 2IkramLec 56000MSYes 3AhmadAP 89000CSYes 4WaqarAP 98000MSYes 5Ijaz Assistan t 32500MSNo 6ImranLDC 28000MSYes 7HarisLEC 58000MSYes 8AbidLDC 30000MathYes 9JavedAP 91000MathYes 10WajidLec 70000CSNo DeptStrengthTotal Salary CS3184600 Math2121000 MS5273400 JobTitleStrengthTotal Salary AP3278000 Assistant132500 LDC384500 Lec3184000

30 Having The HAVING clause is often used with the GROUP BY clause to filter groups based on a specified list of conditions. SELECT column_name, aggregate_function(Column_name) FROM table_name GROUP BY column_name [ Having ] In this syntax, the GROUP BY clause summarizes the rows into groups and the HAVING clause applies one or more conditions to these groups. Only groups that make the conditions evaluate to TRUE are included in the result.

31 Order By with Group By SELECT column_name, aggregate_function(Column_name) FROM table_name [ where ] GROUP BY column_name [ Having ] [ Order by ASC|DESC]


Download ppt "SQL Built-in functions & Group By Clause Having Clause."

Similar presentations


Ads by Google