0% found this document useful (0 votes)
37 views

SQL Function Ip

The document discusses SQL functions including single row functions, aggregate functions, and examples of using functions to manipulate data in a database. Single row functions return a single value for each row while aggregate functions return a single value for multiple rows. The document provides examples of using various string, numeric, and date functions.

Uploaded by

Shubham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

SQL Function Ip

The document discusses SQL functions including single row functions, aggregate functions, and examples of using functions to manipulate data in a database. Single row functions return a single value for each row while aggregate functions return a single value for multiple rows. The document provides examples of using various string, numeric, and date functions.

Uploaded by

Shubham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit 2: Database Query using SQL

Functions in SQL:
 A function is used to perform some particular task and it returns zero or more
values as a result.
 Functions can be applied to work on single or multiple records (rows) of a
table.
Depending on their application in one or multiple rows, SQL functions are
categorised as Single row functions and Aggregate functions.

 Single Row Functions:


 These are also known as Scalar functions.
 Single row functions are applied on a single value and return a single
value.

NOTE: Math functions accept numeric value as input, and return a numeric value
as a result. String functions accept character value as input, and return either
character or numeric values as output. Date and time functions accept date and time
values as input, and return numeric or string, or date and time values as output.
Numeric Functions
Three commonly used numeric functions are POWER( ), ROUND( ) and
MOD( ).

EX: Calculate HRA as 20% of salary and display the result after rounding it off to one
decimal place.
Solution:
mysql> SELECT ROUND(0.10*SALARY,1) "HRA" FROM EMPLOYEE;
+---------+
| HRA |
+---------+
| 25000.0 |
| 15000.0 |
| 20000.0 |
| 12000.0 |
| 10000.0 |
| 20000.0 |
| 10000.0 |
| 20000.0 |
| 20000.0 |
| 15000.0 |
+---------+
10 rows in set (0.00 sec)
String Functions
String functions can perform various operations on alphanumeric data which are
stored in a table. They can be used to change the case (uppercase to lowercase or
vice-versa), extract a substring, calculate the length of a string and so on.
Note: To demonstrate the above string function we use following employee
table.

eid ename gender salary did city post doj EMAIL

1001 ADITYA M 150000 109 PATNA DBA 12-07-2017 [email protected]


1002 SAHIL M 150000 105 DELHI MANAGER 16-08-2022 [email protected]
1003 ALOK M 200000 103 PATNA ARCHITECTURE 21-02-2020 [email protected]
1004 ARYAN M 120000 102 MUMBAI LOAN 17-05-2022 [email protected]
EXECUTIVE
1005 SACHIN M 100000 102 MUMBAI ACCOUNTANT NULL [email protected]
1006 SHREYA F 200000 101 HYDERABAD SOFT 01-01-2022 [email protected]
DEVELOPER
1007 RISHAL M 100000 104 AMRITSAR SALE EXECUTIVE 23-07-2020 [email protected]
1008 UTKARSH M 200000 101 BANGALORE SOFT ENGINEER 12-05-2022 [email protected]
1009 ABHIJAT M 200000 101 PUNE SOFT ENGINEER 12-05-2022 [email protected]
1010 TEJSHWANI F 150000 108 HYDERABAD RECRUITER 12-04-2021 [email protected]
1011 SABA M 120000 102 PATNA LOAN NULL [email protected]
EXECUTIVE
1012 ASHWIN M 110000 103 LUCKNOW MANAGER 10-05-2020 [email protected]
1013 ANJALI F 115000 104 DELHI CORDINATOR 05-01-2021 [email protected]
1014 NIKKY F 105000 106 PATNA MANAGER NULL [email protected]
1015 MANSHI F 125000 102 MUMBAI MANAGER NULL [email protected]
1016 GAURAV M 100000 107 DELHI SALE EXECUTIVE 10-04-2022 [email protected]
Ex(1): Display the employee name in lower case and email in uppercase from
employee table.
SELECT LOWER(ENAME), UPPER(EMAIL) FROM EMPLOYEE;
LOWER(ENAME) UPPER(EMAIL)
aditya [email protected]
sahil [email protected]
alok [email protected]
aryan [email protected]
sachin [email protected]
shreya [email protected]
rishal [email protected]
utkarsh [email protected]
abhijat [email protected]

