SlideShare a Scribd company logo
2
SORTING OUTPUT
By default records will come in the output in the same order in which it was
entered. Tosee the output rows in sorted or arranged in ascending or descending
order SQL provide ORDER BY clause. By default output will be ascending
order(ASC) to see output in descending order we use DESC clause with ORDER BY
.
Select * from emp order by name; (ascending order)
Select * from emp order by salary desc;
Select * from emp order by dept asc, salary desc;
Most read
3
AGGREGATEfunctions
Aggregate function is used to perform calculation on group of rows and return the
calculated summary like sum of salary,averageof salary etc.
Available aggregatefunctions are –
1. SUM()
2. AVG()
3. COUNT()
4. MAX()
5. MIN()
6. COUNT(*)
Most read
6
AGGREGATEfunctions
Select COUNT(name) from emp;
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Output – 5
Select COUNT(salary) from emp where dept=‘HR’;
Output - 1
Select COUNT(DISTINCT dept)from emp;
Output - 3
Most read
Database Query Using SQL
Lets do practical on DATABASE…
SORTING OUTPUT
By default records will come in the output in the same order in which it was
entered. Tosee the output rows in sorted or arranged in ascending or descending
order SQL provide ORDER BY clause. By default output will be ascending
order(ASC) to see output in descending order we use DESC clause with ORDER BY
.
Select * from emp order by name; (ascending order)
Select * from emp order by salary desc;
Select * from emp order by dept asc, salary desc;
AGGREGATEfunctions
Aggregate function is used to perform calculation on group of rows and return the
calculated summary like sum of salary,averageof salary etc.
Available aggregatefunctions are –
1. SUM()
2. AVG()
3. COUNT()
4. MAX()
5. MIN()
6. COUNT(*)
AGGREGATEfunctions
Select SUM(salary) from emp;
Output – 161000
Select SUM(salary) from emp where dept=‘sales’;
Output - 59000
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select AVG(salary) from emp;
Output – 32200
Select AVG(salary) from emp where dept=‘sales’;
Output - 29500
AGGREGATEfunctions
Select COUNT(name) from emp;
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Output – 5
Select COUNT(salary) from emp where dept=‘HR’;
Output - 1
Select COUNT(DISTINCT dept)from emp;
Output - 3
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select MAX(Salary) from emp;
Output – 45000
Select MAX(salary) from emp where dept=‘Sales’;
Output - 35000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select MIN(Salary) from emp;
Output – 24000
Select MIN(salary) from emp where dept=‘IT’;
Output - 27000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
6 Krish HR
Select COUNT(*)from emp;
Output – 6
Select COUNT(salary) from emp;
Output - 5
count(*) Vs count()
Count(*) function is used to count the number of rows in query
output whereas count() is used to count values present in any
column excludingNULL values.
Note:
All aggregatefunction ignores the NULL values.
GROUP BY
GROUP BY clause is used to divide the table into logical groups and we can
perform aggregate functions in those groups. In this case aggregate function
will return output for each group. For example if we want sum of salary of each
department we have to divide table records.
Aggregate functions by default takes the entire table as a single group that’s why we are
getting the sum(), avg(), etc output for the entire table. Now suppose organizationwants the
sum() of all the job separately,or wants to find the average salary of every job. In this case
we have to logically divide our table into groups based on job, so that every group will be
passed to aggregate function for calculation and aggregate function will return the result for
every group.
Group by clause helps up to divide the table into logical groups based on any
column value. In those logically divided records we can apply aggregate
functions.For.E.g.
SELECT SUM(SAL) FROM EMP GROUP BY DEPT;
SELECT JOB,SUM(SAL) FROM EMP GROUP BY
DEPT;
SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),COUNT(*) EMPLOYEE_COUNTFROM EMP;
NOTE :- when we are using GROUP BY we can use only aggregate function and the column
on which we are grouping in the SELECT list because they will form a group other than any
column will gives you an error because they will be not the part of the group.
For e.g.
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression
HAVING with GROUP BY
• If we wantto filter or restrict some rowsfrom the output produced by GROUP BYthen we use HAVING
clause. It is used to put condition of group of rows. With having clause we can use aggregatefunctions
also.
• WHERE is used before the GROUP BY.With WHEREwe cannot use aggregatefunction.
• E.g.
• SELECT DEPT,AVG(SAL)FROM EMP GROUP BYDEPT HAVINGJOB IN (‘HR’,’SALES’
)
• SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*)FROM EMP GROUP BYDEPT HAVING
COUNT(*)>2
• SELECT DEPT,MAX(SAL),MIN(SAL)FROM EMP WHERE SAL>=2000 GROUP BYDEPT HAVING
DEPT IN(‘IT’,’HR’)
MYSQL FUNCTIONS
A function is built – in code for specific purpose that takesvalue and returns a
single value. Valuespassed to functions are known as arguments/parameters.
There are various categories of function in MySQL:-
1) String Function
2) Mathematical function
3) Date and time function
Function Description Example
CHAR() Return character for
given ASCII Code
Select Char(65);
Output- A
CONCAT() Return concatenated
string
Select concat(name, ‘ works in ‘, dept,’ department ’);
LOWER()/
LCASE()
Return string in small
letters
Select lower(‘INDIA’); Output- india
Select lower(name) from emp;
SUBSTRING(S,
P,N) /
MID(S,P,N)
Return N character of
string S, beginning from
P
Select SUBSTRING(‘LAPTOP’,3,3); Output – PTO
Select SUBSTR(‘COMPUTER’,4,3); Output – PUT
UPPER()/
UCASE()
Return string in capital
letters
Select Upper(‘india’); Output- INDIA
LTRIM() Removes leading space Select LTRIM(‘ Apple’); Output- ‘Apple’
RTRIM Remove trailing space Select RTRIM(‘Apple ‘); Output- ‘Apple’
String Function
Function Description Example
TRIM() Remove spaces from
beginning and ending
Select TRIM(‘ Apple ‘); Output-’Apple’
Select* fromemp where trim(name) = ‘Suyash’;
INSTR() It search one string in
another string and
returns position, if not
found 0
SelectINSTR(‘COMPUTER’,’PUT’); Output-4
Select INSTR(‘PYTHON’,’C++’); Output – 0
LENGTH() Returns number of
character in string
Selectlength(‘python’); Output- 7 Select
name, length(name) fromemp
LEFT(S,N) Return N characters of S
from beginning
SelectLEFT(‘KV NO1 TEZPUR’,2); Output-KV
String Function
RIGHT(S,N) Return N characters of S
from ending
Select RIGHT(‘KV NO1 ’,3); Output- NO1
Function Description Example
MOD(M,N) Return remainderM/N SelectMOD(11,5); Output- 1
POWER(B,P) Return B to power P SelectPOWER(2,5); Output-32
ROUND(N,D) Return number rounded to D
place after decimal
SelectROUND(11.589,2); Output- 11.59
SelectROUND(12.999,2); Output- 13.00
SelectROUND(267.478,-2) OUTPUT- 300
SIGN(N) Return -1 for –ve number 1 for
ve
+ number
Selectsign(-10) Output : -1
Selectsign(10); Output : 1
SQRT(N) Returns square rootof N SelectSQRT(144);Output: 12
TRUNCATE(M,
N)
Return number upto N place after
decimal without rounding it
SelectTruncate(15.789,2);Output: 15.79
Numeric Function
Function Description Example
CURDATE()/
CURRENT_DATE()/
CURRENT_DATE
Return the current date Selectcurdate(); Select current_date();
DATE() Return date part from
datetime expression
Select date(‘2018-08-15 12:30’); Output:
2018-08-15
MONTH() Return month fromdate Selectmonth(‘2018-08-15’);Output: 08
YEAR() Return year fromdate Selectyear(‘2018-08-15’);Output: 2018
DAYNAME() Return weekday name Select dayname(‘2018-12-04’); Output:
Tuesday
DAYOFMONTH() Return value from1-31 Select dayofmonth(‘2018-08-15’) Output:
15
DAYOFWEEK() Return weekday index, for
Sunday-1, Monday-2, ..
Select dayofweek(‘2018-12-04’); Output:
3
Date and Time Function
DAYOFYEAR() Return value from1-366 Select dayofyear(‘2018-02-10’) Output:
41
Date and Time Function
Function Description Example
NOW() Return both current date and time Select now();
at which the function executes
SYSDATE() Return both current date and time Select sysdate()
Difference Between NOW() and SYSDATE():
NOW() function return the date and time at which function was executed even if we
execute multiple NOW() function with select. whereas SYSDATE() will always return
date and time at which each SYDATE() function started execution. For example.
mysql> Select now(), sleep(2), now();
Output: 2018-12-04 10:26:20, 0, 2018-12-04 10:26:20
mysql> Select sysdate(), sleep(2), sysdate();
Output: 2018-12-04 10:27:08, 0, 2018-12-04 10:27:10
Database Query Using SQL_ip.docx

