SlideShare a Scribd company logo
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

Similar to Database Query Using SQL_ip.docx

Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Les04
Les04Les04
Les04
Les04Les04
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
CindyCuesta
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
Ankit Dubey
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
KieveBarreto1
 
It6312 dbms lab-ex2
It6312 dbms lab-ex2It6312 dbms lab-ex2
It6312 dbms lab-ex2
MNM Jain Engineering College
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
Nitesh Singh
 
V34 numeric function-c
V34  numeric function-cV34  numeric function-c
V34 numeric function-c
Dhirendra Chauhan
 
Functions
FunctionsFunctions
Functions
Ankit Dubey
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 

Similar to Database Query Using SQL_ip.docx (20)

Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
 
Les03
Les03Les03
Les03
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Les04
Les04Les04
Les04
 
Les04
Les04Les04
Les04
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
 
It6312 dbms lab-ex2
It6312 dbms lab-ex2It6312 dbms lab-ex2
It6312 dbms lab-ex2
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Clauses
ClausesClauses
Clauses
 
V34 numeric function-c
V34  numeric function-cV34  numeric function-c
V34 numeric function-c
 
Mysql1
Mysql1Mysql1
Mysql1
 
Functions
FunctionsFunctions
Functions
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 

More from 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
 

More from VandanaGoyal21 (9)

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

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 

Recently uploaded (20)

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 

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