Ex(2): Display the length of the email and part of the email from the email ID
before the character „@‟. (Note - Do not print „@‟)
SELECT LENGTH(EMAIL), LEFT( EMAIL, INSTR(EMAIL,‟@‟) – 1)
FROM EMPLOYEE;

Note: The function INSTR will return the position of “@” in the email address. So to print
email id without “@” we have to use position -1.
Ex(3): Find the year of joining of employee.
As the first four character from date is year when employee join the job.
SELECT MID(DOJ,1,4) FROM EMPLOYEE;

Ex(4): Display emails after removing the domain name extension “.com” from
emails of the employee.
SELECT TRIM( “.com” FROM EMAIL ) FROM EMPLOYEE;
Ex(5): Display details of all the customers having yahoo emails only.
SELECT * FROM EMPLOYEE WHERE EMAIL LIKE “%yahoo%”;

EX OF TRIM ( ) FUNCTION:
1. Write a query to remove leading and trailing spaces from string:
“ school “
Sol: SELECT TRIM( “ school “);
2. Write a query to remove leading spaces from string: “ school “
Sol: SELECT TRIM( leading ' ' FROM " SCHOOL ");

3. Write a query to remove leading x character from string: „xxxxSCHOOLxxxx


Sol: SELECT TRIM(LEADING 'X' FROM 'XXXXSCHOOLXXXX' );

4. Write a query to remove trailing x character from string:


„XXXXSCHOOLXXXX‟
Sol: SELECT TRIM( TRAILING 'X' FROM 'XXXXSCHOOLXXXX');
Date and Time Functions There are various functions that are used to perform operations
on date and time data. Some of the operations include displaying the current date, extracting each
element of a date (day, month and year), displaying day of the week and so on.

Ex:
1. Write a query to display current date and time on your system.
Sol: SELECT NOW();
2. Write a query to display only current date on your system.
Sol: SELECT CURDATE();

3. Write a query to extract date from a given datetime value „2022-11-30 12:59:25‟.
Sol: SELECT DATE('2022-11-30 12:59:25');

4. Write a query to display the name of month for given date ‟01-12-2022‟.
SELECT MONTHNAME('2022-12-01');

5. Write a query to display the name of day for date 12 Jul 1995.
SELECT DAYNAME('1995-07-12');
6. Select the day, month number and year of joining of all employees.
Sol: SELECT DAY(DOJ), MONTH(DOJ), YEAR(DOJ) FROM EMPLOYEE;

7. If the date of joining is not a Sunday, then display it in the following format
"Wednesday, 12, July, 1995."
SELECT DAYNAME(DOJ), DAY(DOJ), MONTHNAME(DOJ),
YEAR(DOJ) FROM EMPLOYEE WHERE DAYNAME(DOJ) <>
"SUNDAY";
Aggregate Functions
Aggregate functions are also called multiple row functions.
These functions work on a set of records as a whole, and return a single value for each column
of the records on which the function is applied.
The following table will shows the differences between single row functions and multiple row
functions. And another table describes some of the aggregate functions along with their usage.
Note that column must be of numeric type.

Differences between Single row and Multiple row Functions

Aggregate Functions in SQL


GROUP BY
 To fetch a group of rows on the basis of common values in a column.
 This can be done using a GROUP BY clause.
 It groups the rows together that contain the same values in a specified column.
 We can use the aggregate functions (COUNT, MAX, MIN, AVG and SUM) to
work on the grouped values.
 HAVING Clause in SQL is used to specify conditions on the rows with
GROUP BY clause.
Ex(1): To display no. of employee from employee table of each gender.
Sol: SELECT GENDER, COUNT(*) FROM EMPLOYEE
GROUP BY GENDER;

Ex(2): To display did and sum of salary of those department where number
of employee is more than two.
Sol: SELECT DID, SUM(SALARY) FROM EMPLOYEE
GROUP BY DID HAVING COUNT(*) > 2;

Ex(3): To display post wise, no. of employee from employee table.


Sol: SELECT POST, COUNT(*) FROM EMPLOYEE
GROUP BY POST;
Ex(4): To display no. of employee from each city.
Sol: SELECT CITY, COUNT(*) FROM EMPLOYEE
GROUP BY CITY;

Ex(5): To display name of employee from each city with maximum salary.
Sol: SELECT ENAME, CITY, SALARY AS MAX_SAL
FROM EMPLOYEE
GROUP BY CITY
HAVING MAX( SALARY );

You might also like