More Related Content

What's hot (20)

Array in C
Array in CArray in C
Array in C
Kamal Acharya
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
Aswini Dharmaraj
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
Medhat Dawoud
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
Jaspal Singh
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
Nishant Munjal
 
Supervised Machine Learning Algorithm
Supervised Machine Learning AlgorithmSupervised Machine Learning Algorithm
Supervised Machine Learning Algorithm
Pyingkodi Maran
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
poonam.rwalia
 
Windows Server 2008 Active Directory
Windows Server 2008 Active DirectoryWindows Server 2008 Active Directory
Windows Server 2008 Active Directory
anilinvns
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Python tuples and Dictionary
Python   tuples and DictionaryPython   tuples and Dictionary
Python tuples and Dictionary
Aswini Dharmaraj
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
Jaspal Singh
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Supervised Machine Learning Algorithm
Supervised Machine Learning AlgorithmSupervised Machine Learning Algorithm
Supervised Machine Learning Algorithm
Pyingkodi Maran
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
Windows Server 2008 Active Directory
Windows Server 2008 Active DirectoryWindows Server 2008 Active Directory
Windows Server 2008 Active Directory
anilinvns
 

Similar to Database Query Using SQL_ip.docx (20)

SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
MrHello6
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
अभिषेक शर्मा
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhhSQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
NaveeN547338
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
Shahadat153031
 
Data Base Management Slides SQL with example
Data Base Management Slides SQL with exampleData Base Management Slides SQL with example
Data Base Management Slides SQL with example
AmeerHamza708060
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sql
gourav kottawar
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHONAGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
examcelldavrng
 
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
DevKartikSharma1
 
DBMS: Week 07 - Advanced SQL Queries in MySQL
DBMS: Week 07 - Advanced SQL Queries in  MySQLDBMS: Week 07 - Advanced SQL Queries in  MySQL
DBMS: Week 07 - Advanced SQL Queries in MySQL
RashidFaridChishti
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
Sachin Shukla
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
Balqees Al.Mubarak
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
Dr. C.V. Suresh Babu
 
SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5
Umair Amjad
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
Evelyn Oluchukwu
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhhSQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
SQL-AGG-FUN.pdfiiiijuyyttfffgyyuyyyyyhhh
NaveeN547338
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
Shahadat153031
 
Data Base Management Slides SQL with example
Data Base Management Slides SQL with exampleData Base Management Slides SQL with example
Data Base Management Slides SQL with example
AmeerHamza708060
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sql
gourav kottawar
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHONAGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
examcelldavrng
 
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
2 puc cs.pptx bsbshsjshsbbsjsjshdbdbbdbdd
DevKartikSharma1
 
DBMS: Week 07 - Advanced SQL Queries in MySQL
DBMS: Week 07 - Advanced SQL Queries in  MySQLDBMS: Week 07 - Advanced SQL Queries in  MySQL
DBMS: Week 07 - Advanced SQL Queries in MySQL
RashidFaridChishti
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 
SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5
Umair Amjad
 
Ad

More from VandanaGoyal21 (9)

sql_data.pdf
sql_data.pdfsql_data.pdf
sql_data.pdf
VandanaGoyal21
 
sample project-binary file.docx
sample project-binary file.docxsample project-binary file.docx
sample project-binary file.docx
VandanaGoyal21
 
STACK.docx
STACK.docxSTACK.docx
STACK.docx
VandanaGoyal21
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
VandanaGoyal21
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
VandanaGoyal21
 
functions.docx
functions.docxfunctions.docx
functions.docx
VandanaGoyal21
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
CLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdfCLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdf
VandanaGoyal21
 
WEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdfWEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdf
VandanaGoyal21
 
Ad

Recently uploaded (20)

Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 

Database Query Using SQL_ip.docx

  • 1. Database Query Using SQL Lets do practical on DATABASE…
  • 2. SORTING OUTPUT By default records will come in the output in the same order in which it was entered. Tosee the output rows in sorted or arranged in ascending or descending order SQL provide ORDER BY clause. By default output will be ascending order(ASC) to see output in descending order we use DESC clause with ORDER BY . Select * from emp order by name; (ascending order) Select * from emp order by salary desc; Select * from emp order by dept asc, salary desc;
  • 3. AGGREGATEfunctions Aggregate function is used to perform calculation on group of rows and return the calculated summary like sum of salary,averageof salary etc. Available aggregatefunctions are – 1. SUM() 2. AVG() 3. COUNT() 4. MAX() 5. MIN() 6. COUNT(*)
  • 4. AGGREGATEfunctions Select SUM(salary) from emp; Output – 161000 Select SUM(salary) from emp where dept=‘sales’; Output - 59000 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 5. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select AVG(salary) from emp; Output – 32200 Select AVG(salary) from emp where dept=‘sales’; Output - 29500
  • 6. AGGREGATEfunctions Select COUNT(name) from emp; Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Output – 5 Select COUNT(salary) from emp where dept=‘HR’; Output - 1 Select COUNT(DISTINCT dept)from emp; Output - 3
  • 7. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select MAX(Salary) from emp; Output – 45000 Select MAX(salary) from emp where dept=‘Sales’; Output - 35000
  • 8. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select MIN(Salary) from emp; Output – 24000 Select MIN(salary) from emp where dept=‘IT’; Output - 27000
  • 9. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 6 Krish HR Select COUNT(*)from emp; Output – 6 Select COUNT(salary) from emp; Output - 5
  • 10. count(*) Vs count() Count(*) function is used to count the number of rows in query output whereas count() is used to count values present in any column excludingNULL values. Note: All aggregatefunction ignores the NULL values.
  • 11. GROUP BY GROUP BY clause is used to divide the table into logical groups and we can perform aggregate functions in those groups. In this case aggregate function will return output for each group. For example if we want sum of salary of each department we have to divide table records.
  • 12. Aggregate functions by default takes the entire table as a single group that’s why we are getting the sum(), avg(), etc output for the entire table. Now suppose organizationwants the sum() of all the job separately,or wants to find the average salary of every job. In this case we have to logically divide our table into groups based on job, so that every group will be passed to aggregate function for calculation and aggregate function will return the result for every group.
  • 13. Group by clause helps up to divide the table into logical groups based on any column value. In those logically divided records we can apply aggregate functions.For.E.g. SELECT SUM(SAL) FROM EMP GROUP BY DEPT; SELECT JOB,SUM(SAL) FROM EMP GROUP BY DEPT; SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),COUNT(*) EMPLOYEE_COUNTFROM EMP; NOTE :- when we are using GROUP BY we can use only aggregate function and the column on which we are grouping in the SELECT list because they will form a group other than any column will gives you an error because they will be not the part of the group. For e.g. SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB; Error -> because Ename is not a group expression
  • 14. HAVING with GROUP BY • If we wantto filter or restrict some rowsfrom the output produced by GROUP BYthen we use HAVING clause. It is used to put condition of group of rows. With having clause we can use aggregatefunctions also. • WHERE is used before the GROUP BY.With WHEREwe cannot use aggregatefunction. • E.g. • SELECT DEPT,AVG(SAL)FROM EMP GROUP BYDEPT HAVINGJOB IN (‘HR’,’SALES’ ) • SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*)FROM EMP GROUP BYDEPT HAVING COUNT(*)>2 • SELECT DEPT,MAX(SAL),MIN(SAL)FROM EMP WHERE SAL>=2000 GROUP BYDEPT HAVING DEPT IN(‘IT’,’HR’)
  • 15. MYSQL FUNCTIONS A function is built – in code for specific purpose that takesvalue and returns a single value. Valuespassed to functions are known as arguments/parameters. There are various categories of function in MySQL:- 1) String Function 2) Mathematical function 3) Date and time function
  • 16. Function Description Example CHAR() Return character for given ASCII Code Select Char(65); Output- A CONCAT() Return concatenated string Select concat(name, ‘ works in ‘, dept,’ department ’); LOWER()/ LCASE() Return string in small letters Select lower(‘INDIA’); Output- india Select lower(name) from emp; SUBSTRING(S, P,N) / MID(S,P,N) Return N character of string S, beginning from P Select SUBSTRING(‘LAPTOP’,3,3); Output – PTO Select SUBSTR(‘COMPUTER’,4,3); Output – PUT UPPER()/ UCASE() Return string in capital letters Select Upper(‘india’); Output- INDIA LTRIM() Removes leading space Select LTRIM(‘ Apple’); Output- ‘Apple’ RTRIM Remove trailing space Select RTRIM(‘Apple ‘); Output- ‘Apple’ String Function
  • 17. Function Description Example TRIM() Remove spaces from beginning and ending Select TRIM(‘ Apple ‘); Output-’Apple’ Select* fromemp where trim(name) = ‘Suyash’; INSTR() It search one string in another string and returns position, if not found 0 SelectINSTR(‘COMPUTER’,’PUT’); Output-4 Select INSTR(‘PYTHON’,’C++’); Output – 0 LENGTH() Returns number of character in string Selectlength(‘python’); Output- 7 Select name, length(name) fromemp LEFT(S,N) Return N characters of S from beginning SelectLEFT(‘KV NO1 TEZPUR’,2); Output-KV String Function
  • 18. RIGHT(S,N) Return N characters of S from ending Select RIGHT(‘KV NO1 ’,3); Output- NO1
  • 19. Function Description Example MOD(M,N) Return remainderM/N SelectMOD(11,5); Output- 1 POWER(B,P) Return B to power P SelectPOWER(2,5); Output-32 ROUND(N,D) Return number rounded to D place after decimal SelectROUND(11.589,2); Output- 11.59 SelectROUND(12.999,2); Output- 13.00 SelectROUND(267.478,-2) OUTPUT- 300 SIGN(N) Return -1 for –ve number 1 for ve + number Selectsign(-10) Output : -1 Selectsign(10); Output : 1 SQRT(N) Returns square rootof N SelectSQRT(144);Output: 12 TRUNCATE(M, N) Return number upto N place after decimal without rounding it SelectTruncate(15.789,2);Output: 15.79 Numeric Function
  • 20. Function Description Example CURDATE()/ CURRENT_DATE()/ CURRENT_DATE Return the current date Selectcurdate(); Select current_date(); DATE() Return date part from datetime expression Select date(‘2018-08-15 12:30’); Output: 2018-08-15 MONTH() Return month fromdate Selectmonth(‘2018-08-15’);Output: 08 YEAR() Return year fromdate Selectyear(‘2018-08-15’);Output: 2018 DAYNAME() Return weekday name Select dayname(‘2018-12-04’); Output: Tuesday DAYOFMONTH() Return value from1-31 Select dayofmonth(‘2018-08-15’) Output: 15 DAYOFWEEK() Return weekday index, for Sunday-1, Monday-2, .. Select dayofweek(‘2018-12-04’); Output: 3 Date and Time Function
  • 21. DAYOFYEAR() Return value from1-366 Select dayofyear(‘2018-02-10’) Output: 41
  • 22. Date and Time Function Function Description Example NOW() Return both current date and time Select now(); at which the function executes SYSDATE() Return both current date and time Select sysdate() Difference Between NOW() and SYSDATE(): NOW() function return the date and time at which function was executed even if we execute multiple NOW() function with select. whereas SYSDATE() will always return date and time at which each SYDATE() function started execution. For example. mysql> Select now(), sleep(2), now(); Output: 2018-12-04 10:26:20, 0, 2018-12-04 10:26:20 mysql> Select sysdate(), sleep(2), sysdate(); Output: 2018-12-04 10:27:08, 0, 2018-12-04 10:27:10